Facets.spec.js
12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
define(function(require) {
'use strict';
var _ = require('underscore');
var Backbone = require('backbone');
var Facets = require('solr/model/Facets');
var Util = require('solr/model/Util');
var SolrSpecData = require('solr/model/Solr.spec.data');
describe('Facets', function() {
it('loads', function() {
expect(Facets).toBeDefined();
});
describe('foo', function() {
var f;
var formData = [];
var solrData = [];
var origNow;
var onItemChange;
beforeEach(function() {
jasmine.clock().install();
jasmine.clock().mockDate();
origNow = _.now;
_.now = function() { return new Date().getTime(); };
spyOn(Backbone, 'ajax');
onItemChange = jasmine.createSpy('onItemChange');
f = new Facets(SolrSpecData.facetsBuilder(), {search: {url: '/'}});
f.on('item-change', onItemChange);
});
afterEach(function() {
jasmine.clock().uninstall();
_.now = origNow;
});
describe('initial settings', function() {
it('foo', function() {
expect(true).toBe(true);
});
});
describe('step 1', function() {
beforeEach(function() {
formData.push(f.getFacetFormData());
});
describe('facets', function() {
var modelFacet, modelItems;
var yearFacet, yearItems;
var dobFacet, dobItems;
var valueFacet, valueItems;
var categoryFacet, categoryItems;
var odometerFacet, odometerItems;
beforeEach(function() {
f.applyFacetResults(SolrSpecData.responses.sequential[0]);
modelFacet = f.get('model');
modelItems = modelFacet.get('items');
yearFacet = f.get('year');
yearItems = yearFacet.get('items');
dobFacet = f.get('dateOfBirth');
dobItems = dobFacet.get('items');
valueFacet = f.get('value');
valueItems = valueFacet.get('items');
categoryFacet = f.get('category');
categoryItems = categoryFacet.get('items');
odometerFacet = f.get('odometer');
odometerItems = odometerFacet.get('items');
});
it('initial', function() {
//console.log(JSON.stringify(yearItems));
expect(yearItems.length).toBe(3);
SolrSpecData.expectItem(yearItems.at(0), '2000-01-01T00:00:00.000Z', 1, false);
SolrSpecData.expectItem(yearItems.at(1), '2004-01-01T00:00:00.000Z', 3, false);
SolrSpecData.expectItem(yearItems.at(2), '2010-01-01T00:00:00.000Z', 1, false);
expect(modelItems.length).toBe(4);
SolrSpecData.expectItem(modelItems.at(0), 'chevy', 18, false);
SolrSpecData.expectItem(modelItems.at(1), 'ford', 23, false);
SolrSpecData.expectItem(modelItems.at(2), 'misc', 26, false);
SolrSpecData.expectItem(modelItems.at(3), 'toyota', 5, false);
expect(dobItems.length).toBe(4);
SolrSpecData.expectItem(dobItems.at(0), 'before', 5, false);
SolrSpecData.expectItem(dobItems.at(1), '1970-01-01T00:00:00.000Z', 1, false);
SolrSpecData.expectItem(dobItems.at(2), '1972-01-01T00:00:00.000Z', 1, false);
SolrSpecData.expectItem(dobItems.at(3), '1974-01-01T00:00:00.000Z', 1, false);
expect(valueItems.length).toBe(7);
SolrSpecData.expectItem(valueItems.at(0), '1234', 1, false);
SolrSpecData.expectItem(valueItems.at(1), '2345', 2, false);
SolrSpecData.expectItem(valueItems.at(2), '3456', 3, false);
SolrSpecData.expectItem(valueItems.at(3), '4567', 4, false);
SolrSpecData.expectItem(valueItems.at(4), '5678', 5, false);
SolrSpecData.expectItem(valueItems.at(5), '6789', 6, false);
SolrSpecData.expectItem(valueItems.at(6), '7890', 7, false);
expect(categoryItems.length).toBe(3);
SolrSpecData.expectItem(categoryItems.at(0), '10000:First Category', 5, false);
SolrSpecData.expectItem(categoryItems.at(1), '20000:Second Category', 42, false);
SolrSpecData.expectItem(categoryItems.at(2), '30000:Third Category', 75, false);
expect(odometerItems.length).toBe(3);
SolrSpecData.expectItem(odometerItems.at(0), '0', 27, false);
SolrSpecData.expectItem(odometerItems.at(1), '50000', 73, false);
SolrSpecData.expectItem(odometerItems.at(2), '150000', 96, false);
expect(Backbone.ajax).not.toHaveBeenCalled();
expect(onItemChange).not.toHaveBeenCalled();
});
describe('click', function() {
beforeEach(function() {
modelItems.get('ford').set('checked', true);
modelItems.get('toyota').set('checked', true);
yearItems.get('2004-01-01T00:00:00.000Z').set('checked', true);
dobItems.get('before').set('checked', true);
dobItems.get('1972-01-01T00:00:00.000Z').set('checked', true);
valueFacet.set('queryMin', 1234);
valueFacet.set('queryMax', 6000);
categoryItems.get('20000:Second Category').set('checked', true);
odometerItems.get('50000').set('checked', true);
});
it('result', function() {
expect(Backbone.ajax).not.toHaveBeenCalled();
expect(onItemChange).toHaveBeenCalled();
expect(onItemChange.calls.count()).toEqual(9);
console.log(JSON.stringify(onItemChange.calls.allArgs(), null, 1));
var facetFormData = f.getFacetFormData();
console.log(JSON.stringify(facetFormData, null, 1));
expect(facetFormData.facet).toBe(true);
expect(facetFormData.stats).toBe(true);
expect(facetFormData['facet.missing']).toBe(true);
expect(facetFormData['facet.field']).toEqual([
'{!key=model ex=model}model_s',
'{!key=year ex=year}year_dt',
'{!key=value ex=value}value_d',
'{!key=bodyType tag=bodyType}bodyType_s',
'{!key=category ex=category}category_and_name_s',
]);
expect(facetFormData['facet.range']).toEqual([
'{!facet.range.other=before facet.range.start=NOW/YEAR-25YEAR facet.range.gap=+2YEAR key=dateOfBirth ex=dateOfBirth}dateOfBirth_dt',
'{!facet.range.gap=5000 key=odometer ex=odometer}odometer_i',
]);
expect(facetFormData['facet.query']).toEqual([
'{!key=model:misc tag=model:misc}(model_s:saturn OR model_s:jeep)',
'{!key=model:startsWithB tag=model:startsWithB}(model_s:buick)',
'{!key=year:ancient tag=year:ancient}(year_dt:\'1950-01-01T00:00:00.000Z\' OR year_dt:\'1952-01-01T00:00:00.000Z\')',
]);
expect(facetFormData['stats.field']).toEqual([
'{!key=year ex=year}year_dt',
'{!facet.range.other=before facet.range.start=NOW/YEAR-25YEAR facet.range.gap=+2YEAR key=dateOfBirth ex=dateOfBirth}dateOfBirth_dt',
'{!key=value ex=value}value_d',
'{!facet.range.gap=5000 key=odometer ex=odometer}odometer_i',
]);
expect(facetFormData.fq).toEqual([
'{!tag=model}model_s:("ford" OR "toyota")',
'{!tag=year}year_dt:(2004-01-01T00:00:00.000Z)',
'{!tag=dateOfBirth}dateOfBirth_dt:([* TO NOW/YEAR-25YEAR-1MILLISECOND] OR [1972-01-01T00:00:00.000Z TO 1973-12-31T23:59:59.999Z])',
'{!tag=value}value_d:([1234 TO 6000])',
'{!tag=category}category_id_s:(20000)',
'{!tag=odometer}odometer_i:([50000 TO 55000])',
]);
});
});
});
describe('suggestions', function() {
beforeEach(function() {
f.get('model').set('query', 'fo');
});
it('initial', function() {
expect(true).toBe(true);
expect(Backbone.ajax).not.toHaveBeenCalled();
});
describe('call solr', function() {
beforeEach(function() {
jasmine.clock().tick(300);
});
it('ajax called with', function() {
expect(Backbone.ajax).toHaveBeenCalled();
expect(Backbone.ajax.calls.count()).toBe(1);
var ajaxData = Backbone.ajax.calls.argsFor(0);
//console.log(JSON.stringify(ajaxData, null, 1));
expect(ajaxData.length).toBe(1);
var arg0 = ajaxData[0];
expect(arg0.facetSuggestions).toBe(true);
expect(arg0.childFacet).toBe(f.get('model'));
var data = arg0.data;
expect(data.rows).toBe(0);
});
describe('result', function() {
var wantedSuggestions;
beforeEach(function() {
var ajaxData = Backbone.ajax.calls.argsFor(0);
var arg0 = ajaxData[0];
arg0.success(SolrSpecData.responses.suggestions[0], arg0);
wantedSuggestions = [
{key: 'toyota', value: 5},
{key: 'ford', value: 23},
{key: 'chevy', value: 18},
];
});
it('applied', function() {
expect(f.get('model').get('suggestions')).toEqual(wantedSuggestions);
});
describe('reset suggestions', function() {
beforeEach(function() {
f.get('model').set('query', '');
});
it('foo', function() {
expect(Backbone.ajax).toHaveBeenCalled();
expect(Backbone.ajax.calls.count()).toBe(1);
expect(f.get('model').get('suggestions')).toEqual(wantedSuggestions);
});
describe('call solr', function() {
beforeEach(function() {
jasmine.clock().tick(300);
});
it('foo', function() {
expect(Backbone.ajax).toHaveBeenCalled();
expect(Backbone.ajax.calls.count()).toBe(1);
expect(f.get('model').get('suggestions')).toEqual([]);
});
});
});
});
});
});
});
});
});
});