Major rework, uses a builder pattern now.
Showing
4 changed files
with
485 additions
and
271 deletions
... | @@ -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,32 +4,89 @@ define(function(require) { | ... | @@ -4,32 +4,89 @@ 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 | build: function build() { | ||
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(); | ||
20 | } | 45 | } |
21 | items.set(newItems); | 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 | }, |
25 | it('all', function() { | 65 | }); |
26 | expect(f.get('all')).toBe(all); | 66 | var FacetBaseBuilder = BaseBuilder.extend({ |
27 | }); | 67 | type: 'FacetBaseBuilder', |
28 | 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) { | ||
29 | var wantedCheckedKeysLength = 0; | 85 | var wantedCheckedKeysLength = 0; |
30 | var wantedCheckedKeys = {}; | 86 | var wantedCheckedKeys = {}; |
31 | var wantedChecks = {}; | 87 | var wantedChecks = {}; |
32 | var wantedItemOrder = []; | 88 | var wantedItemOrder = []; |
89 | optionalHasQuery = optionalHasQuery || {}; | ||
33 | function scanKey(key, wanted) { | 90 | function scanKey(key, wanted) { |
34 | wantedChecks[key] = wanted; | 91 | wantedChecks[key] = wanted; |
35 | if (wanted) { | 92 | if (wanted) { |
... | @@ -38,9 +95,33 @@ define(function(require) { | ... | @@ -38,9 +95,33 @@ define(function(require) { |
38 | } | 95 | } |
39 | wantedItemOrder.push(key); | 96 | wantedItemOrder.push(key); |
40 | } | 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 | } | ||
41 | for (var i = 0; i < orderedItems.length; i++) { | 110 | for (var i = 0; i < orderedItems.length; i++) { |
42 | scanKey(ascItemOrder[i], orderedItems[i]); | 111 | scanKey(ascItemOrder[i], orderedItems[i]); |
43 | } | 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 | }); | ||
121 | it('all', function() { | ||
122 | expect(f.get('all')).toBe(all); | ||
123 | }); | ||
124 | |||
44 | it('items', function() { | 125 | it('items', function() { |
45 | var gotChecks = {}; | 126 | var gotChecks = {}; |
46 | var gotOrder = []; | 127 | var gotOrder = []; |
... | @@ -70,177 +151,279 @@ define(function(require) { | ... | @@ -70,177 +151,279 @@ 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 | }); | 164 | }); |
165 | } else { | ||
166 | it('has query:min', function() { | ||
167 | expect(f.get('hasQuery')).toBe(false); | ||
161 | }); | 168 | }); |
162 | describe('checked all', function() { | 169 | } |
163 | var c3; | 170 | } else if (optionalHasQuery.max) { |
164 | beforeEach(function() { | 171 | it('has query:max', function() { |
165 | f.set('all', true); | 172 | expect(f.get('hasQuery')).toBe(false); |
166 | c3 = f.get('checkedKeys'); | ||
167 | }); | 173 | }); |
168 | createExpects(true, [false, false, false, false, false]); | 174 | } |
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 | }); | 175 | }); |
174 | describe('checkedKeys: [d, e]', function() { | 176 | return this; |
175 | var c4; | 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; | ||
176 | beforeEach(function() { | 198 | beforeEach(function() { |
177 | f.set('checkedKeys', ['d', 'e']); | 199 | var f = builder.facetBuilder().get('f'); |
178 | c4 = f.get('checkedKeys'); | 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')); | ||
179 | }); | 213 | }); |
180 | createExpects(false, [false, false, false, true, true]); | ||
181 | it('checkedKeys not same', function() { | 214 | it('checkedKeys not same', function() { |
182 | expect(c4).not.toBe(c3); | 215 | for (var i = 0; i < checkedKeysList.length; i++) { |
183 | expect(c4).not.toBe(c2); | 216 | for (var j = 0; j < checkedKeysList.length; j++) { |
184 | expect(c4).not.toBe(c1); | 217 | if (i != j) { |
185 | expect(c4).not.toBe(c0); | 218 | expect(checkedKeysList[i]).not.toBe(checkedKeysList[j]); |
186 | }); | 219 | } |
187 | }); | 220 | } |
188 | }); | 221 | } |
189 | }); | ||
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 | }); |
229 | var FacetBuilder = FacetBaseBuilder.extend({ | ||
230 | type: 'FacetBuilder', | ||
231 | constructor: function constructor(nameOrBuilder, description) { | ||
232 | FacetBaseBuilder.call( | ||
233 | this, | ||
234 | null, | ||
235 | {} | ||
236 | ); | ||
237 | if (description) { | ||
238 | description = 'facet[:' + description + ']'; | ||
239 | } else { | ||
240 | description = 'facet[' + nameOrBuilder + ']'; | ||
241 | } | ||
242 | this.description = function () { | ||
243 | return description; | ||
197 | }; | 244 | }; |
198 | var pojoItems = [ | 245 | this.nameOrBuilder = nameOrBuilder; |
199 | {key: '2005', value: 500}, | 246 | this.addBuilder(function(builder) { |
200 | {key: '2004', value: 400}, | 247 | var f; |
201 | {key: '2003', value: 300}, | ||
202 | {key: '2002', value: 200}, | ||
203 | {key: '2001', value: 100}, | ||
204 | ]; | ||
205 | ascItemOrder = ['2001', '2002', '2003', '2004', '2005']; | ||
206 | var c0; | ||
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 { |
211 | }); | 252 | f = builder.nameOrBuilder(); |
212 | createPojoItems(pojoItems); | 253 | } |
213 | describe('query', function() { | 254 | builder.set('f', f); |
214 | describe('has', function() { | 255 | builder.set('items', f.get('items')); |
215 | it('min', function() { | 256 | builder.set('checkedKeysList', [f.get('checkedKeys')]); |
216 | f.set({queryMin: 1000, queryMax: undefined}); | 257 | }); |
217 | expect(f.get('hasQuery')).toBe(false); | 258 | it('exists[' + name + ']', function() { |
218 | }); | 259 | expect(f).not.toBe(undefined); |
219 | it('max', function() { | 260 | expect(f).not.toBe(null); |
220 | f.set({queryMin: undefined, queryMax: 5000}); | 261 | }); |
221 | expect(f.get('hasQuery')).toBe(false); | 262 | }); |
222 | }); | 263 | }, |
223 | it('min+max', function() { | 264 | facetBuilder: function facetBuilder() { |
224 | f.set({queryMin: 1000, queryMax: 5000}); | 265 | return this; |
225 | expect(f.get('hasQuery')).toBe(true); | 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)); | ||
282 | } | ||
283 | for (var i = 0; i < pojoItems.length; i++) { | ||
284 | newItems.push(new Facet.Item(pojoItems[i])); | ||
285 | } | ||
286 | if (builder.hasAfter) { | ||
287 | newItems.push(new Facet.Item(builder.hasAfter)); | ||
288 | } | ||
289 | builder.get('items').set(newItems); | ||
226 | }); | 290 | }); |
227 | }); | 291 | }); |
292 | return this; | ||
293 | }, | ||
228 | }); | 294 | }); |
229 | createExpects(true, [false, false, false, false, false]); | 295 | describe('Facet', function() { |
296 | it('loads', function() { | ||
297 | expect(Facet).toBeDefined(); | ||
230 | }); | 298 | }); |
231 | function rangeSupportHelper(hasBefore, hasAfter) { | 299 | function setFacet(nameOrBuilder, description) { |
232 | function createBeforeAfterExpects(all, orderedItems) { | 300 | return new FacetBuilder(nameOrBuilder, description); |
233 | if (!hasBefore) { | ||
234 | orderedItems.shift(); | ||
235 | } | 301 | } |
236 | if (!hasAfter) { | 302 | |
237 | orderedItems.pop(); | 303 | setFacet('model') |
238 | } | 304 | .createPojoItems([ |
239 | createExpects(all, orderedItems); | 305 | {key: 'toyota', value: 5}, |
240 | } | 306 | {key: 'misc', value: 26}, |
241 | describe('type[year]' + (hasBefore ? ' hasBefore' : '') + (hasAfter ? ' hasAfter' : ''), function() { | 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() { | ||
242 | var facetOptions = { | 425 | var facetOptions = { |
243 | queryField: 'value_d', | 426 | queryField: 'dateOfBirth_dt', |
244 | facetType: 'year', | 427 | facetType: 'year', |
245 | facetStats: true, | 428 | facetStats: true, |
246 | }; | 429 | }; |
... | @@ -253,74 +436,46 @@ define(function(require) { | ... | @@ -253,74 +436,46 @@ define(function(require) { |
253 | } else if (hasAfter) { | 436 | } else if (hasAfter) { |
254 | facetOptions.other = 'after'; | 437 | facetOptions.other = 'after'; |
255 | } | 438 | } |
256 | var pojoItems = [ | 439 | return new Facet(null, facetOptions); |
257 | {key: '2005', value: 500}, | 440 | }, description) |
258 | {key: '2004', value: 400}, | 441 | .createPojoItems([ |
259 | {key: '2003', value: 300}, | 442 | {key: '1974-01-01T00:00:00.000Z', value: 1}, |
260 | {key: '2002', value: 200}, | 443 | {key: '1972-01-01T00:00:00.000Z', value: 1}, |
261 | {key: '2001', value: 100}, | 444 | {key: '1970-01-01T00:00:00.000Z', value: 1}, |
262 | ]; | 445 | ]) |
263 | var beforeItem = {key: 'before', value: 2000}; | 446 | .setAscItemOrder([ |
264 | var afterItem = {key: 'after', value: 1500}; | 447 | '1970-01-01T00:00:00.000Z', |
265 | ascItemOrder = ['2001', '2002', '2003', '2004', '2005']; | 448 | '1972-01-01T00:00:00.000Z', |
266 | var c0; | 449 | '1974-01-01T00:00:00.000Z', |
267 | beforeEach(function() { | 450 | ]) |
268 | f = new Facet(null, facetOptions); | 451 | .setBeforeAfter(hasBefore, hasAfter) |
269 | items = f.get('items'); | 452 | .setChecked('1972-01-01T00:00:00.000Z', true) |
270 | c0 = f.get('checkedKeys'); | 453 | .createExpects(false, [false, false, true, false, false]) |
271 | }); | 454 | ; |
272 | if (hasAfter) { | ||
273 | ascItemOrder.push('after'); | ||
274 | pojoItems.push(afterItem); | ||
275 | } | ||
276 | if (hasBefore) { | 455 | if (hasBefore) { |
277 | ascItemOrder.unshift('before'); | 456 | builder.setChecked('before', true) |
278 | pojoItems.unshift(beforeItem); | 457 | .createExpects(false, [true, false, true, false, false]) |
458 | .setChecked('checkedKeys', ['before', '1974-01-01T00:00:00.000Z']) | ||
459 | .createExpects(false, [true, false, false, true, false]) | ||
460 | .setChecked('checkedKeys', []) | ||
461 | .createExpects(true, [false, false, false, false, false]) | ||
462 | ; | ||
279 | } | 463 | } |
280 | createPojoItems(pojoItems); | 464 | if (hasAfter) { |
281 | describe('query', function() { | 465 | builder.setChecked('after', true) |
282 | describe('min', function() { | 466 | .createExpects(false, [false, false, true, false, true]) |
283 | beforeEach(function() { | 467 | .setChecked('checkedKeys', ['after', '1974-01-01T00:00:00.000Z']) |
284 | f.set({queryMin: '2002', queryMax: undefined}); | 468 | .createExpects(false, [false, false, false, true, true]) |
285 | }); | 469 | .setChecked('checkedKeys', []) |
286 | createBeforeAfterExpects(true, [false, false, false, false, false, false, false]); | 470 | .createExpects(true, [false, false, false, false, false]) |
287 | it('result', function() { | 471 | ; |
288 | expect(f.get('hasQuery')).toBe(false); | ||
289 | }); | ||
290 | }); | ||
291 | describe('max', function() { | ||
292 | beforeEach(function() { | ||
293 | f.set({queryMin: undefined, queryMax: '2004'}); | ||
294 | }); | ||
295 | createBeforeAfterExpects(true, [false, false, false, false, false, false, false]); | ||
296 | it('result', function() { | ||
297 | expect(f.get('hasQuery')).toBe(false); | ||
298 | }); | ||
299 | }); | ||
300 | describe('min+max', function() { | ||
301 | beforeEach(function() { | ||
302 | f.set({queryMin: '1980', queryMax: '1984'}); | ||
303 | }); | ||
304 | createBeforeAfterExpects(false, [false, false, false, false, false, false, false]); | ||
305 | it('result', function() { | ||
306 | expect(f.get('hasQuery')).toBe(true); | ||
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 | } | 472 | } |
319 | rangeSupportHelper(true, true); | 473 | builder.endAll().build(); |
320 | rangeSupportHelper(true, false); | 474 | } |
321 | rangeSupportHelper(false, false); | 475 | rangeSupportHelper({key: 'before', value: 5}, null); |
322 | rangeSupportHelper(false, true); | 476 | rangeSupportHelper({key: 'before', value: 5}, {key: 'after', value: 23}); |
323 | }); | 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 | if (_.isFunction(toMerge)) { | ||
37 | toMerge = toMerge(solrData); | ||
38 | } else { | ||
36 | _.each(solrData, function(value, key) { | 39 | _.each(solrData, function(value, key) { |
37 | _.extend(value, toMerge[key]); | 40 | _.extend(value, toMerge[key]); |
38 | }); | 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,15 +112,53 @@ define(function(require) { | ... | @@ -104,15 +112,53 @@ 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 | }, |
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 | ], | ||
108 | }; | 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 | ||
111 | var responses = { | 142 | function createSolrDataMerger(modelData) { |
112 | sequential: [ | 143 | return function(solrData) { |
113 | createStaticSolrResponse({stats: true}, { | 144 | _.each(solrData, function(solrValue, solrKey) { |
114 | facetRanges: { | 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 = { | ||
115 | dateOfBirth: { | 160 | dateOfBirth: { |
161 | facetRanges: { | ||
116 | counts: [ | 162 | counts: [ |
117 | '1974-01-01T00:00:00.000Z', 1, | 163 | '1974-01-01T00:00:00.000Z', 1, |
118 | '1972-01-01T00:00:00.000Z', 1, | 164 | '1972-01-01T00:00:00.000Z', 1, |
... | @@ -120,9 +166,18 @@ define(function(require) { | ... | @@ -120,9 +166,18 @@ define(function(require) { |
120 | ], | 166 | ], |
121 | before: 5, | 167 | before: 5, |
122 | }, | 168 | }, |
169 | statsFields: {min: '2000', max: '2010'}, | ||
123 | }, | 170 | }, |
124 | facetFields: { | 171 | bodyType: { |
125 | model: [ | 172 | facetFields: [ |
173 | 'aluminum', 14, | ||
174 | 'carbonComposite', 66, | ||
175 | 'steel', 2, | ||
176 | ], | ||
177 | }, | ||
178 | |||
179 | model: { | ||
180 | facetFields: [ | ||
126 | 'ford', 23, | 181 | 'ford', 23, |
127 | 'chevy', 18, | 182 | 'chevy', 18, |
128 | 'toyota', 5, | 183 | 'toyota', 5, |
... | @@ -131,12 +186,21 @@ define(function(require) { | ... | @@ -131,12 +186,21 @@ define(function(require) { |
131 | 'buick', 0, // keep here to test the bin/exclude feature | 186 | 'buick', 0, // keep here to test the bin/exclude feature |
132 | 'dodge', 0, | 187 | 'dodge', 0, |
133 | ], | 188 | ], |
134 | value: [ | 189 | facetQueries: { |
190 | 'model:misc': 26, | ||
191 | 'model:startsWithB': 0, | ||
192 | }, | ||
193 | }, | ||
194 | value: { | ||
195 | facetFields: [ | ||
135 | '23456', 1, | 196 | '23456', 1, |
136 | '5678', 2, | 197 | '5678', 2, |
137 | '1234', 3, | 198 | '1234', 3, |
138 | ], | 199 | ], |
139 | year: [ | 200 | statsFields: {min: '1234', max: '23456'}, |
201 | }, | ||
202 | year: { | ||
203 | facetFields: [ | ||
140 | '2000-01-01T00:00:00.000Z', 1, | 204 | '2000-01-01T00:00:00.000Z', 1, |
141 | '2001-01-01T00:00:00.000Z', 4, | 205 | '2001-01-01T00:00:00.000Z', 4, |
142 | '2002-01-01T00:00:00.000Z', 0, | 206 | '2002-01-01T00:00:00.000Z', 0, |
... | @@ -149,17 +213,12 @@ define(function(require) { | ... | @@ -149,17 +213,12 @@ define(function(require) { |
149 | '2009-01-01T00:00:00.000Z', 0, | 213 | '2009-01-01T00:00:00.000Z', 0, |
150 | '2010-01-01T00:00:00.000Z', 1, | 214 | '2010-01-01T00:00:00.000Z', 1, |
151 | ], | 215 | ], |
216 | statsFields: {min: '2000-01-01T00:00:00.000Z', max: '2010-01-01T00:00:00.000Z'}, | ||
152 | }, | 217 | }, |
153 | facetQueries: { | 218 | }; |
154 | 'model:misc': 26, | 219 | var responses = { |
155 | 'model:startsWithB': 0, | 220 | sequential: [ |
156 | }, | 221 | createStaticSolrResponse({stats: true}, createSolrDataMerger(defaultModelData)), |
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'); | ... | ... |
-
Please register or sign in to post a comment