Issues (99)

test/test.js (1 issue)

1
describe('Jets', function() {
2
3
  var people = ['John Doe', 'Mike Doe', 'Alex Smith', 'Alice Vazovsky', 'Denis Koen'],
4
    $search = $('#jetsSearch'),
5
    searchNode = $search.get(0),
6
    $content = $('#jetsContent');
7
8
9
  /*
10
   * Helper functions
11
   */
12
  function trigger(ev, _elem) {
13
    elem = _elem || searchNode
14
    if ("createEvent" in document) {
15
        var evt = document.createEvent("HTMLEvents");
16
        evt.initEvent(ev, false, true);
17
        elem.dispatchEvent(evt);
18
    } else elem.fireEvent("on" + ev);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
19
  }
20
21
  function ext(options) {
22
    return $.extend({
23
      searchTag: '#jetsSearch',
24
      contentTag: '#jetsContent'
25
    }, options || {});
26
  }
27
28
  function make(action) {
29
    action();
30
    trigger('change');
31
  }
32
33
34
  /*
35
   * Mocha helpers
36
   */ 
37
  before(function() {
38
    $content.html($.map(people, function(val, i){
39
      var reversed = val.split('').reverse().join('');
40
      return '<tr><td data-custom="' + reversed + '">' + val + '</td><td>' + i + '</td></tr>'
41
    }).join(''));
42
  })
43
44
  afterEach(function() {
45
    jet && jet.destroy();
46
    $search.val('');
47
    trigger('change', searchNode);
48
  })
49
50
  after(function() {
51
    jet && jet.destroy();
52
    $search.val('');
53
    $content.find('[data-custom]').removeAttr('data-custom');
54
  })
55
56
57
  /*
58
  * Specs
59
  */
60
61
  it('Should hide inappropriate rows', function() {
62
    jet = new Jets(ext({}));
63
    make(function() {
64
      $search.val(people[0]);
65
    })
66
    assert.lengthOf($content.children(':visible'), 1);
67
  })
68
69
  it('Should show all rows if search query not specified', function() {
70
    jet = new Jets(ext({}));
71
    make(function() {
72
      $search.val('');
73
    })
74
    assert.lengthOf($content.children(':visible'), people.length);
75
  })
76
77
  it('Should hide all rows if no suitable results', function() {
78
    jet = new Jets(ext({}));
79
    make(function() {
80
      $search.val('404');
81
    })
82
    assert.lengthOf($content.children(':visible'), 0);
83
  })
84
85
  describe('On init', function() {
86
87
    it('Shoud add style tag after init', function() {
88
      jet = new Jets(ext({}));
89
      assert.lengthOf($('head').find(jet.styleTag), 1);
90
    })
91
92
    it('Should add "data-jets" attribute', function() {
93
      jet = new Jets(ext({
94
        columns: [0]
95
      }));
96
      var $firstRow = $content.children(':first');
97
      assert.equal($firstRow.attr('data-jets'), $firstRow.children(':first').text().trim().toLowerCase());
98
    })
99
100
  })
101
102
  describe('On destroy', function() {
103
104
    it('Shoud remove style tag after destroy', function() {
105
      jet = new Jets(ext({}));
106
      jet.destroy();
107
      assert.lengthOf($('head').find(jet.styleTag), 0);
108
    })
109
110
    it('Should remove "data-jets" attribute', function() {
111
      jet = new Jets(ext({}));
112
      jet.destroy()
113
      assert.isUndefined($content.children(':first').attr('data-jets'));
114
    })
115
    
116
  })
117
118
  describe('Columns option', function() {
119
    
120
    it('Should search by any column', function() {
121
      jet = new Jets(ext({}));
122
      make(function() {
123
        $search.val('1');
124
      })
125
      assert.lengthOf($content.children(':visible'), 1);
126
    })
127
128
    it('Should find one row by first column', function() {
129
      jet = new Jets(ext({
130
        columns: [0]
131
      }));
132
      make(function() {
133
        $search.val(people[0]);
134
      })
135
      assert.lengthOf($content.children(':visible'), 1);
136
    })
137
138
    it('Should avoid second column', function() {
139
      jet = new Jets(ext({
140
        columns: [0]
141
      }));
142
      make(function() {
143
        $search.val('1');
144
      })
145
      assert.lengthOf($content.children(':visible'), 0);
146
    })
147
    
148
  })
149
150
  describe('addImportant option', function() {
151
152
    it('Should avoid !important by default', function() {
153
      jet = new Jets(ext({}));
154
      make(function() {
155
        $search.val(people[0]);
156
      })
157
      assert.notInclude($(jet.styleTag).html(), '!important');
158
    })
159
160
    it('Should add !important', function() {
161
      jet = new Jets(ext({
162
        addImportant: true
163
      }));
164
      make(function() {
165
        $search.val(people[0]);
166
      })
167
      assert.include($(jet.styleTag).html(), '!important');
168
    })
169
170
  })
171
172
  describe('searchSelector option', function() {
173
174
    function execute(selector) {
175
      it('Should use custom selector: ' + selector, function() {
176
        jet = new Jets(ext({
177
          searchSelector: selector
178
        }));
179
        make(function() {
180
          $search.val(people[0]);
181
        })
182
        assert.include($(jet.styleTag).html(), selector);  
183
      })
184
    }
185
186
    var selectors = ['^', '$', '~', '|'];
187
    for(var i = 0, ii = selectors.length; i < ii; i++) {
188
      execute(selectors[i]);
189
    }
190
191
    it('Should find one result with * selector', function() {
192
193
      jet = new Jets(ext({
194
        searchSelector: '*'
195
      }));
196
      make(function() {
197
        $search.val('John Doe');
198
      })
199
      assert.lengthOf($content.children(':visible'), 1);
200
201
    })
202
203
    it('Should hide all results with * selector and mixed words', function() {
204
205
      jet = new Jets(ext({
206
        searchSelector: '*'
207
      }));
208
      make(function() {
209
        $search.val('Doe John');
210
      })
211
      assert.lengthOf($content.children(':visible'), 0);
212
213
    })
214
215
    it('Should find one result with *AND selector and mixed words', function() {
216
217
      jet = new Jets(ext({
218
        searchSelector: '*AND'
219
      }));
220
      make(function() {
221
        $search.val('Doe John');
222
      })
223
      assert.lengthOf($content.children(':visible'), 1);
224
225
    })
226
227
    it('Should find one result with *AND selector and part of word', function() {
228
229
      jet = new Jets(ext({
230
        searchSelector: '*AND'
231
      }));
232
      make(function() {
233
        $search.val('John oe');
234
      })
235
      assert.lengthOf($content.children(':visible'), 1);
236
237
    })
238
239
    it('Should find three results with *OR selector and part of word', function() {
240
241
      jet = new Jets(ext({
242
        searchSelector: '*OR'
243
      }));
244
      make(function() {
245
        $search.val('John oe');
246
      })
247
      assert.lengthOf($content.children(':visible'), 3);
248
249
    })
250
251
  })
252
253
  describe('manualContentHandling option', function() {
254
255
    it('Should fetch content by custom rule', function() {
256
      jet = new Jets(ext({
257
        manualContentHandling: function(tag) {
258
          return $(tag).children(0).attr('data-custom')
259
        }
260
      }));
261
      make(function() {
262
        $search.val(people[0].split('').reverse().join(''));
263
      })
264
      assert.lengthOf($content.children(':visible'), 1);
265
    })
266
267
  })
268
269
  describe('invert option', function() {
270
271
    it('Should invert results', function() {
272
      jet = new Jets(ext({
273
        invert: true
274
      }));
275
      make(function() {
276
        $search.val(people[0]);
277
      })
278
      assert.lengthOf($content.children(':visible'), 3);
279
    })
280
281
  })
282
283
  describe('callSearchManually option', function() {
284
285
    it('Should not call search on typing', function() {
286
      jet = new Jets(ext({
287
        callSearchManually: true
288
      }));
289
      make(function() {
290
        $search.val(people[0]);
291
      })
292
      assert.lengthOf($content.children(':visible'), 5);
293
    })
294
295
    it('Should call search by manual calling .search', function() {
296
      jet = new Jets(ext({
297
        callSearchManually: true
298
      }));
299
      make(function() {
300
        $search.val(people[0]);
301
      })
302
      jet.search();
303
      assert.lengthOf($content.children(':visible'), 1);
304
    })
305
306
    it('Should call search by manual calling .search with search phrase as attribute', function() {
307
      jet = new Jets(ext({
308
        callSearchManually: true
309
      }));
310
      jet.search(people[0]);
311
      assert.lengthOf($content.children(':visible'), 1);
312
    })
313
314
  })
315
316
  describe('searchInSpecificColumn option', function() {
317
318
    it('Should search in specific column', function() {
319
      // duplicate data in columns (in reversed order)
320
      $content.html($.map(people, function(val, i){
321
        var reversed = val.split('').reverse().join('');
322
        return '<tr><td>' + val + '</td><td>' + people[people.length - i - 1] + '</td></tr>'
323
      }).join(''));
324
      jet = new Jets(ext({
325
        callSearchManually: true,
326
        searchInSpecificColumn: true
327
      }));
328
      make(function() {
329
        jet.search(people[0], 0);
330
      })
331
      assert.lengthOf($content.children(':visible'), 1);
332
    })
333
334
    it('Should search in all column in column not specified', function() {
335
      // duplicate data in columns (in reversed order)
336
      $content.html($.map(people, function(val, i){
337
        var reversed = val.split('').reverse().join('');
338
        return '<tr><td>' + val + '</td><td>' + people[people.length - i - 1] + '</td></tr>'
339
      }).join(''));
340
      jet = new Jets(ext({
341
        callSearchManually: true,
342
        searchInSpecificColumn: true
343
      }));
344
      make(function() {
345
        jet.search(people[0]);
346
      })
347
      assert.lengthOf($content.children(':visible'), 2);
348
    })
349
350
  })
351
352
})