forked from twitter/typeahead.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_index_spec.js
60 lines (47 loc) · 1.69 KB
/
search_index_spec.js
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
describe('SearchIndex', function() {
beforeEach(function() {
this.searchIndex = new SearchIndex({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer
});
this.searchIndex.add(fixtures.data.simple);
});
it('should support serialization/deserialization', function() {
var serialized = this.searchIndex.serialize();
this.searchIndex = new SearchIndex({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer
});
this.searchIndex.bootstrap(serialized);
expect(this.searchIndex.get('smaller')).toEqual([{ value: 'smaller' }]);
});
it('should be able to add data on the fly', function() {
this.searchIndex.add({ value: 'new' });
expect(this.searchIndex.get('new')).toEqual([{ value: 'new' }]);
});
it('#get should return datums that match the given query', function() {
expect(this.searchIndex.get('big')).toEqual([
{ value: 'big' },
{ value: 'bigger' },
{ value: 'biggest' }
]);
expect(this.searchIndex.get('small')).toEqual([
{ value: 'small' },
{ value: 'smaller' },
{ value: 'smallest' }
]);
});
it('#get should return an empty array of there are no matches', function() {
expect(this.searchIndex.get('wtf')).toEqual([]);
});
it('#reset should empty the search index', function() {
this.searchIndex.reset();
expect(this.searchIndex.datums).toEqual([]);
expect(this.searchIndex.trie.ids).toEqual([]);
expect(this.searchIndex.trie.children).toEqual({});
});
// helper functions
// ----------------
function datumTokenizer(d) { return $.trim(d.value).split(/\s+/); }
function queryTokenizer(s) { return $.trim(s).split(/\s+/); }
});