-
Notifications
You must be signed in to change notification settings - Fork 15
/
livesearch_input_dropdown_test.js
227 lines (174 loc) · 5.4 KB
/
livesearch_input_dropdown_test.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
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
(function($) {
/*
======== A Handy Little QUnit Reference ========
http://api.qunitjs.com/
Test methods:
module(name, {[setup][ ,teardown]})
test(name, callback)
expect(numberOfAssertions)
stop(increment)
start(decrement)
Test assertions:
ok(value, [message])
equal(actual, expected, [message])
notEqual(actual, expected, [message])
deepEqual(actual, expected, [message])
notDeepEqual(actual, expected, [message])
strictEqual(actual, expected, [message])
notStrictEqual(actual, expected, [message])
throws(block, [expected], [message])
*/
module('jQuery#livesearch', {
// This will run before each test in this module.
setup: function() {
this.$input = $('#qunit-fixture input');
this.delay = 400;
this.makeServer = function () {
this.server = this.sandbox.useFakeServer();
this.server.respondWith(function (req) {
req.respond(
200,
{ "Content-Type": "application/json" },
'["' + req.url + '"]');
});
};
this.applyLivesearch = function (options) {
var callback = this.spy();
this.$input.livesearch(
$.extend(
{delay: this.delay, minimum_characters: 0}, options
)
);
this.$input.on('livesearch:results', callback);
return callback;
};
this.type = function (val, delay) {
this.$input.val(val);
this.$input.trigger('input');
this.clock.tick(delay || this.delay);
this.server.respond();
};
}
});
test('is chainable', function() {
expect(1);
// Not a bad test to run on collection methods.
strictEqual(this.$input.livesearch()[0], this.$input[0], 'should be chainable');
});
test('calls the server on input', function() {
expect(1);
this.makeServer();
var callback = this.applyLivesearch();
this.type('1');
ok(callback.args[0][1].length);
});
test('calls the server on change', function() {
expect(1);
this.makeServer();
var callback = this.applyLivesearch();
this.$input.trigger('change');
this.clock.tick(this.delay);
this.server.respond();
ok(callback.args[0][1].length);
});
test('does not search twice if new input is recieved before the delay', function() {
expect(1);
this.makeServer();
var callback = this.applyLivesearch();
this.type('1', this.delay / 2);
this.type('2');
strictEqual(callback.callCount, 1);
});
test('does not search twice if the value has not changed', function() {
expect(1);
this.makeServer();
var callback = this.applyLivesearch();
this.type('1');
this.type('1');
strictEqual(callback.callCount, 1);
});
test('does not search if the minimum characters are not reached', function() {
expect(1);
this.makeServer();
var callback = this.applyLivesearch({minimum_characters: 2});
this.type('1');
strictEqual(callback.callCount, 0);
});
test('caches results', function() {
expect(2);
this.makeServer();
var callback = this.applyLivesearch();
this.type('1');
this.type('2');
this.type('1');
strictEqual(callback.args[0][1], callback.args[2][1]);
strictEqual(this.server.requests.length, 2);
});
test('caching can be turned off', function() {
expect(1);
this.makeServer();
this.applyLivesearch({client_side_cache: false});
this.type('1');
this.type('2');
this.type('1');
strictEqual(this.server.requests.length, 3);
});
test('can be suspended', function () {
expect(2);
this.makeServer();
var callback = this.applyLivesearch();
this.$input.trigger('livesearch:suspend');
this.type('1');
strictEqual(callback.callCount, 0, 'suspended');
this.$input.trigger('livesearch:activate');
this.type('1');
strictEqual(callback.callCount, 1, 'activated');
});
test('can be suspended while a callback is executed', function () {
expect(2);
this.makeServer();
var callback = this.applyLivesearch();
var livesearch = this.$input.data('livesearch');
var test = this;
livesearch.suspend_while(function () {
test.type('1');
strictEqual(callback.callCount, 0, 'suspended');
});
this.$input.trigger('livesearch:activate');
this.type('1');
strictEqual(callback.callCount, 1, 'activated');
});
test('searches can be canceled', function () {
expect(1);
this.makeServer();
var callback = this.applyLivesearch();
this.$input.val('1');
this.$input.trigger('input');
this.clock.tick(this.delay);
this.$input.trigger('livesearch:cancel');
this.server.respond();
strictEqual(callback.callCount, 0);
});
test('process data can be used to change ajax params', function () {
expect(1);
this.makeServer();
var callback = this.applyLivesearch({
process_data: function () {
return {hello: 'hello'};
}
});
this.type('1');
var params = callback.args[0][1][0].split('?')[1];
strictEqual(params, 'hello=hello');
});
test('can ensure a file extension on xhr url', function () {
expect(1);
this.makeServer();
var callback = this.applyLivesearch({
file_extension: 'json'
});
this.type('1');
var extension = callback.args[0][1][0].split('.')[1].split('?')[0];
strictEqual(extension, 'json');
});
}(jQuery));