-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathbrowserifySpec.js
More file actions
386 lines (338 loc) · 8.26 KB
/
browserifySpec.js
File metadata and controls
386 lines (338 loc) · 8.26 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import assert from 'node:assert';
import itemsJS from '../src/index.js';
describe('itemjs general tests', function () {
const items = [
{
name: 'movie1',
tags: ['a', 'b', 'c', 'd'],
actors: ['a', 'b'],
},
{
name: 'movie2',
tags: ['a', 'e', 'f'],
actors: ['a', 'b'],
},
{
name: 'movie3',
tags: ['a', 'c'],
actors: ['e'],
},
];
const similarItems = [
{
name: 'movie1',
tags: 'Another tag',
},
{
name: 'movie2',
tags: 'Another',
},
{
name: 'movie3',
tags: 'Another tag',
},
];
const itemsjs = itemsJS(items);
it('makes search', function test(done) {
const result = itemsjs.search();
assert.equal(result.data.items.length, 3);
done();
});
it('makes search with pagination', function test(done) {
let result = itemsjs.search({
per_page: 1,
});
assert.equal(result.data.items.length, 1);
result = itemsjs.search({
per_page: 1,
page: 4,
});
assert.equal(result.data.items.length, 0);
result = itemsjs.search({
per_page: 1,
page: 3,
});
assert.equal(result.data.items.length, 1);
done();
});
it('makes search with pagination, and is_all_filtered_items', function test(done) {
let result = itemsjs.search({
per_page: 1,
is_all_filtered_items: true,
});
assert.equal(result.data.items.length, 1);
assert.equal(result.data.allFilteredItems.length, 3);
result = itemsjs.search({
per_page: 1,
is_all_filtered_items: false,
});
assert.equal(result.data.items.length, 1);
assert.equal(result.data.allFilteredItems, null);
result = itemsjs.search({
per_page: 1,
});
assert.equal(result.data.items.length, 1);
assert.equal(result.data.allFilteredItems, null);
done();
});
it('makes search with pagination and filter', function test(done) {
const result = itemsjs.search({
per_page: 1,
page: 3,
filter: (item) => item.tags.includes('a'),
});
assert.equal(result.data.items.length, 1);
done();
});
it('makes search with aggregation filters', function test(done) {
const itemsjs = itemsJS(items, {
aggregations: {
tags: {},
actors: {},
},
});
let result = itemsjs.search({
filters: {
tags: ['e', 'f'],
},
});
assert.equal(result.data.items.length, 1);
result = itemsjs.search({
filters: {
tags: ['e', 'f'],
actors: ['a', 'b'],
},
});
assert.equal(result.data.items.length, 1);
/*var result = itemsjs.search({
filters: {
tags: ['e', 'f'],
actors: ['a', 'd']
}
});
assert.equal(result.data.items.length, 0);
var result = itemsjs.search();
assert.equal(result.data.items.length, 3);*/
done();
});
it('makes search with aggregation filters with single value object', function test(done) {
const itemsjs = itemsJS(similarItems, {
aggregations: {
tags: {},
},
});
const result = itemsjs.search();
assert.equal(result.data.items.length, 3);
assert.equal(result.data.aggregations.tags.buckets[0].doc_count, 2); // Another tag
assert.equal(result.data.aggregations.tags.buckets[1].doc_count, 1); // Another
/*var result = itemsjs.search({
query: '',
filters: {
name: [],
tags: ['Another tag']
},
per_page: Infinity
});
assert.equal(result.data.aggregations.tags.buckets[0].doc_count, result.data.items.length);
assert.equal(result.data.items.length, 2);
var result = itemsjs.search({
filters: {
tags: ['Another']
}
});
assert.equal(result.data.aggregations.tags.buckets[0].doc_count, result.data.items.length);
assert.equal(result.data.items.length, 1);*/
done();
});
it('makes aggregations when configuration supplied', function test(done) {
const itemsjs = itemsJS(items, {
aggregations: {
tags: {
type: 'terms',
size: 10,
title: 'Tags',
},
},
});
const result = itemsjs.search({});
assert.equal(result.data.items.length, 3);
//assert.equal(result.data.aggregations.tags.name, 'tags');
assert.equal(result.data.aggregations.tags.buckets.length, 6);
done();
});
it('makes aggregations for non array (string) fields', function test(done) {
const items = [
{
name: 'movie1',
tags: 'a',
},
{
name: 'movie2',
tags: 'a',
},
{
name: 'movie3',
tags: 'a',
},
];
const itemsjs = itemsJS(items, {
aggregations: {
tags: {
type: 'terms',
size: 10,
title: 'Tags',
},
},
});
const result = itemsjs.search({});
assert.equal(result.data.items.length, 3);
//assert.equal(result.data.aggregations.tags.name, 'tags');
assert.equal(result.data.aggregations.tags.buckets.length, 1);
assert.equal(result.data.aggregations.tags.buckets[0].doc_count, 3);
done();
});
it('makes aggregations with facet_stats', function test(done) {
const items = [
{
name: 'Apple 7',
price: 1,
},
{
name: 'Apple 8',
price: 1,
},
{
name: 'Apple 9',
price: '7',
},
{
name: 'Samsung',
price: 7,
},
{
name: 'Apple 10',
},
];
const itemsjs = itemsJS(items, {
aggregations: {
price: {
title: 'Price',
size: 3,
show_facet_stats: true,
},
},
});
const result = itemsjs.search({
query: 'Apple',
});
assert.equal(result.data.aggregations.price.facet_stats.min, 1);
assert.equal(result.data.aggregations.price.facet_stats.max, 7);
assert.equal(result.data.aggregations.price.facet_stats.avg, 3);
assert.equal(result.data.aggregations.price.facet_stats.sum, 9);
done();
});
it('makes aggregations with facet_stats and string values', function test(done) {
const items = [
{
name: 'movie1',
tags: '€ 1 euro',
},
{
name: 'movie2',
tags: '€ 1 euro',
},
{
name: 'movie3',
tags: '€ 1 euro',
},
];
const itemsjs = itemsJS(items, {
aggregations: {
tags: {
title: 'Tags',
size: 1,
show_facet_stats: true,
},
},
});
try {
itemsjs.search({
query: '',
});
} catch (err) {
assert.equal(
err.message,
'You cant use chars to calculate the facet_stats.',
);
}
done();
});
xit('makes aggregations for undefined field', function test(done) {
const items = [
{
name: 'movie1',
},
{
name: 'movie2',
},
{
name: 'movie3',
},
];
const itemsjs = itemsJS(items, {
aggregations: {
tags: {
type: 'terms',
size: 10,
title: 'Tags',
},
},
});
const result = itemsjs.search({});
assert.equal(result.data.items.length, 3);
//assert.equal(result.data.aggregations.tags.buckets.length, {});
done();
});
it('returns empty list when similar base item is missing', function test(done) {
const itemsWithIds = items.map((item, idx) => ({
...item,
id: idx + 1,
}));
const itemsjs = itemsJS(itemsWithIds, {
aggregations: {
tags: {},
},
});
const result = itemsjs.similar(999, { field: 'tags', per_page: 5 });
assert.equal(result.pagination.total, 0);
assert.deepEqual(result.data.items, []);
done();
});
it('search by tags', function test(done) {
const items = [
{
name: 'movie1',
tags: ['drama'],
},
{
name: 'movie2',
tags: ['drama', 'crime'],
},
{
name: 'movie3',
},
];
const itemsjs = itemsJS(items, {
searchableFields: ['name', 'tags'],
});
let result = itemsjs.search({
query: 'drama',
});
assert.equal(result.data.items.length, 2);
result = itemsjs.search({
query: 'crime',
});
assert.equal(result.data.items.length, 1);
done();
});
});