628b2310 by Adam Heath

Major rework, uses a builder pattern now.

1 parent 2b235279
...@@ -61,7 +61,6 @@ module.exports = function(grunt) { ...@@ -61,7 +61,6 @@ module.exports = function(grunt) {
61 var _ = require('lodash'); 61 var _ = require('lodash');
62 62
63 function genJasmineConfig(withCoverage, options) { 63 function genJasmineConfig(withCoverage, options) {
64 var template, templateOptions;
65 var src = [ 64 var src = [
66 'src/scripts/{,**/}*.js', 65 'src/scripts/{,**/}*.js',
67 '!src/scripts/main.js', 66 '!src/scripts/main.js',
...@@ -103,7 +102,7 @@ module.exports = function(grunt) { ...@@ -103,7 +102,7 @@ module.exports = function(grunt) {
103 } 102 }
104 } 103 }
105 url = parts.join('/'); 104 url = parts.join('/');
106 if (url.match(/^src\/scripts\/(.*?\/)?[^\/]+\.spec\.js$/)) { 105 if (0) if (url.match(/^src\/scripts\/(.*?\/)?[^\/]+\.spec\.js$/)) {
107 url = './.grunt/grunt-contrib-jasmine/' + url; 106 url = './.grunt/grunt-contrib-jasmine/' + url;
108 } else if (url.match(/^src\/scripts\/(.*?\/)?[^\/]+\.js$/)) { 107 } else if (url.match(/^src\/scripts\/(.*?\/)?[^\/]+\.js$/)) {
109 url = './.grunt/grunt-contrib-jasmine/' + url; 108 url = './.grunt/grunt-contrib-jasmine/' + url;
...@@ -149,7 +148,7 @@ module.exports = function(grunt) { ...@@ -149,7 +148,7 @@ module.exports = function(grunt) {
149 template: template, 148 template: template,
150 templateOptions: templateOptions, 149 templateOptions: templateOptions,
151 }, 150 },
152 } 151 };
153 return jasmineConfig; 152 return jasmineConfig;
154 } 153 }
155 154
......
...@@ -4,43 +4,124 @@ define(function(require) { ...@@ -4,43 +4,124 @@ define(function(require) {
4 var Backbone = require('backbone'); 4 var Backbone = require('backbone');
5 var Facet = require('solr/model/Facet'); 5 var Facet = require('solr/model/Facet');
6 var Sort = require('solr/model/Sort'); 6 var Sort = require('solr/model/Sort');
7 var SolrSpecData = require('solr/model/Solr.spec.data');
7 8
8 describe('Facet', function() { 9 var BaseHandler = Backbone.Model.extend({
9 it('loads', function() { 10 constructor: function constructor(parent, data) {
10 expect(Facet).toBeDefined(); 11 Backbone.Model.call(this, data);
11 }); 12 this.parent = parent;
12 describe('items', function() { 13 this.builders = [];
13 var f, items, ascItemOrder; 14 this.addBuilder(function(builder) {
14 function createPojoItems(pojoItems) {
15 beforeEach(function() { 15 beforeEach(function() {
16 var newItems = []; 16 builder.clear();
17 for (var i = 0; i < pojoItems.length; i++) { 17 });
18 var item = new Facet.Item(pojoItems[i]); 18 });
19 newItems.push(item); 19 },
20 } 20 build: function build() {
21 items.set(newItems); 21 _.each(this.builders, function(builder) {
22 builder(this);
23 }, this);
24 },
25 addBuilder: function addBuilder(builder) {
26 this.builders.push(builder);
27 return builder;
28 },
29 next: function next() {
30 var next = this.createNextHandler();
31 this.addBuilder(function() {
32 next.build();
33 });
34 return next;
35 },
36 end: function end() {
37 return this.parent;
38 },
39 endAll: function endAll() {
40 var ptr = this;
41 var end = ptr.end();
42 while (end) {
43 ptr = end;
44 end = ptr.end();
45 }
46 return ptr;
47 },
48 });
49 var BaseBuilder = BaseHandler.extend({
50 type: 'BaseBuilder',
51 constructor: function constructor(parent, data) {
52 BaseHandler.call(this, parent, data);
53 },
54 build: function build() {
55 var self = this;
56 var description = this.description();
57 if (description) {
58 describe(this.description(), function() {
59 BaseBuilder.__super__.build.call(self, build);
22 }); 60 });
61 } else {
62 BaseBuilder.__super__.build.call(self, build);
23 } 63 }
24 function createExpects(all, orderedItems) { 64 },
65 });
66 var FacetBaseBuilder = BaseBuilder.extend({
67 type: 'FacetBaseBuilder',
68 constructor: function constructor(parent, data) {
69 BaseBuilder.call(
70 this,
71 parent,
72 data
73 );
74 },
75 description: function() {
76 return null;
77 },
78 facetBuilder: function facetBuilder() {
79 return this.parent.facetBuilder();
80 },
81 createNextHandler: function createNextHandler() {
82 return new FacetBaseBuilder(this);
83 },
84 createExpects: function createExpects(all, orderedItems, optionalHasQuery) {
85 var wantedCheckedKeysLength = 0;
86 var wantedCheckedKeys = {};
87 var wantedChecks = {};
88 var wantedItemOrder = [];
89 optionalHasQuery = optionalHasQuery || {};
90 function scanKey(key, wanted) {
91 wantedChecks[key] = wanted;
92 if (wanted) {
93 wantedCheckedKeys[key] = true;
94 wantedCheckedKeysLength++;
95 }
96 wantedItemOrder.push(key);
97 }
98 var facetBuilder = this.facetBuilder();
99 var ascItemOrder = _.clone(facetBuilder.ascItemOrder);
100 if (facetBuilder.hasAfter) {
101 ascItemOrder.push('after');
102 } else if (facetBuilder.hasAfter !== undefined) {
103 orderedItems.pop();
104 }
105 if (facetBuilder.hasBefore) {
106 ascItemOrder.unshift('before');
107 } else if (facetBuilder.hasBefore !== undefined) {
108 orderedItems.shift();
109 }
110 for (var i = 0; i < orderedItems.length; i++) {
111 scanKey(ascItemOrder[i], orderedItems[i]);
112 }
113 var item2 = ascItemOrder[2];
114 this.addBuilder(function(builder) {
115 var f, items;
116 beforeEach(function() {
117 var facetBuilder = builder.facetBuilder();
118 f = facetBuilder.get('f');
119 items = facetBuilder.get('items');
120 });
25 it('all', function() { 121 it('all', function() {
26 expect(f.get('all')).toBe(all); 122 expect(f.get('all')).toBe(all);
27 }); 123 });
28 124
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() { 125 it('items', function() {
45 var gotChecks = {}; 126 var gotChecks = {};
46 var gotOrder = []; 127 var gotOrder = [];
...@@ -70,257 +151,331 @@ define(function(require) { ...@@ -70,257 +151,331 @@ define(function(require) {
70 }); 151 });
71 expect(got).toEqual(wantedCheckedKeys); 152 expect(got).toEqual(wantedCheckedKeys);
72 }); 153 });
73 var item2 = ascItemOrder[2];
74 it('remove not supported', function() { 154 it('remove not supported', function() {
75 expect(items.length).toBe(wantedItemOrder.length); 155 expect(items.length).toBe(wantedItemOrder.length);
76 items.remove(item2); 156 items.remove(item2);
77 expect(items.length).toBe(wantedItemOrder.length); 157 expect(items.length).toBe(wantedItemOrder.length);
78 }); 158 });
79 } 159 if (optionalHasQuery.min) {
80 describe('type[field]', function() { 160 if (optionalHasQuery.max) {
81 var facetOptions = { 161 it('has query:min+max', function() {
82 queryField: 'value_d', 162 expect(f.get('hasQuery')).toBe(true);
83 facetType: 'field', 163 expect(f.get('all')).toBe(false);
84 facetStats: true,
85 };
86 var pojoItems = [
87 {key: 'e', value: 100},
88 {key: 'd', value: 200},
89 {key: 'c', value: 300},
90 {key: 'b', value: 400},
91 {key: 'a', value: 500},
92 ];
93 ascItemOrder = ['a', 'b', 'c', 'd', 'e'];
94 var c0;
95 beforeEach(function() {
96 f = new Facet(null, facetOptions);
97 items = f.get('items');
98 c0 = f.get('checkedKeys');
99 });
100 createPojoItems(pojoItems);
101 createExpects(true, [false, false, false, false, false]);
102 describe('check c', function() {
103 var c1;
104 beforeEach(function() {
105 items.get('c').set('checked', true);
106 c1 = f.get('checkedKeys');
107 });
108 createExpects(false, [false, false, true, false, false]);
109 it('checkedKeys not same', function() {
110 expect(c1).not.toBe(c0);
111 });
112 describe('check d', function() {
113 var c2;
114 beforeEach(function() {
115 items.get('d').set('checked', true);
116 c2 = f.get('checkedKeys');
117 });
118 createExpects(false, [false, false, true, true, false]);
119 it('checkedKeys not same', function() {
120 expect(c2).not.toBe(c1);
121 expect(c2).not.toBe(c0);
122 });
123 describe('checkedKeys: [a, e]', function() {
124 var c3;
125 beforeEach(function() {
126 f.set('checkedKeys', ['a', 'e']);
127 c3 = f.get('checkedKeys');
128 });
129 createExpects(false, [true, false, false, false, true]);
130 it('checkedKeys not same', function() {
131 expect(c3).not.toBe(c2);
132 expect(c3).not.toBe(c1);
133 expect(c3).not.toBe(c0);
134 });
135 });
136 describe('uncheck c', function() {
137 var c3;
138 beforeEach(function() {
139 items.get('c').set('checked', false);
140 c3 = f.get('checkedKeys');
141 });
142 createExpects(false, [false, false, false, true, false]);
143 it('checkedKeys not same', function() {
144 expect(c3).not.toBe(c2);
145 expect(c3).not.toBe(c1);
146 expect(c3).not.toBe(c0);
147 });
148 });
149 describe('checkedKeys: [d, e]', function() {
150 var c3;
151 beforeEach(function() {
152 f.set('checkedKeys', ['d', 'e']);
153 c3 = f.get('checkedKeys');
154 });
155 createExpects(false, [false, false, false, true, true]);
156 it('checkedKeys not same', function() {
157 expect(c3).not.toBe(c2);
158 expect(c3).not.toBe(c1);
159 expect(c3).not.toBe(c0);
160 });
161 }); 164 });
162 describe('checked all', function() { 165 } else {
163 var c3; 166 it('has query:min', function() {
164 beforeEach(function() { 167 expect(f.get('hasQuery')).toBe(false);
165 f.set('all', true);
166 c3 = f.get('checkedKeys');
167 });
168 createExpects(true, [false, false, false, false, false]);
169 it('checkedKeys not same', function() {
170 expect(c3).not.toBe(c2);
171 expect(c3).not.toBe(c1);
172 expect(c3).not.toBe(c0);
173 });
174 describe('checkedKeys: [d, e]', function() {
175 var c4;
176 beforeEach(function() {
177 f.set('checkedKeys', ['d', 'e']);
178 c4 = f.get('checkedKeys');
179 });
180 createExpects(false, [false, false, false, true, true]);
181 it('checkedKeys not same', function() {
182 expect(c4).not.toBe(c3);
183 expect(c4).not.toBe(c2);
184 expect(c4).not.toBe(c1);
185 expect(c4).not.toBe(c0);
186 });
187 });
188 }); 168 });
169 }
170 } else if (optionalHasQuery.max) {
171 it('has query:max', function() {
172 expect(f.get('hasQuery')).toBe(false);
189 }); 173 });
174 }
175 });
176 return this;
177 },
178 setChecked: function setChecked(itemName, checkedValue) {
179 var next = new FacetSetCheckedBuilder(this, itemName, checkedValue);
180 this.addBuilder(function() {
181 next.build();
182 });
183 return next;
184 },
185 });
186 var FacetSetCheckedBuilder = FacetBaseBuilder.extend({
187 type: 'FacetSetCheckedBuilder',
188 constructor: function constructor(parent, itemName, checkedValue) {
189 FacetBaseBuilder.call(
190 this,
191 parent,
192 {}
193 );
194 this.itemName = itemName;
195 this.checkedValue = checkedValue;
196 this.addBuilder(function(builder) {
197 var checkedKeysList;
198 beforeEach(function() {
199 var f = builder.facetBuilder().get('f');
200 var items = f.get('items');
201 checkedKeysList = builder.facetBuilder().get('checkedKeysList');
202 switch (builder.itemName) {
203 case 'all':
204 case 'checkedKeys':
205 case 'queryMin':
206 case 'queryMax':
207 f.set(builder.itemName, builder.checkedValue);
208 break;
209 default:
210 items.get(builder.itemName).set('checked', builder.checkedValue);
211 }
212 checkedKeysList.push(f.get('checkedKeys'));
213 });
214 it('checkedKeys not same', function() {
215 for (var i = 0; i < checkedKeysList.length; i++) {
216 for (var j = 0; j < checkedKeysList.length; j++) {
217 if (i != j) {
218 expect(checkedKeysList[i]).not.toBe(checkedKeysList[j]);
219 }
220 }
221 }
190 }); 222 });
191 }); 223 });
192 describe('type[year]', function() { 224 },
193 var facetOptions = { 225 description: function description() {
194 queryField: 'value_d', 226 return 'checked[' + this.itemName + ']=' + this.checkedValue;
195 facetType: 'year', 227 },
196 facetStats: true, 228 });
197 }; 229 var FacetBuilder = FacetBaseBuilder.extend({
198 var pojoItems = [ 230 type: 'FacetBuilder',
199 {key: '2005', value: 500}, 231 constructor: function constructor(nameOrBuilder, description) {
200 {key: '2004', value: 400}, 232 FacetBaseBuilder.call(
201 {key: '2003', value: 300}, 233 this,
202 {key: '2002', value: 200}, 234 null,
203 {key: '2001', value: 100}, 235 {}
204 ]; 236 );
205 ascItemOrder = ['2001', '2002', '2003', '2004', '2005']; 237 if (description) {
206 var c0; 238 description = 'facet[:' + description + ']';
239 } else {
240 description = 'facet[' + nameOrBuilder + ']';
241 }
242 this.description = function () {
243 return description;
244 };
245 this.nameOrBuilder = nameOrBuilder;
246 this.addBuilder(function(builder) {
247 var f;
207 beforeEach(function() { 248 beforeEach(function() {
208 f = new Facet(null, facetOptions); 249 if (typeof builder.nameOrBuilder === 'string') {
209 items = f.get('items'); 250 f = SolrSpecData.facetsBuilder(builder.nameOrBuilder);
210 c0 = f.get('checkedKeys'); 251 } else {
252 f = builder.nameOrBuilder();
253 }
254 builder.set('f', f);
255 builder.set('items', f.get('items'));
256 builder.set('checkedKeysList', [f.get('checkedKeys')]);
211 }); 257 });
212 createPojoItems(pojoItems); 258 it('exists[' + name + ']', function() {
213 describe('query', function() { 259 expect(f).not.toBe(undefined);
214 describe('has', function() { 260 expect(f).not.toBe(null);
215 it('min', function() {
216 f.set({queryMin: 1000, queryMax: undefined});
217 expect(f.get('hasQuery')).toBe(false);
218 });
219 it('max', function() {
220 f.set({queryMin: undefined, queryMax: 5000});
221 expect(f.get('hasQuery')).toBe(false);
222 });
223 it('min+max', function() {
224 f.set({queryMin: 1000, queryMax: 5000});
225 expect(f.get('hasQuery')).toBe(true);
226 });
227 });
228 }); 261 });
229 createExpects(true, [false, false, false, false, false]);
230 }); 262 });
231 function rangeSupportHelper(hasBefore, hasAfter) { 263 },
232 function createBeforeAfterExpects(all, orderedItems) { 264 facetBuilder: function facetBuilder() {
233 if (!hasBefore) { 265 return this;
234 orderedItems.shift(); 266 },
267 setAscItemOrder: function setAscItemOrder(ascItemOrder) {
268 this.ascItemOrder = ascItemOrder;
269 return this;
270 },
271 setBeforeAfter: function setBeforeAfter(hasBefore, hasAfter) {
272 this.hasBefore = hasBefore;
273 this.hasAfter = hasAfter;
274 return this;
275 },
276 createPojoItems: function createPojoItems(pojoItems) {
277 this.addBuilder(function(builder) {
278 beforeEach(function() {
279 var newItems = [];
280 if (builder.hasBefore) {
281 newItems.push(new Facet.Item(builder.hasBefore));
235 } 282 }
236 if (!hasAfter) { 283 for (var i = 0; i < pojoItems.length; i++) {
237 orderedItems.pop(); 284 newItems.push(new Facet.Item(pojoItems[i]));
238 } 285 }
239 createExpects(all, orderedItems); 286 if (builder.hasAfter) {
240 } 287 newItems.push(new Facet.Item(builder.hasAfter));
241 describe('type[year]' + (hasBefore ? ' hasBefore' : '') + (hasAfter ? ' hasAfter' : ''), function() {
242 var facetOptions = {
243 queryField: 'value_d',
244 facetType: 'year',
245 facetStats: true,
246 };
247 if (hasBefore) {
248 if (hasAfter) {
249 facetOptions.other = 'all';
250 } else {
251 facetOptions.other = 'before';
252 }
253 } else if (hasAfter) {
254 facetOptions.other = 'after';
255 } 288 }
256 var pojoItems = [ 289 builder.get('items').set(newItems);
257 {key: '2005', value: 500}, 290 });
258 {key: '2004', value: 400}, 291 });
259 {key: '2003', value: 300}, 292 return this;
260 {key: '2002', value: 200}, 293 },
261 {key: '2001', value: 100}, 294 });
262 ]; 295 describe('Facet', function() {
263 var beforeItem = {key: 'before', value: 2000}; 296 it('loads', function() {
264 var afterItem = {key: 'after', value: 1500}; 297 expect(Facet).toBeDefined();
265 ascItemOrder = ['2001', '2002', '2003', '2004', '2005']; 298 });
266 var c0; 299 function setFacet(nameOrBuilder, description) {
267 beforeEach(function() { 300 return new FacetBuilder(nameOrBuilder, description);
268 f = new Facet(null, facetOptions); 301 }
269 items = f.get('items'); 302
270 c0 = f.get('checkedKeys'); 303 setFacet('model')
271 }); 304 .createPojoItems([
305 {key: 'toyota', value: 5},
306 {key: 'misc', value: 26},
307 {key: 'ford', value: 23},
308 {key: 'chevy', value: 18},
309 ]).setAscItemOrder(['chevy', 'ford', 'misc', 'toyota'])
310 .createExpects(true, [false, false, false, false])
311 .setChecked('chevy', true)
312 .createExpects(false, [true, false, false, false])
313 .setChecked('misc', true)
314 .createExpects(false, [true, false, true, false])
315 .setChecked('checkedKeys', ['chevy', 'ford'])
316 .createExpects(false, [true, true, false, false])
317 .end()
318 .setChecked('checkedKeys', ['ford', 'toyota'])
319 .createExpects(false, [false, true, false, true])
320 .end()
321 .setChecked('chevy', false)
322 .createExpects(false, [false, false, true, false])
323 .setChecked('misc', false)
324 .createExpects(true, [false, false, false, false])
325 .end()
326 .end()
327 .end()
328 .setChecked('all', true)
329 .createExpects(true, [false, false, false, false])
330 .setChecked('checkedKeys', ['ford', 'toyota'])
331 .createExpects(false, [false, true, false, true])
332 .endAll()
333 .build();
334 setFacet('value')
335 .createPojoItems([
336 {key: '7890', value: 7},
337 {key: '6789', value: 6},
338 {key: '5678', value: 5},
339 {key: '4567', value: 4},
340 {key: '3456', value: 3},
341 {key: '2345', value: 2},
342 {key: '1234', value: 1},
343 ])
344 .setAscItemOrder(['1234', '2345', '3456', '4567', '5678', '6789', '7890'])
345 .setChecked('5678', true)
346 .createExpects(false, [false, false, false, false, true, false, false])
347 .setChecked('queryMin', '1234')
348 .createExpects(false, [false, false, false, false, true, false, false], {min: true, max: false})
349 .setChecked('queryMax', '7890')
350 .createExpects(false, [false, false, false, false, true, false, false], {min: true, max: true})
351 .setChecked('checkedKeys', ['5678', '6789'])
352 .createExpects(false, [false, false, false, false, true, true, false], {min: true, max: true})
353 .end()
354 .setChecked('all', true)
355 .createExpects(true, [false, false, false, false, false, false, false], {min: false, max: false})
356 .setChecked('checkedKeys', ['2345', '6789'])
357 .createExpects(false, [false, true, false, false, false, true, false], {min: false, max: false})
358 .end()
359 .end()
360 .setChecked('checkedKeys', [])
361 .createExpects(false, [false, false, false, false, false, false, false], {min: true, max: true})
362 .setChecked('queryMin', undefined)
363 .createExpects(true, [false, false, false, false, false, false, false], {min: false, max: true})
364 .setChecked('queryMax', undefined)
365 .createExpects(true, [false, false, false, false, false, false, false], {min: false, max: false})
366 .endAll()
367 .build();
368
369 setFacet('year')
370 .createPojoItems([
371 {key: '2010-01-01T00:00:00.000Z', value: 1},
372 {key: '2009-01-01T00:00:00.000Z', value: 0},
373 {key: '2008-01-01T00:00:00.000Z', value: 7},
374 {key: '2007-01-01T00:00:00.000Z', value: 0},
375 {key: '2006-01-01T00:00:00.000Z', value: 0},
376
377 {key: '2005-01-01T00:00:00.000Z', value: 0},
378
379 {key: '2004-01-01T00:00:00.000Z', value: 0},
380 {key: '2003-01-01T00:00:00.000Z', value: 3},
381 {key: '2002-01-01T00:00:00.000Z', value: 0},
382 {key: '2001-01-01T00:00:00.000Z', value: 4},
383 {key: '2000-01-01T00:00:00.000Z', value: 1},
384 ])
385 .setAscItemOrder([
386 '2000-01-01T00:00:00.000Z',
387 '2001-01-01T00:00:00.000Z',
388 '2003-01-01T00:00:00.000Z',
389 '2008-01-01T00:00:00.000Z',
390 '2010-01-01T00:00:00.000Z',
391 ])
392 .setChecked('2003-01-01T00:00:00.000Z', true)
393 .endAll()
394 .build();
395 setFacet('dateOfBirth')
396 .createPojoItems([
397 {key: '1974-01-01T00:00:00.000Z', value: 1},
398 {key: '1972-01-01T00:00:00.000Z', value: 1},
399 {key: '1970-01-01T00:00:00.000Z', value: 1},
400 ])
401 .setAscItemOrder([
402 '1970-01-01T00:00:00.000Z',
403 '1972-01-01T00:00:00.000Z',
404 '1974-01-01T00:00:00.000Z',
405 ])
406 .setBeforeAfter({key: 'before', value: 5}, undefined)
407 .setChecked('1972-01-01T00:00:00.000Z', true)
408 .createExpects(false, [false, false, true, false])
409 .setChecked('before', true)
410 .createExpects(false, [true, false, true, false])
411 .setChecked('checkedKeys', ['before', '1974-01-01T00:00:00.000Z'])
412 .createExpects(false, [true, false, false, true])
413 .setChecked('checkedKeys', [])
414 .createExpects(true, [false, false, false, false])
415 .end()
416 .end()
417
418 .endAll()
419 .build();
420
421
422 function rangeSupportHelper(hasBefore, hasAfter) {
423 var description = 'type[year]' + (hasBefore ? ' hasBefore' : '') + (hasAfter ? ' hasAfter' : '');
424 var builder = setFacet(function() {
425 var facetOptions = {
426 queryField: 'dateOfBirth_dt',
427 facetType: 'year',
428 facetStats: true,
429 };
430 if (hasBefore) {
272 if (hasAfter) { 431 if (hasAfter) {
273 ascItemOrder.push('after'); 432 facetOptions.other = 'all';
274 pojoItems.push(afterItem); 433 } else {
275 } 434 facetOptions.other = 'before';
276 if (hasBefore) {
277 ascItemOrder.unshift('before');
278 pojoItems.unshift(beforeItem);
279 } 435 }
280 createPojoItems(pojoItems); 436 } else if (hasAfter) {
281 describe('query', function() { 437 facetOptions.other = 'after';
282 describe('min', function() { 438 }
283 beforeEach(function() { 439 return new Facet(null, facetOptions);
284 f.set({queryMin: '2002', queryMax: undefined}); 440 }, description)
285 }); 441 .createPojoItems([
286 createBeforeAfterExpects(true, [false, false, false, false, false, false, false]); 442 {key: '1974-01-01T00:00:00.000Z', value: 1},
287 it('result', function() { 443 {key: '1972-01-01T00:00:00.000Z', value: 1},
288 expect(f.get('hasQuery')).toBe(false); 444 {key: '1970-01-01T00:00:00.000Z', value: 1},
289 }); 445 ])
290 }); 446 .setAscItemOrder([
291 describe('max', function() { 447 '1970-01-01T00:00:00.000Z',
292 beforeEach(function() { 448 '1972-01-01T00:00:00.000Z',
293 f.set({queryMin: undefined, queryMax: '2004'}); 449 '1974-01-01T00:00:00.000Z',
294 }); 450 ])
295 createBeforeAfterExpects(true, [false, false, false, false, false, false, false]); 451 .setBeforeAfter(hasBefore, hasAfter)
296 it('result', function() { 452 .setChecked('1972-01-01T00:00:00.000Z', true)
297 expect(f.get('hasQuery')).toBe(false); 453 .createExpects(false, [false, false, true, false, false])
298 }); 454 ;
299 }); 455 if (hasBefore) {
300 describe('min+max', function() { 456 builder.setChecked('before', true)
301 beforeEach(function() { 457 .createExpects(false, [true, false, true, false, false])
302 f.set({queryMin: '1980', queryMax: '1984'}); 458 .setChecked('checkedKeys', ['before', '1974-01-01T00:00:00.000Z'])
303 }); 459 .createExpects(false, [true, false, false, true, false])
304 createBeforeAfterExpects(false, [false, false, false, false, false, false, false]); 460 .setChecked('checkedKeys', [])
305 it('result', function() { 461 .createExpects(true, [false, false, false, false, false])
306 expect(f.get('hasQuery')).toBe(true); 462 ;
307 });
308 describe('checkedKeys: [2002, 2003]', function() {
309 beforeEach(function() {
310 f.set('checkedKeys', ['2002', '2003']);
311 });
312 createBeforeAfterExpects(false, [false, false, true, true, false, false, false]);
313 });
314 });
315 });
316 createBeforeAfterExpects(true, [false, false, false, false, false, false, false]);
317 });
318 } 463 }
319 rangeSupportHelper(true, true); 464 if (hasAfter) {
320 rangeSupportHelper(true, false); 465 builder.setChecked('after', true)
321 rangeSupportHelper(false, false); 466 .createExpects(false, [false, false, true, false, true])
322 rangeSupportHelper(false, true); 467 .setChecked('checkedKeys', ['after', '1974-01-01T00:00:00.000Z'])
323 }); 468 .createExpects(false, [false, false, false, true, true])
469 .setChecked('checkedKeys', [])
470 .createExpects(true, [false, false, false, false, false])
471 ;
472 }
473 builder.endAll().build();
474 }
475 rangeSupportHelper({key: 'before', value: 5}, null);
476 rangeSupportHelper({key: 'before', value: 5}, {key: 'after', value: 23});
477 rangeSupportHelper(null, {key: 'after', value: 23});
478 rangeSupportHelper(null, null);
324 }); 479 });
325 return {}; 480 return {};
326 }); 481 });
......
...@@ -2,6 +2,7 @@ define(function(require) { ...@@ -2,6 +2,7 @@ define(function(require) {
2 'use strict'; 2 'use strict';
3 var _ = require('underscore'); 3 var _ = require('underscore');
4 var Util = require('./Util'); 4 var Util = require('./Util');
5 var Facet = require('./Facet');
5 var Facets = require('./Facets'); 6 var Facets = require('./Facets');
6 var Solr = require('./Solr'); 7 var Solr = require('./Solr');
7 8
...@@ -19,23 +20,26 @@ define(function(require) { ...@@ -19,23 +20,26 @@ define(function(require) {
19 return solrData; 20 return solrData;
20 } 21 }
21 22
22 function makeSolrResponseMixedCase(solrData) { 23 function makeSolrResponseMixedCase(solrResponse) {
23 var facetCounts = Util.getField(solrData, 'facet_counts'); 24 var facetCounts = Util.getField(solrResponse, 'facet_counts');
24 return { 25 return {
25 facetCounts: facetCounts,
26 facetRanges: Util.getField(facetCounts, 'facet_ranges'), 26 facetRanges: Util.getField(facetCounts, 'facet_ranges'),
27 facetFields: Util.getField(facetCounts, 'facet_fields'), 27 facetFields: Util.getField(facetCounts, 'facet_fields'),
28 facetQueries: Util.getField(facetCounts, 'facet_queries'), 28 facetQueries: Util.getField(facetCounts, 'facet_queries'),
29 statsFields: Util.getField(solrData.stats, 'stats_fields'), 29 statsFields: Util.getField(solrResponse.stats, 'stats_fields'),
30 }; 30 };
31 } 31 }
32 32
33 function createStaticSolrResponse(has, toMerge) { 33 function createStaticSolrResponse(has, toMerge) {
34 var solrResponse = createSolrResponse({stats: true}); 34 var solrResponse = createSolrResponse({stats: true});
35 var solrData = makeSolrResponseMixedCase(solrResponse); 35 var solrData = makeSolrResponseMixedCase(solrResponse);
36 _.each(solrData, function(value, key) { 36 if (_.isFunction(toMerge)) {
37 _.extend(value, toMerge[key]); 37 toMerge = toMerge(solrData);
38 }); 38 } else {
39 _.each(solrData, function(value, key) {
40 _.extend(value, toMerge[key]);
41 });
42 }
39 return solrResponse; 43 return solrResponse;
40 } 44 }
41 45
...@@ -45,26 +49,26 @@ define(function(require) { ...@@ -45,26 +49,26 @@ define(function(require) {
45 expect(item.get('checked')).toEqual(checked); 49 expect(item.get('checked')).toEqual(checked);
46 } 50 }
47 51
48 var facetsBuilder = function facetsBuilder() { 52 var definedFacets = {
49 return { 53 model: [
50 model: new Facets.Facet({ 54 {
51 formName: 'model', 55 formName: 'model',
52 label: 'Model', 56 label: 'Model',
53 }, 57 }, {
54 {
55 queryField: 'model_s', 58 queryField: 'model_s',
56 facetType: 'field', 59 facetType: 'field',
57 bins: { 60 bins: {
58 misc: ['saturn', 'jeep'], 61 misc: ['saturn', 'jeep'],
59 startsWithB: ['buick'], 62 startsWithB: ['buick'],
60 }, 63 },
61 }), 64 },
62 year: new Facets.Facet({ 65 ],
66 year: [
67 {
63 formName: 'year', 68 formName: 'year',
64 orderByDirection: 'desc', 69 orderByDirection: 'desc',
65 label: 'Year', 70 label: 'Year',
66 }, 71 }, {
67 {
68 queryField: 'year_dt', 72 queryField: 'year_dt',
69 comparator: 'date', 73 comparator: 'date',
70 facetType: 'year-field', 74 facetType: 'year-field',
...@@ -80,8 +84,10 @@ define(function(require) { ...@@ -80,8 +84,10 @@ define(function(require) {
80 var year = new Date(value).getUTCFullYear(); 84 var year = new Date(value).getUTCFullYear();
81 return year + ' - ' + (year + 2); 85 return year + ' - ' + (year + 2);
82 }, 86 },
83 }), 87 },
84 dateOfBirth: new Facets.Facet({ 88 ],
89 dateOfBirth: [
90 {
85 formName: 'dateOfBirth', 91 formName: 'dateOfBirth',
86 label: 'Birthday', 92 label: 'Birthday',
87 }, { 93 }, {
...@@ -91,8 +97,10 @@ define(function(require) { ...@@ -91,8 +97,10 @@ define(function(require) {
91 facetStats: true, 97 facetStats: true,
92 other: 'before', 98 other: 'before',
93 start: 'NOW/YEAR-25YEAR', 99 start: 'NOW/YEAR-25YEAR',
94 }), 100 },
95 value: new Facets.Facet({ 101 ],
102 value: [
103 {
96 formName: 'value', 104 formName: 'value',
97 label: 'Value', 105 label: 'Value',
98 }, { 106 }, {
...@@ -104,62 +112,113 @@ define(function(require) { ...@@ -104,62 +112,113 @@ define(function(require) {
104 var value = parseInt(item.get('key')); 112 var value = parseInt(item.get('key'));
105 return '$' + value + ' - $' + (value + 1000); 113 return '$' + value + ' - $' + (value + 1000);
106 }, 114 },
107 }), 115 },
108 }; 116 ],
117 bodyType: [
118 {
119 label: 'Type'
120 }, {
121 facetType: 'field',
122 labelMap: {
123 aluminum: 'Aluminum',
124 plastic: 'Plastic',
125 carbonComposite: 'Carbon Composite',
126 steel: 'Steel',
127 },
128 },
129 ],
130 };
131 var facetsBuilder = function facetsBuilder(facetName) {
132 if (facetName) {
133 return new Facet(definedFacets[facetName][0], definedFacets[facetName][1]);
134 }
135 var result = {};
136 _.each(_.keys(definedFacets), function(facetName) {
137 result[facetName] = new Facet(definedFacets[facetName][0], definedFacets[facetName][1]);
138 });
139 return result;
109 }; 140 };
110 141
142 function createSolrDataMerger(modelData) {
143 return function(solrData) {
144 _.each(solrData, function(solrValue, solrKey) {
145 _.each(modelData, function(modelObj, modelKey) {
146 var modelValue = modelObj[solrKey];
147 if (modelValue) {
148 if (solrKey === 'facetQueries') {
149 _.extend(solrValue, modelValue);
150 } else {
151 solrValue[modelKey] = modelValue;
152 }
153 }
154 });
155 });
156 };
157 }
158
159 var defaultModelData = {
160 dateOfBirth: {
161 facetRanges: {
162 counts: [
163 '1974-01-01T00:00:00.000Z', 1,
164 '1972-01-01T00:00:00.000Z', 1,
165 '1970-01-01T00:00:00.000Z', 1,
166 ],
167 before: 5,
168 },
169 statsFields: {min: '2000', max: '2010'},
170 },
171 bodyType: {
172 facetFields: [
173 'aluminum', 14,
174 'carbonComposite', 66,
175 'steel', 2,
176 ],
177 },
178
179 model: {
180 facetFields: [
181 'ford', 23,
182 'chevy', 18,
183 'toyota', 5,
184 'saturn', 7, // keep here to test the bin/exclude feature
185 'jeep', 19, // keep here to test the bin/exclude feature
186 'buick', 0, // keep here to test the bin/exclude feature
187 'dodge', 0,
188 ],
189 facetQueries: {
190 'model:misc': 26,
191 'model:startsWithB': 0,
192 },
193 },
194 value: {
195 facetFields: [
196 '23456', 1,
197 '5678', 2,
198 '1234', 3,
199 ],
200 statsFields: {min: '1234', max: '23456'},
201 },
202 year: {
203 facetFields: [
204 '2000-01-01T00:00:00.000Z', 1,
205 '2001-01-01T00:00:00.000Z', 4,
206 '2002-01-01T00:00:00.000Z', 0,
207 '2003-01-01T00:00:00.000Z', 3,
208 '2004-01-01T00:00:00.000Z', 0,
209 '2005-01-01T00:00:00.000Z', 0,
210 '2006-01-01T00:00:00.000Z', 0,
211 '2007-01-01T00:00:00.000Z', 0,
212 '2008-01-01T00:00:00.000Z', 7,
213 '2009-01-01T00:00:00.000Z', 0,
214 '2010-01-01T00:00:00.000Z', 1,
215 ],
216 statsFields: {min: '2000-01-01T00:00:00.000Z', max: '2010-01-01T00:00:00.000Z'},
217 },
218 };
111 var responses = { 219 var responses = {
112 sequential: [ 220 sequential: [
113 createStaticSolrResponse({stats: true}, { 221 createStaticSolrResponse({stats: true}, createSolrDataMerger(defaultModelData)),
114 facetRanges: {
115 dateOfBirth: {
116 counts: [
117 '1974-01-01T00:00:00.000Z', 1,
118 '1972-01-01T00:00:00.000Z', 1,
119 '1970-01-01T00:00:00.000Z', 1,
120 ],
121 before: 5,
122 },
123 },
124 facetFields: {
125 model: [
126 'ford', 23,
127 'chevy', 18,
128 'toyota', 5,
129 'saturn', 7, // keep here to test the bin/exclude feature
130 'jeep', 19, // keep here to test the bin/exclude feature
131 'buick', 0, // keep here to test the bin/exclude feature
132 'dodge', 0,
133 ],
134 value: [
135 '23456', 1,
136 '5678', 2,
137 '1234', 3,
138 ],
139 year: [
140 '2000-01-01T00:00:00.000Z', 1,
141 '2001-01-01T00:00:00.000Z', 4,
142 '2002-01-01T00:00:00.000Z', 0,
143 '2003-01-01T00:00:00.000Z', 3,
144 '2004-01-01T00:00:00.000Z', 0,
145 '2005-01-01T00:00:00.000Z', 0,
146 '2006-01-01T00:00:00.000Z', 0,
147 '2007-01-01T00:00:00.000Z', 0,
148 '2008-01-01T00:00:00.000Z', 7,
149 '2009-01-01T00:00:00.000Z', 0,
150 '2010-01-01T00:00:00.000Z', 1,
151 ],
152 },
153 facetQueries: {
154 'model:misc': 26,
155 'model:startsWithB': 0,
156 },
157 statsFields: {
158 year: {min: '2000', max: '2010'},
159 dateOfBirth: {min: '2000', max: '2010'},
160 value: {min: '1234', max: '23456'},
161 },
162 }),
163 ], 222 ],
164 suggestions: [ 223 suggestions: [
165 createStaticSolrResponse({stats: true}, { 224 createStaticSolrResponse({stats: true}, {
......
...@@ -29,7 +29,8 @@ define(function(require) { ...@@ -29,7 +29,8 @@ define(function(require) {
29 return obj[key]; 29 return obj[key];
30 }, 30 },
31 setField: function getField(obj, key, value) { 31 setField: function getField(obj, key, value) {
32 return obj[key] = value; 32 obj[key] = value;
33 return value;
33 }, 34 },
34 getItemKeyAccessor: function getItemKeyAccessor(item) { 35 getItemKeyAccessor: function getItemKeyAccessor(item) {
35 return item.get('key'); 36 return item.get('key');
......