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

    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');
                onItemChange = jasmine.createSpy('onItemChange');
                f = new Facets(SolrSpecData.facetsBuilder(), {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());
                });

                describe('facets', function() {
                    var modelFacet, modelItems;
                    var yearFacet, yearItems;
                    var dobFacet, dobItems;
                    var valueFacet, valueItems;
                    beforeEach(function() {
                        f.applyFacetResults(SolrSpecData.responses.sequential[0]);
                        modelFacet = f.get('model');
                        modelItems = modelFacet.get('items');
                        yearFacet = f.get('year');
                        yearItems = yearFacet.get('items');
                        dobFacet = f.get('dateOfBirth');
                        dobItems = dobFacet.get('items');
                        valueFacet = f.get('value');
                        valueItems = valueFacet.get('items');
                    });
                    it('initial', function() {
                        //console.log(JSON.stringify(yearItems));
                        expect(yearItems.length).toBe(5);
                        SolrSpecData.expectItem(yearItems.at(0), '2010-01-01T00:00:00.000Z', 1, false);
                        SolrSpecData.expectItem(yearItems.at(1), '2008-01-01T00:00:00.000Z', 7, false);
                        SolrSpecData.expectItem(yearItems.at(2), '2003-01-01T00:00:00.000Z', 3, false);
                        SolrSpecData.expectItem(yearItems.at(3), '2001-01-01T00:00:00.000Z', 4, false);
                        SolrSpecData.expectItem(yearItems.at(4), '2000-01-01T00:00:00.000Z', 1, false);
                        expect(modelItems.length).toBe(4);
                        SolrSpecData.expectItem(modelItems.at(0), 'chevy', 18, false);
                        SolrSpecData.expectItem(modelItems.at(1), 'ford', 23, false);
                        SolrSpecData.expectItem(modelItems.at(2), 'misc', 26, false);
                        SolrSpecData.expectItem(modelItems.at(3), 'toyota', 5, false);
                        expect(dobItems.length).toBe(4);
                        SolrSpecData.expectItem(dobItems.at(0), 'before', 5, false);
                        SolrSpecData.expectItem(dobItems.at(1), '1970-01-01T00:00:00.000Z', 1, false);
                        SolrSpecData.expectItem(dobItems.at(2), '1972-01-01T00:00:00.000Z', 1, false);
                        SolrSpecData.expectItem(dobItems.at(3), '1974-01-01T00:00:00.000Z', 1, false);
                        expect(valueItems.length).toBe(3);
                        SolrSpecData.expectItem(valueItems.at(0), '1234', 3, false);
                        SolrSpecData.expectItem(valueItems.at(1), '5678', 2, false);
                        SolrSpecData.expectItem(valueItems.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);
                            valueFacet.set('queryMin', 1234);
                            valueFacet.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_dt:(2003-01-01T00:00:00.000Z)',
                                '{!tag=dateOfBirth}dateOfBirth_dt:([* TO NOW/YEAR-25YEAR-1SECOND])',
                                '{!tag=value}value_d:([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 ajaxData = Backbone.ajax.calls.argsFor(0);
                                var arg0 = ajaxData[0];
                                arg0.success(SolrSpecData.responses.suggestions[0], 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([]);
                                    });
                                });
                            });
                        });

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