8534addb by Adam Heath

Much more testing of Facet, getting closer to 100%.

1 parent 2cc67ace
......@@ -33,6 +33,7 @@ define(function(require) {
var Facet = Backbone.Model.extend({
defaults: function defaults() {
return {
checkedKeys: [],
formName: null,
query: null,
suggestions: [],
......@@ -110,7 +111,7 @@ define(function(require) {
var newChecked = checkedKeys[facetItem.get('key')];
var currentChecked = facetItem.get('checked');
if (newChecked !== currentChecked) {
facetItem.set('checked', newChecked);
facetItem.set('checked', !!newChecked);
}
if (facetItem.get('checked')) {
checkedCount++;
......@@ -128,19 +129,6 @@ define(function(require) {
if (!sortKeyExtractor) {
sortKeyExtractor = getItemKeyAccessor;
}
var comparator = options.comparator;
if (!comparator) {
comparator = Sort.standard;
} else if (typeof comparator === 'string') {
comparator = Sort[comparator];
}
switch (options.facetType) {
case 'year':
case 'range':
case 'interval':
comparator = wrapComparatorForAllBeforeAfter(comparator, ['all', 'before'].indexOf(options.other) !== -1, ['all', 'after'].indexOf(options.other) !== -1);
break;
}
var formatter = options.formatter;
if (!formatter) {
var labelMap = options.labelMap;
......@@ -155,21 +143,46 @@ define(function(require) {
}
}
this.formatter = formatter;
comparator = (function(comparator) {
return function facetItemValue(a, b) {
var keyA = sortKeyExtractor(a);
var keyB = sortKeyExtractor(b);
switch (this.get('orderByDirection')) {
case 'desc':
var tmp = keyA;
keyA = keyB;
keyB = tmp;
break;
}
return comparator.call(this, sortKeyExtractor(a), sortKeyExtractor(b));
};
})(comparator);
this.set('items', new ItemCollection([], {comparator: comparator}));
this.on('change:comparator change:orderByDirection', function() {
var comparator = this.get('comparator');
if (!comparator) {
comparator = Sort.standard;
} else if (typeof comparator === 'string') {
comparator = Sort[comparator];
}
switch (options.facetType) {
case 'year':
case 'range':
case 'interval':
comparator = wrapComparatorForAllBeforeAfter(comparator, ['all', 'before'].indexOf(options.other) !== -1, ['all', 'after'].indexOf(options.other) !== -1);
break;
}
var facet = this;
comparator = (function(comparator) {
return function facetItemValue(a, b) {
var keyA = sortKeyExtractor(a);
var keyB = sortKeyExtractor(b);
switch (facet.get('orderByDirection')) {
case 'desc':
var tmp = keyA;
keyA = keyB;
keyB = tmp;
break;
}
return comparator.call(facet, keyA, keyB);
};
})(comparator);
var items = this.get('items');
items.comparator = comparator;
items.sort();
}, this);
this.set('items', new ItemCollection([]));
this.set('comparator', options.comparator, {silent: true});
this.trigger('change:comparator');
this.get('items').on('item-change', function() {
this.trigger('item-change');
}, this);
this.facetType = options.facetType;
this.facetStats = options.facetStats;
this.other = options.other;
......@@ -243,7 +256,7 @@ define(function(require) {
checked: false,
hidden: false,
},
_initialize: function(data, options) {
initialize: function(data, options) {
this.on('add', function(model, parent, options) {
// added to a collection
this.on('change:checked', function() {
......
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));
});
});
});
});
});
......