Facet.js 12.4 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);
                });
            }
            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,
                    formatter: function(item) {
                        var value = parseInt(item.get('key'));
                        return '$' + value + ' - $' + (value + 1000);
                    },
                };
                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]);
            });
            describe('type[year; before]', function() {
                var facetOptions = {
                    queryField: 'value_d',
                    facetType: 'year',
                    facetStats: true,
                    other: 'all',
                    formatter: function(item) {
                        var value = parseInt(item.get('key'));
                        return '$' + value + ' - $' + (value + 1000);
                    },
                };
                var pojoItems = [
                    {key: 'before', value: 2000},
                    {key: '2005', value: 500},
                    {key: '2004', value: 400},
                    {key: '2003', value: 300},
                    {key: '2002', value: 200},
                    {key: '2001', value: 100},
                    {key: 'after', value: 1500},
                ];
                ascItemOrder = ['before', '2001', '2002', '2003', '2004', '2005', 'after'];
                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, false, false]);
                it('foo', function() {
                    console.log(JSON.stringify(items.models));
                });
            });
        });
    });
});