8534addb by Adam Heath

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

1 parent 2cc67ace
...@@ -33,6 +33,7 @@ define(function(require) { ...@@ -33,6 +33,7 @@ define(function(require) {
33 var Facet = Backbone.Model.extend({ 33 var Facet = Backbone.Model.extend({
34 defaults: function defaults() { 34 defaults: function defaults() {
35 return { 35 return {
36 checkedKeys: [],
36 formName: null, 37 formName: null,
37 query: null, 38 query: null,
38 suggestions: [], 39 suggestions: [],
...@@ -110,7 +111,7 @@ define(function(require) { ...@@ -110,7 +111,7 @@ define(function(require) {
110 var newChecked = checkedKeys[facetItem.get('key')]; 111 var newChecked = checkedKeys[facetItem.get('key')];
111 var currentChecked = facetItem.get('checked'); 112 var currentChecked = facetItem.get('checked');
112 if (newChecked !== currentChecked) { 113 if (newChecked !== currentChecked) {
113 facetItem.set('checked', newChecked); 114 facetItem.set('checked', !!newChecked);
114 } 115 }
115 if (facetItem.get('checked')) { 116 if (facetItem.get('checked')) {
116 checkedCount++; 117 checkedCount++;
...@@ -128,19 +129,6 @@ define(function(require) { ...@@ -128,19 +129,6 @@ define(function(require) {
128 if (!sortKeyExtractor) { 129 if (!sortKeyExtractor) {
129 sortKeyExtractor = getItemKeyAccessor; 130 sortKeyExtractor = getItemKeyAccessor;
130 } 131 }
131 var comparator = options.comparator;
132 if (!comparator) {
133 comparator = Sort.standard;
134 } else if (typeof comparator === 'string') {
135 comparator = Sort[comparator];
136 }
137 switch (options.facetType) {
138 case 'year':
139 case 'range':
140 case 'interval':
141 comparator = wrapComparatorForAllBeforeAfter(comparator, ['all', 'before'].indexOf(options.other) !== -1, ['all', 'after'].indexOf(options.other) !== -1);
142 break;
143 }
144 var formatter = options.formatter; 132 var formatter = options.formatter;
145 if (!formatter) { 133 if (!formatter) {
146 var labelMap = options.labelMap; 134 var labelMap = options.labelMap;
...@@ -155,21 +143,46 @@ define(function(require) { ...@@ -155,21 +143,46 @@ define(function(require) {
155 } 143 }
156 } 144 }
157 this.formatter = formatter; 145 this.formatter = formatter;
146 this.on('change:comparator change:orderByDirection', function() {
147 var comparator = this.get('comparator');
148 if (!comparator) {
149 comparator = Sort.standard;
150 } else if (typeof comparator === 'string') {
151 comparator = Sort[comparator];
152 }
153 switch (options.facetType) {
154 case 'year':
155 case 'range':
156 case 'interval':
157 comparator = wrapComparatorForAllBeforeAfter(comparator, ['all', 'before'].indexOf(options.other) !== -1, ['all', 'after'].indexOf(options.other) !== -1);
158 break;
159 }
160 var facet = this;
158 comparator = (function(comparator) { 161 comparator = (function(comparator) {
159 return function facetItemValue(a, b) { 162 return function facetItemValue(a, b) {
160 var keyA = sortKeyExtractor(a); 163 var keyA = sortKeyExtractor(a);
161 var keyB = sortKeyExtractor(b); 164 var keyB = sortKeyExtractor(b);
162 switch (this.get('orderByDirection')) { 165 switch (facet.get('orderByDirection')) {
163 case 'desc': 166 case 'desc':
164 var tmp = keyA; 167 var tmp = keyA;
165 keyA = keyB; 168 keyA = keyB;
166 keyB = tmp; 169 keyB = tmp;
167 break; 170 break;
168 } 171 }
169 return comparator.call(this, sortKeyExtractor(a), sortKeyExtractor(b)); 172 return comparator.call(facet, keyA, keyB);
170 }; 173 };
171 })(comparator); 174 })(comparator);
172 this.set('items', new ItemCollection([], {comparator: comparator})); 175 var items = this.get('items');
176 items.comparator = comparator;
177 items.sort();
178 }, this);
179
180 this.set('items', new ItemCollection([]));
181 this.set('comparator', options.comparator, {silent: true});
182 this.trigger('change:comparator');
183 this.get('items').on('item-change', function() {
184 this.trigger('item-change');
185 }, this);
173 this.facetType = options.facetType; 186 this.facetType = options.facetType;
174 this.facetStats = options.facetStats; 187 this.facetStats = options.facetStats;
175 this.other = options.other; 188 this.other = options.other;
...@@ -243,7 +256,7 @@ define(function(require) { ...@@ -243,7 +256,7 @@ define(function(require) {
243 checked: false, 256 checked: false,
244 hidden: false, 257 hidden: false,
245 }, 258 },
246 _initialize: function(data, options) { 259 initialize: function(data, options) {
247 this.on('add', function(model, parent, options) { 260 this.on('add', function(model, parent, options) {
248 // added to a collection 261 // added to a collection
249 this.on('change:checked', function() { 262 this.on('change:checked', function() {
......
1 define(function(require) { 1 define(function(require) {
2 'use strict'; 2 'use strict';
3 var _ = require('underscore');
4 var Backbone = require('backbone');
3 var Facet = require('solr/model/Facet'); 5 var Facet = require('solr/model/Facet');
6 var Sort = require('solr/model/Sort');
4 7
5 describe('Facet', function() { 8 describe('Facet', function() {
6 it('loads', function() { 9 it('loads', function() {
7 expect(Facet).toBeDefined(); 10 expect(Facet).toBeDefined();
8 }); 11 });
12 describe('items', function() {
13 var f, items, ascItemOrder;
14 function createPojoItems(pojoItems) {
15 beforeEach(function() {
16 var newItems = [];
17 for (var i = 0; i < pojoItems.length; i++) {
18 var item = new Facet.Item(pojoItems[i]);
19 newItems.push(item);
20 }
21 items.set(newItems);
22 });
23 }
24 function createExpects(all, orderedItems) {
25 it('all', function() {
26 expect(f.get('all')).toBe(all);
27 });
28
29 var wantedCheckedKeysLength = 0;
30 var wantedCheckedKeys = {};
31 var wantedChecks = {};
32 var wantedItemOrder = [];
33 function scanKey(key, wanted) {
34 wantedChecks[key] = wanted;
35 if (wanted) {
36 wantedCheckedKeys[key] = true;
37 wantedCheckedKeysLength++;
38 }
39 wantedItemOrder.push(key);
40 }
41 for (var i = 0; i < orderedItems.length; i++) {
42 scanKey(ascItemOrder[i], orderedItems[i]);
43 }
44 it('items', function() {
45 var gotChecks = {};
46 var gotOrder = [];
47 items.each(function(item) {
48 var key = item.get('key');
49 gotChecks[key] = item.get('checked');
50 gotOrder.push(key);
51 });
52 expect(gotChecks).toEqual(wantedChecks);
53 expect(gotOrder).toEqual(wantedItemOrder);
54 f.set('orderByDirection', 'desc');
55 wantedItemOrder.reverse();
56 gotOrder = [];
57 items.each(function(item) {
58 var key = item.get('key');
59 gotOrder.push(key);
60 });
61 expect(gotOrder).toEqual(wantedItemOrder);
62 });
63 it('checkedKeys.length', function() {
64 expect(f.get('checkedKeys').length).toBe(wantedCheckedKeysLength);
65 });
66 it('checkedKeys', function() {
67 var got = {};
68 _.each(f.get('checkedKeys'), function(value) {
69 got[value] = true;
70 });
71 expect(got).toEqual(wantedCheckedKeys);
72 });
73 }
74 describe('type[field]', function() {
75 var facetOptions = {
76 queryField: 'value_d',
77 facetType: 'field',
78 facetStats: true,
79 };
80 var pojoItems = [
81 {key: 'e', value: 100},
82 {key: 'd', value: 200},
83 {key: 'c', value: 300},
84 {key: 'b', value: 400},
85 {key: 'a', value: 500},
86 ];
87 ascItemOrder = ['a', 'b', 'c', 'd', 'e'];
88 var c0;
89 beforeEach(function() {
90 f = new Facet(null, facetOptions);
91 items = f.get('items');
92 c0 = f.get('checkedKeys');
93 });
94 createPojoItems(pojoItems);
95 createExpects(true, [false, false, false, false, false]);
96 describe('check c', function() {
97 var c1;
98 beforeEach(function() {
99 items.get('c').set('checked', true);
100 c1 = f.get('checkedKeys');
101 });
102 createExpects(false, [false, false, true, false, false]);
103 it('checkedKeys not same', function() {
104 expect(c1).not.toBe(c0);
105 });
106 describe('check d', function() {
107 var c2;
108 beforeEach(function() {
109 items.get('d').set('checked', true);
110 c2 = f.get('checkedKeys');
111 });
112 createExpects(false, [false, false, true, true, false]);
113 it('checkedKeys not same', function() {
114 expect(c2).not.toBe(c1);
115 expect(c2).not.toBe(c0);
116 });
117 describe('checkedKeys: [a, e]', function() {
118 var c3;
119 beforeEach(function() {
120 f.set('checkedKeys', ['a', 'e']);
121 c3 = f.get('checkedKeys');
122 });
123 createExpects(false, [true, false, false, false, true]);
124 it('checkedKeys not same', function() {
125 expect(c3).not.toBe(c2);
126 expect(c3).not.toBe(c1);
127 expect(c3).not.toBe(c0);
128 });
129 });
130 describe('uncheck c', function() {
131 var c3;
132 beforeEach(function() {
133 items.get('c').set('checked', false);
134 c3 = f.get('checkedKeys');
135 });
136 createExpects(false, [false, false, false, true, false]);
137 it('checkedKeys not same', function() {
138 expect(c3).not.toBe(c2);
139 expect(c3).not.toBe(c1);
140 expect(c3).not.toBe(c0);
141 });
142 });
143 describe('checkedKeys: [d, e]', function() {
144 var c3;
145 beforeEach(function() {
146 f.set('checkedKeys', ['d', 'e']);
147 c3 = f.get('checkedKeys');
148 });
149 createExpects(false, [false, false, false, true, true]);
150 it('checkedKeys not same', function() {
151 expect(c3).not.toBe(c2);
152 expect(c3).not.toBe(c1);
153 expect(c3).not.toBe(c0);
154 });
155 });
156 describe('checked all', function() {
157 var c3;
158 beforeEach(function() {
159 f.set('all', true);
160 c3 = f.get('checkedKeys');
161 });
162 createExpects(true, [false, false, false, false, false]);
163 it('checkedKeys not same', function() {
164 expect(c3).not.toBe(c2);
165 expect(c3).not.toBe(c1);
166 expect(c3).not.toBe(c0);
167 });
168 describe('checkedKeys: [d, e]', function() {
169 var c4;
170 beforeEach(function() {
171 f.set('checkedKeys', ['d', 'e']);
172 c4 = f.get('checkedKeys');
173 });
174 createExpects(false, [false, false, false, true, true]);
175 it('checkedKeys not same', function() {
176 expect(c4).not.toBe(c3);
177 expect(c4).not.toBe(c2);
178 expect(c4).not.toBe(c1);
179 expect(c4).not.toBe(c0);
180 });
181 });
182 });
183 });
184 });
185 });
186 describe('type[year]', function() {
187 var facetOptions = {
188 queryField: 'value_d',
189 facetType: 'year',
190 facetStats: true,
191 formatter: function(item) {
192 var value = parseInt(item.get('key'));
193 return '$' + value + ' - $' + (value + 1000);
194 },
195 };
196 var pojoItems = [
197 {key: '2005', value: 500},
198 {key: '2004', value: 400},
199 {key: '2003', value: 300},
200 {key: '2002', value: 200},
201 {key: '2001', value: 100},
202 ];
203 ascItemOrder = ['2001', '2002', '2003', '2004', '2005'];
204 var c0;
205 beforeEach(function() {
206 f = new Facet(null, facetOptions);
207 items = f.get('items');
208 c0 = f.get('checkedKeys');
209 });
210 createPojoItems(pojoItems);
211 describe('query', function() {
212 describe('has', function() {
213 it('min', function() {
214 f.set({queryMin: 1000, queryMax: undefined});
215 expect(f.get('hasQuery')).toBe(false);
216 });
217 it('max', function() {
218 f.set({queryMin: undefined, queryMax: 5000});
219 expect(f.get('hasQuery')).toBe(false);
220 });
221 it('min+max', function() {
222 f.set({queryMin: 1000, queryMax: 5000});
223 expect(f.get('hasQuery')).toBe(true);
224 });
225 });
226 });
227 createExpects(true, [false, false, false, false, false]);
228 });
229 describe('type[year; before]', function() {
230 var facetOptions = {
231 queryField: 'value_d',
232 facetType: 'year',
233 facetStats: true,
234 other: 'all',
235 formatter: function(item) {
236 var value = parseInt(item.get('key'));
237 return '$' + value + ' - $' + (value + 1000);
238 },
239 };
240 var pojoItems = [
241 {key: 'before', value: 2000},
242 {key: '2005', value: 500},
243 {key: '2004', value: 400},
244 {key: '2003', value: 300},
245 {key: '2002', value: 200},
246 {key: '2001', value: 100},
247 {key: 'after', value: 1500},
248 ];
249 ascItemOrder = ['before', '2001', '2002', '2003', '2004', '2005', 'after'];
250 var c0;
251 beforeEach(function() {
252 f = new Facet(null, facetOptions);
253 items = f.get('items');
254 c0 = f.get('checkedKeys');
255 });
256 createPojoItems(pojoItems);
257 describe('query', function() {
258 describe('has', function() {
259 it('min', function() {
260 f.set({queryMin: 1000, queryMax: undefined});
261 expect(f.get('hasQuery')).toBe(false);
262 });
263 it('max', function() {
264 f.set({queryMin: undefined, queryMax: 5000});
265 expect(f.get('hasQuery')).toBe(false);
266 });
267 it('min+max', function() {
268 f.set({queryMin: 1000, queryMax: 5000});
269 expect(f.get('hasQuery')).toBe(true);
270 });
271 });
272 });
273 createExpects(true, [false, false, false, false, false, false, false]);
274 it('foo', function() {
275 console.log(JSON.stringify(items.models));
276 });
277 });
278 });
9 }); 279 });
10 }); 280 });
......