Facets.spec.js 15.2 KB
define(function(require) {
    'use strict';
    var _ = require('underscore');
    var Backbone = require('backbone');
    var Facets = require('solr/model/Facets');

    function setData(obj, key, value) {
        return obj[key] = value;
    }

    function getData(obj, key) {
        return obj[key];
    }

    function createSolrResponse(has) {
        var solrData = {};
        var facetCounts = setData(solrData, 'facet_counts', {});
        var facetIntervals = setData(facetCounts, 'facet_intervals', {});
        var facetRanges = setData(facetCounts, 'facet_ranges', {});
        var facetFields = setData(facetCounts, 'facet_fields', {});
        var facetQueries = setData(facetCounts, 'facet_queries', {});
        if (has.stats) {
            solrData.stats = {};
            setData(solrData.stats, 'stats_fields', {});
        }
        return solrData;
    }

    function makeSolrResponseMixedCase(solrData) {
        var facetCounts = getData(solrData, 'facet_counts');
        return {
            facetCounts: facetCounts,
            facetRanges: getData(facetCounts, 'facet_ranges'),
            facetFields: getData(facetCounts, 'facet_fields'),
            facetQueries: getData(facetCounts, 'facet_queries'),
            statsFields: getData(solrData.stats, 'stats_fields'),
        };
    }

    describe('Facets', function() {
        it('loads', function() {
            expect(Facets).toBeDefined();
        });

        describe('foo', function() {
            var f;
            var formData = [];
            var solrData = [];
            var origNow;
            var onItemChange;
            beforeEach(function() {
                jasmine.clock().install();
                jasmine.clock().mockDate();
                origNow = _.now;
                _.now = function() { return new Date().getTime(); };
                spyOn(Backbone, 'ajax');
                var facets = {
                    model: new Facets.Facet({
                        formName: 'model',
                        label: 'Model',
                    }, {
                        queryField: 'model_s',
                        facetType: 'field',
                        bins: {
                            misc: ['saturn', 'jeep'],
                            startsWithB: ['buick'],
                        },
                    }),
                    year: new Facets.Facet({
                        formName: 'year',
                        label: 'Year',
                    }, {
                        queryField: 'year_d',
                        comparator: Facets.Sort.date,
                        facetType: 'year',
                        facetStats: true,
                    }),
                    dateOfBirth: new Facets.Facet({
                        formName: 'dateOfBirth',
                        label: 'Birthday',
                    }, {
                        queryField: 'dateOfBirth_d',
                        comparator: Facets.Sort.date,
                        facetType: 'year',
                        facetStats: true,
                        other: 'before',
						start: 'NOW/YEAR-25YEAR',
                    }),
                    price: new Facets.Facet({
                        formName: 'price',
                        label: 'Price',
                    }, {
                        queryField: 'price_i',
                        comparator: Facets.Sort.number,
                        facetType: 'field',
                        facetStats: true,
                    }),
                };
                onItemChange = jasmine.createSpy('onItemChange');
                f = new Facets(facets, {search: {url: '/'}});
                f.on('item-change', onItemChange);
            });
            afterEach(function() {
                jasmine.clock().uninstall();
                _.now = origNow;
            });
            describe('initial settings', function() {
                it('foo', function() {
                    expect(true).toBe(true);
                });
            });
            describe('step 1', function() {
                beforeEach(function() {
                    formData.push(f.getFacetFormData());
                });
                function expectItem(item, key, value, checked) {
                    expect(item.get('key')).toEqual(key);
                    expect(item.get('value')).toEqual(value);
                    expect(item.get('checked')).toEqual(checked);
                }

                describe('facets', function() {
                    var modelFacet, modelItems;
                    var yearFacet, yearItems;
                    var dobFacet, dobItems;
                    var priceFacet, priceItems;
                    beforeEach(function() {
                        var solrResponse = createSolrResponse({stats: true});
                        var solrData = makeSolrResponseMixedCase(solrResponse);
                        //console.log('solrData', JSON.stringify(solrResponse));
                        solrData.facetRanges.year = {
                            counts: [
                                '2000-01-01T00:00:00.000Z', 1,
                                '2001-01-01T00:00:00.000Z', 4,
                                '2002-01-01T00:00:00.000Z', 0,
                                '2003-01-01T00:00:00.000Z', 3,
                                '2004-01-01T00:00:00.000Z', 0,
                                '2005-01-01T00:00:00.000Z', 0,
                                '2006-01-01T00:00:00.000Z', 0,
                                '2007-01-01T00:00:00.000Z', 0,
                                '2008-01-01T00:00:00.000Z', 7,
                                '2009-01-01T00:00:00.000Z', 0,
                                '2010-01-01T00:00:00.000Z', 1,
                            ],
                        };
                        solrData.facetRanges.dateOfBirth = {
                            counts: [
                                '1974-01-01T00:00:00.000Z', 1,
                                '1972-01-01T00:00:00.000Z', 1,
                                '1970-01-01T00:00:00.000Z', 1,
                            ],
                            before: 5,
                        };
                        solrData.facetFields.model = [
                            'ford', 23,
                            'chevy', 18,
                            'toyota', 5,
                            'saturn', 7,    // keep here to test the bin/exclude feature
                            'jeep', 19,     // keep here to test the bin/exclude feature
                            'buick', 0,     // keep here to test the bin/exclude feature
                            'dodge', 0,
                        ];
                        solrData.facetFields.price = [
                            '23456', 1,
                            '5678', 2,
                            '1234', 3,
                        ];
                        solrData.facetQueries['model:misc'] = 26;
                        solrData.facetQueries['model:startsWithB'] = 0;
                        solrData.statsFields.year = {min: '2000', max: '2010'};
                        solrData.statsFields.dateOfBirth = {min: '2000', max: '2010'};
                        solrData.statsFields.price = {min: '1234', max: '23456'};
                        f.applyFacetResults(solrResponse);
                        modelFacet = f.get('model');
                        modelItems = modelFacet.get('items');
                        yearFacet = f.get('year');
                        yearItems = yearFacet.get('items');
                        dobFacet = f.get('dateOfBirth');
                        dobItems = dobFacet.get('items');
                        priceFacet = f.get('price');
                        priceItems = priceFacet.get('items');
                    });
                    it('initial', function() {
                        //console.log(JSON.stringify(yearItems));
                        expect(yearItems.length).toBe(5);
                        expectItem(yearItems.at(0), '2000-01-01T00:00:00.000Z', 1, false);
                        expectItem(yearItems.at(1), '2001-01-01T00:00:00.000Z', 4, false);
                        expectItem(yearItems.at(2), '2003-01-01T00:00:00.000Z', 3, false);
                        expectItem(yearItems.at(3), '2008-01-01T00:00:00.000Z', 7, false);
                        expectItem(yearItems.at(4), '2010-01-01T00:00:00.000Z', 1, false);
                        expect(modelItems.length).toBe(4);
                        expectItem(modelItems.at(0), 'chevy', 18, false);
                        expectItem(modelItems.at(1), 'ford', 23, false);
                        expectItem(modelItems.at(2), 'misc', 26, false);
                        expectItem(modelItems.at(3), 'toyota', 5, false);
                        expect(dobItems.length).toBe(4);
                        expectItem(dobItems.at(0), 'before', 5, false);
                        expectItem(dobItems.at(1), '1970-01-01T00:00:00.000Z', 1, false);
                        expectItem(dobItems.at(2), '1972-01-01T00:00:00.000Z', 1, false);
                        expectItem(dobItems.at(3), '1974-01-01T00:00:00.000Z', 1, false);
                        expect(priceItems.length).toBe(3);
                        expectItem(priceItems.at(0), '1234', 3, false);
                        expectItem(priceItems.at(1), '5678', 2, false);
                        expectItem(priceItems.at(2), '23456', 1, false);
                        expect(Backbone.ajax).not.toHaveBeenCalled();
                        expect(onItemChange).not.toHaveBeenCalled();
                    });
                    describe('click', function() {
                        beforeEach(function() {
                            modelItems.get('ford').set('checked', true);
                            modelItems.get('toyota').set('checked', true);
                            yearItems.get('2003-01-01T00:00:00.000Z').set('checked', true);
                            dobItems.get('before').set('checked', true);
                            priceFacet.set('queryMin', 1234);
                            priceFacet.set('queryMax', 6000);
                        });
                        it('result', function() {
                            expect(Backbone.ajax).not.toHaveBeenCalled();
                            expect(onItemChange).toHaveBeenCalled();
                            expect(onItemChange.calls.count()).toEqual(6);
                            console.log(JSON.stringify(onItemChange.calls.allArgs(), null, 1));
                            var facetFormData = f.getFacetFormData();
                            console.log(JSON.stringify(facetFormData, null, 1));
                            expect(facetFormData.facet).toBe(true);
                            expect(facetFormData.stats).toBe(true);
                            expect(facetFormData['facet.missing']).toBe(true);
                            expect(facetFormData.fq).toEqual([
                                "{!tag=model}model_s:(\"ford\" OR \"toyota\")",
                                "{!tag=year}year_d:([2003-01-01T00:00:00.000Z TO 2004-01-01T00:00:00.000Z])",
                                "{!tag=dateOfBirth}dateOfBirth_d:([* TO NOW/YEAR-25YEAR-1SECOND])",
                                "{!tag=price}price_i:([1234 TO 6000])"
                            ]);
                        });
                    });
                });
                describe('suggestions', function() {
                    beforeEach(function() {
                        f.get('model').set('query', 'fo');
                    });
                    it('initial', function() {
                        expect(true).toBe(true);
                        expect(Backbone.ajax).not.toHaveBeenCalled();
                    });
                    describe('call solr', function() {
                        beforeEach(function() {
                            jasmine.clock().tick(300);
                        });
                        it('ajax called with', function() {
                            expect(Backbone.ajax).toHaveBeenCalled();
                            expect(Backbone.ajax.calls.count()).toBe(1);
                            var ajaxData = Backbone.ajax.calls.argsFor(0);
                            //console.log(JSON.stringify(ajaxData, null, 1));
                            expect(ajaxData.length).toBe(1);
                            var arg0 = ajaxData[0];
                            expect(arg0.facetSuggestions).toBe(true);
                            expect(arg0.childFacet).toBe(f.get('model'));
                            var data = arg0.data;
                            expect(data.rows).toBe(0);
                        });
                        describe('result', function() {
                            var wantedSuggestions;
                            beforeEach(function() {
                                var solrResponse = createSolrResponse({stats: true});
                                var solrData = makeSolrResponseMixedCase(solrResponse);
                                //console.log('solrData', JSON.stringify(solrData));
                                solrData.facetFields['suggestions:model'] = [
                                    'toyota', 5,
                                    'ford', 23,
                                    'chevy', 18,
                                    'nissan', 0,
                                ];

                                var ajaxData = Backbone.ajax.calls.argsFor(0);
                                var arg0 = ajaxData[0];
                                arg0.success(solrResponse, arg0);
                                wantedSuggestions = [
                                    {key: 'toyota', value: 5},
                                    {key: 'ford', value: 23},
                                    {key: 'chevy', value: 18},
                                ];
                            });
                            it('applied', function() {
                                expect(f.get('model').get('suggestions')).toEqual(wantedSuggestions);
                            });
                            describe('reset suggestions', function() {
                                beforeEach(function() {
                                    f.get('model').set('query', '');
                                });
                                it('foo', function() {
                                    expect(Backbone.ajax).toHaveBeenCalled();
                                    expect(Backbone.ajax.calls.count()).toBe(1);
                                    expect(f.get('model').get('suggestions')).toEqual(wantedSuggestions);
                                });
                                describe('call solr', function() {
                                    beforeEach(function() {
                                        jasmine.clock().tick(300);
                                    });
                                    it('foo', function() {
                                        expect(Backbone.ajax).toHaveBeenCalled();
                                        expect(Backbone.ajax.calls.count()).toBe(1);
                                        expect(f.get('model').get('suggestions')).toEqual([]);
                                    });
                                });
                            });
                        });

                    });
                });
            });
        });
    });
});