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

    describe('Facet', function() {
        it('loads', function() {
            expect(Facet).toBeDefined();
        });
        describe('items', function() {
            var f, items, ascItemOrder;
            function createPojoItems(pojoItems) {
                beforeEach(function() {
                    var newItems = [];
                    for (var i = 0; i < pojoItems.length; i++) {
                        var item = new Facet.Item(pojoItems[i]);
                        newItems.push(item);
                    }
                    items.set(newItems);
                });
            }
            function createExpects(all, orderedItems) {
                it('all', function() {
                    expect(f.get('all')).toBe(all);
                });

                var wantedCheckedKeysLength = 0;
                var wantedCheckedKeys = {};
                var wantedChecks = {};
                var wantedItemOrder = [];
                function scanKey(key, wanted) {
                    wantedChecks[key] = wanted;
                    if (wanted) {
                        wantedCheckedKeys[key] = true;
                        wantedCheckedKeysLength++;
                    }
                    wantedItemOrder.push(key);
                }
                for (var i = 0; i < orderedItems.length; i++) {
                    scanKey(ascItemOrder[i], orderedItems[i]);
                }
                it('items', function() {
                    var gotChecks = {};
                    var gotOrder = [];
                    items.each(function(item) {
                        var key = item.get('key');
                        gotChecks[key] = item.get('checked');
                        gotOrder.push(key);
                    });
                    expect(gotChecks).toEqual(wantedChecks);
                    expect(gotOrder).toEqual(wantedItemOrder);
                    f.set('orderByDirection', 'desc');
                    wantedItemOrder.reverse();
                    gotOrder = [];
                    items.each(function(item) {
                        var key = item.get('key');
                        gotOrder.push(key);
                    });
                    expect(gotOrder).toEqual(wantedItemOrder);
                });
                it('checkedKeys.length', function() {
                    expect(f.get('checkedKeys').length).toBe(wantedCheckedKeysLength);
                });
                it('checkedKeys', function() {
                    var got = {};
                    _.each(f.get('checkedKeys'), function(value) {
                        got[value] = true;
                    });
                    expect(got).toEqual(wantedCheckedKeys);
                });
                var item2 = ascItemOrder[2];
                it('remove not supported', function() {
                    expect(items.length).toBe(wantedItemOrder.length);
                    items.remove(item2);
                    expect(items.length).toBe(wantedItemOrder.length);
                });
            }
            describe('type[field]', function() {
                var facetOptions = {
                    queryField: 'value_d',
                    facetType: 'field',
                    facetStats: true,
                };
                var pojoItems = [
                    {key: 'e', value: 100},
                    {key: 'd', value: 200},
                    {key: 'c', value: 300},
                    {key: 'b', value: 400},
                    {key: 'a', value: 500},
                ];
                ascItemOrder = ['a', 'b', 'c', 'd', 'e'];
                var c0;
                beforeEach(function() {
                    f = new Facet(null, facetOptions);
                    items = f.get('items');
                    c0 = f.get('checkedKeys');
                });
                createPojoItems(pojoItems);
                createExpects(true, [false, false, false, false, false]);
                describe('check c', function() {
                    var c1;
                    beforeEach(function() {
                        items.get('c').set('checked', true);
                        c1 = f.get('checkedKeys');
                    });
                    createExpects(false, [false, false, true, false, false]);
                    it('checkedKeys not same', function() {
                        expect(c1).not.toBe(c0);
                    });
                    describe('check d', function() {
                        var c2;
                        beforeEach(function() {
                            items.get('d').set('checked', true);
                            c2 = f.get('checkedKeys');
                        });
                        createExpects(false, [false, false, true, true, false]);
                        it('checkedKeys not same', function() {
                            expect(c2).not.toBe(c1);
                            expect(c2).not.toBe(c0);
                        });
                        describe('checkedKeys: [a, e]', function() {
                            var c3;
                            beforeEach(function() {
                                f.set('checkedKeys', ['a', 'e']);
                                c3 = f.get('checkedKeys');
                            });
                            createExpects(false, [true, false, false, false, true]);
                            it('checkedKeys not same', function() {
                                expect(c3).not.toBe(c2);
                                expect(c3).not.toBe(c1);
                                expect(c3).not.toBe(c0);
                            });
                        });
                        describe('uncheck c', function() {
                            var c3;
                            beforeEach(function() {
                                items.get('c').set('checked', false);
                                c3 = f.get('checkedKeys');
                            });
                            createExpects(false, [false, false, false, true, false]);
                            it('checkedKeys not same', function() {
                                expect(c3).not.toBe(c2);
                                expect(c3).not.toBe(c1);
                                expect(c3).not.toBe(c0);
                            });
                        });
                        describe('checkedKeys: [d, e]', function() {
                            var c3;
                            beforeEach(function() {
                                f.set('checkedKeys', ['d', 'e']);
                                c3 = f.get('checkedKeys');
                            });
                            createExpects(false, [false, false, false, true, true]);
                            it('checkedKeys not same', function() {
                                expect(c3).not.toBe(c2);
                                expect(c3).not.toBe(c1);
                                expect(c3).not.toBe(c0);
                            });
                        });
                        describe('checked all', function() {
                            var c3;
                            beforeEach(function() {
                                f.set('all', true);
                                c3 = f.get('checkedKeys');
                            });
                            createExpects(true, [false, false, false, false, false]);
                            it('checkedKeys not same', function() {
                                expect(c3).not.toBe(c2);
                                expect(c3).not.toBe(c1);
                                expect(c3).not.toBe(c0);
                            });
                            describe('checkedKeys: [d, e]', function() {
                                var c4;
                                beforeEach(function() {
                                    f.set('checkedKeys', ['d', 'e']);
                                    c4 = f.get('checkedKeys');
                                });
                                createExpects(false, [false, false, false, true, true]);
                                it('checkedKeys not same', function() {
                                    expect(c4).not.toBe(c3);
                                    expect(c4).not.toBe(c2);
                                    expect(c4).not.toBe(c1);
                                    expect(c4).not.toBe(c0);
                                });
                            });
                        });
                    });
                });
            });
            describe('type[year]', function() {
                var facetOptions = {
                    queryField: 'value_d',
                    facetType: 'year',
                    facetStats: true,
                };
                var pojoItems = [
                    {key: '2005', value: 500},
                    {key: '2004', value: 400},
                    {key: '2003', value: 300},
                    {key: '2002', value: 200},
                    {key: '2001', value: 100},
                ];
                ascItemOrder = ['2001', '2002', '2003', '2004', '2005'];
                var c0;
                beforeEach(function() {
                    f = new Facet(null, facetOptions);
                    items = f.get('items');
                    c0 = f.get('checkedKeys');
                });
                createPojoItems(pojoItems);
                describe('query', function() {
                    describe('has', function() {
                        it('min', function() {
                            f.set({queryMin: 1000, queryMax: undefined});
                            expect(f.get('hasQuery')).toBe(false);
                        });
                        it('max', function() {
                            f.set({queryMin: undefined, queryMax: 5000});
                            expect(f.get('hasQuery')).toBe(false);
                        });
                        it('min+max', function() {
                            f.set({queryMin: 1000, queryMax: 5000});
                            expect(f.get('hasQuery')).toBe(true);
                        });
                    });
                });
                createExpects(true, [false, false, false, false, false]);
            });
            function rangeSupportHelper(hasBefore, hasAfter) {
                function createBeforeAfterExpects(all, orderedItems) {
                    if (!hasBefore) {
                        orderedItems.shift();
                    }
                    if (!hasAfter) {
                        orderedItems.pop();
                    }
                    createExpects(all, orderedItems);
                }
                describe('type[year]' + (hasBefore ? ' hasBefore' : '') + (hasAfter ? ' hasAfter' : ''), function() {
                    var facetOptions = {
                        queryField: 'value_d',
                        facetType: 'year',
                        facetStats: true,
                    };
                    if (hasBefore) {
                        if (hasAfter) {
                            facetOptions.other = 'all';
                        } else {
                            facetOptions.other = 'before';
                        }
                    } else if (hasAfter) {
                        facetOptions.other = 'after';
                    }
                    var pojoItems = [
                        {key: '2005', value: 500},
                        {key: '2004', value: 400},
                        {key: '2003', value: 300},
                        {key: '2002', value: 200},
                        {key: '2001', value: 100},
                    ];
                    var beforeItem = {key: 'before', value: 2000};
                    var afterItem = {key: 'after', value: 1500};
                    ascItemOrder = ['2001', '2002', '2003', '2004', '2005'];
                    var c0;
                    beforeEach(function() {
                        f = new Facet(null, facetOptions);
                        items = f.get('items');
                        c0 = f.get('checkedKeys');
                    });
                    if (hasAfter) {
                        ascItemOrder.push('after');
                        pojoItems.push(afterItem);
                    }
                    if (hasBefore) {
                        ascItemOrder.unshift('before');
                        pojoItems.unshift(beforeItem);
                    }
                    createPojoItems(pojoItems);
                    describe('query', function() {
                        describe('min', function() {
                            beforeEach(function() {
                                f.set({queryMin: '2002', queryMax: undefined});
                            });
                            createBeforeAfterExpects(true, [false, false, false, false, false, false, false]);
                            it('result', function() {
                                expect(f.get('hasQuery')).toBe(false);
                            });
                        });
                        describe('max', function() {
                            beforeEach(function() {
                                f.set({queryMin: undefined, queryMax: '2004'});
                            });
                            createBeforeAfterExpects(true, [false, false, false, false, false, false, false]);
                            it('result', function() {
                                expect(f.get('hasQuery')).toBe(false);
                            });
                        });
                        describe('min+max', function() {
                            beforeEach(function() {
                                f.set({queryMin: '1980', queryMax: '1984'});
                            });
                            createBeforeAfterExpects(false, [false, false, false, false, false, false, false]);
                            it('result', function() {
                                expect(f.get('hasQuery')).toBe(true);
                            });
                            describe('checkedKeys: [2002, 2003]', function() {
                                beforeEach(function() {
                                    f.set('checkedKeys', ['2002', '2003']);
                                });
                                createBeforeAfterExpects(false, [false, false, true, true, false, false, false]);
                            });
                        });
                    });
                    createBeforeAfterExpects(true, [false, false, false, false, false, false, false]);
                });
            }
            rangeSupportHelper(true, true);
            rangeSupportHelper(true, false);
            rangeSupportHelper(false, false);
            rangeSupportHelper(false, true);
        });
    });
    return {};
});