Welcome to mirror list, hosted at ThFree Co, Russian Federation.

suite.spec.js « test « mocha-3.1.2 « lib « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4cb62dd290686bd01f12790501b4d69bfd37e1e0 (plain)
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
'use strict';

var mocha = require('../');
var Suite = mocha.Suite;
var Test = mocha.Test;

describe('Suite', function () {
  describe('.clone()', function () {
    beforeEach(function () {
      this.suite = new Suite('To be cloned');
      this.suite._timeout = 3043;
      this.suite._slow = 101;
      this.suite._bail = true;
      this.suite.suites.push(1);
      this.suite.tests.push('hello');
      this.suite._beforeEach.push(2);
      this.suite._beforeAll.push(3);
      this.suite._afterEach.push(4);
      this.suite._afterAll.push(5);
    });

    it('should copy the title', function () {
      this.suite.clone().title.should.equal('To be cloned');
    });

    it('should copy the timeout value', function () {
      this.suite.clone().timeout().should.equal(3043);
    });

    it('should copy the slow value', function () {
      this.suite.clone().slow().should.equal(101);
    });

    it('should copy the bail value', function () {
      this.suite.clone().bail().should.be.true();
    });

    it('should not copy the values from the suites array', function () {
      this.suite.clone().suites.should.be.empty();
    });

    it('should not copy the values from the tests array', function () {
      this.suite.clone().tests.should.be.empty();
    });

    it('should not copy the values from the _beforeEach array', function () {
      this.suite.clone()._beforeEach.should.be.empty();
    });

    it('should not copy the values from the _beforeAll array', function () {
      this.suite.clone()._beforeAll.should.be.empty();
    });

    it('should not copy the values from the _afterEach array', function () {
      this.suite.clone()._afterEach.should.be.empty();
    });

    it('should not copy the values from the _afterAll array', function () {
      this.suite.clone()._afterAll.should.be.empty();
    });
  });

  describe('.timeout()', function () {
    beforeEach(function () {
      this.suite = new Suite('A Suite');
    });

    describe('when no argument is passed', function () {
      it('should return the timeout value', function () {
        this.suite.timeout().should.equal(2000);
      });
    });

    describe('when argument is passed', function () {
      it('should return the Suite object', function () {
        var newSuite = this.suite.timeout(5000);
        newSuite.timeout().should.equal(5000);
      });
    });
  });

  describe('.slow()', function () {
    beforeEach(function () {
      this.suite = new Suite('A Suite');
    });

    describe('when given a string', function () {
      it('should parse it', function () {
        this.suite.slow('5 seconds');
        this.suite.slow().should.equal(5000);
      });
    });

    describe('when no argument is passed', function () {
      it('should return the slow value', function () {
        this.suite.slow().should.equal(75);
      });
    });

    describe('when argument is passed', function () {
      it('should return the Suite object', function () {
        var newSuite = this.suite.slow(5000);
        newSuite.slow().should.equal(5000);
      });
    });
  });

  describe('.bail()', function () {
    beforeEach(function () {
      this.suite = new Suite('A Suite');
      this.suite._bail = true;
    });

    describe('when no argument is passed', function () {
      it('should return the bail value', function () {
        this.suite.bail().should.be.true();
      });
    });

    describe('when argument is passed', function () {
      it('should return the Suite object', function () {
        var newSuite = this.suite.bail(false);
        newSuite.bail().should.be.false();
      });
    });
  });

  describe('.beforeAll()', function () {
    beforeEach(function () {
      this.suite = new Suite('A Suite');
    });

    describe('wraps the passed in function in a Hook', function () {
      it('adds it to _beforeAll', function () {
        var fn = function () {};
        this.suite.beforeAll(fn);

        this.suite._beforeAll.should.have.length(1);
        var beforeAllItem = this.suite._beforeAll[0];
        beforeAllItem.title.should.match(/^"before all" hook/);
        beforeAllItem.fn.should.equal(fn);
      });

      it('appends title to hook', function () {
        var fn = function () {};
        this.suite.beforeAll('test', fn);

        this.suite._beforeAll.should.have.length(1);
        var beforeAllItem = this.suite._beforeAll[0];
        beforeAllItem.title.should.equal('"before all" hook: test');
        beforeAllItem.fn.should.equal(fn);

        function namedFn () {}
        this.suite.beforeAll(namedFn);
        this.suite._beforeAll.should.have.length(2);
        beforeAllItem = this.suite._beforeAll[1];
        beforeAllItem.title.should.equal('"before all" hook: namedFn');
        beforeAllItem.fn.should.equal(namedFn);
      });
    });
  });

  describe('.afterAll()', function () {
    beforeEach(function () {
      this.suite = new Suite('A Suite');
    });

    describe('wraps the passed in function in a Hook', function () {
      it('adds it to _afterAll', function () {
        var fn = function () {};
        this.suite.afterAll(fn);

        this.suite._afterAll.should.have.length(1);
        var afterAllItem = this.suite._afterAll[0];
        afterAllItem.title.should.match(/^"after all" hook/);
        afterAllItem.fn.should.equal(fn);
      });
      it('appends title to hook', function () {
        var fn = function () {};
        this.suite.afterAll('test', fn);

        this.suite._afterAll.should.have.length(1);
        var beforeAllItem = this.suite._afterAll[0];
        beforeAllItem.title.should.equal('"after all" hook: test');
        beforeAllItem.fn.should.equal(fn);

        function namedFn () {}
        this.suite.afterAll(namedFn);
        this.suite._afterAll.should.have.length(2);
        beforeAllItem = this.suite._afterAll[1];
        beforeAllItem.title.should.equal('"after all" hook: namedFn');
        beforeAllItem.fn.should.equal(namedFn);
      });
    });
  });

  describe('.beforeEach()', function () {
    beforeEach(function () {
      this.suite = new Suite('A Suite');
    });

    describe('wraps the passed in function in a Hook', function () {
      it('adds it to _beforeEach', function () {
        var fn = function () {};
        this.suite.beforeEach(fn);

        this.suite._beforeEach.should.have.length(1);
        var beforeEachItem = this.suite._beforeEach[0];
        beforeEachItem.title.should.match(/^"before each" hook/);
        beforeEachItem.fn.should.equal(fn);
      });

      it('appends title to hook', function () {
        var fn = function () {};
        this.suite.beforeEach('test', fn);

        this.suite._beforeEach.should.have.length(1);
        var beforeAllItem = this.suite._beforeEach[0];
        beforeAllItem.title.should.equal('"before each" hook: test');
        beforeAllItem.fn.should.equal(fn);

        function namedFn () {}
        this.suite.beforeEach(namedFn);
        this.suite._beforeEach.should.have.length(2);
        beforeAllItem = this.suite._beforeEach[1];
        beforeAllItem.title.should.equal('"before each" hook: namedFn');
        beforeAllItem.fn.should.equal(namedFn);
      });
    });
  });

  describe('.afterEach()', function () {
    beforeEach(function () {
      this.suite = new Suite('A Suite');
    });

    describe('wraps the passed in function in a Hook', function () {
      it('adds it to _afterEach', function () {
        var fn = function () {};
        this.suite.afterEach(fn);

        this.suite._afterEach.should.have.length(1);
        var afterEachItem = this.suite._afterEach[0];
        afterEachItem.title.should.match(/^"after each" hook/);
        afterEachItem.fn.should.equal(fn);
      });

      it('appends title to hook', function () {
        var fn = function () {};
        this.suite.afterEach('test', fn);

        this.suite._afterEach.should.have.length(1);
        var beforeAllItem = this.suite._afterEach[0];
        beforeAllItem.title.should.equal('"after each" hook: test');
        beforeAllItem.fn.should.equal(fn);

        function namedFn () {}
        this.suite.afterEach(namedFn);
        this.suite._afterEach.should.have.length(2);
        beforeAllItem = this.suite._afterEach[1];
        beforeAllItem.title.should.equal('"after each" hook: namedFn');
        beforeAllItem.fn.should.equal(namedFn);
      });
    });
  });

  describe('.addSuite()', function () {
    beforeEach(function () {
      this.first = new Suite('First suite');
      this.first.timeout(4002);
      this.first.slow(200);
      this.second = new Suite('Second suite');
      this.first.addSuite(this.second);
    });

    it('sets the parent on the added Suite', function () {
      this.second.parent.should.equal(this.first);
    });

    it('copies the timeout value', function () {
      this.second.timeout().should.equal(4002);
    });

    it('copies the slow value', function () {
      this.second.slow().should.equal(200);
    });

    it('adds the suite to the suites collection', function () {
      this.first.suites.should.have.length(1);
      this.first.suites[0].should.equal(this.second);
    });

    it('treats suite as pending if its parent is pending', function () {
      this.first.pending = true;
      this.second.isPending.should.be.true;
    });
  });

  // describe('.addTest()', function(){
  //   beforeEach(function(){
  //     this.suite = new Suite('A Suite', new Context);
  //     this.suite.timeout(4002);
  //     this.test = new Test('test');
  //     this.suite.addTest(this.test);
  //   });
  //
  //   it('sets the parent on the added test', function(){
  //     this.test.parent.should.equal(this.suite);
  //   });
  //
  //   it('copies the timeout value', function(){
  //     this.test.timeout().should.equal(4002);
  //   });
  //
  //   it('adds the test to the tests collection', function(){
  //     this.suite.tests.should.have.length(1);
  //     this.suite.tests[0].should.equal(this.test);
  //   });
  // });

  describe('.fullTitle()', function () {
    beforeEach(function () {
      this.suite = new Suite('A Suite');
    });

    describe('when there is no parent', function () {
      it('returns the suite title', function () {
        this.suite.fullTitle().should.equal('A Suite');
      });
    });

    describe('when there is a parent', function () {
      it('returns the combination of parent\'s and suite\'s title', function () {
        var parentSuite = new Suite('I am a parent');
        parentSuite.addSuite(this.suite);
        this.suite.fullTitle().should.equal('I am a parent A Suite');
      });
    });
  });

  describe('.total()', function () {
    beforeEach(function () {
      this.suite = new Suite('A Suite');
    });

    describe('when there are no nested suites or tests', function () {
      it('should return 0', function () {
        this.suite.total().should.equal(0);
      });
    });

    describe('when there are several tests in the suite', function () {
      it('should return the number', function () {
        this.suite.addTest(new Test('a child test'));
        this.suite.addTest(new Test('another child test'));
        this.suite.total().should.equal(2);
      });
    });
  });

  describe('.eachTest(fn)', function () {
    beforeEach(function () {
      this.suite = new Suite('A Suite');
    });

    describe('when there are no nested suites or tests', function () {
      it('should return 0', function () {
        var n = 0;
        function fn () { n++; }
        this.suite.eachTest(fn);
        n.should.equal(0);
      });
    });

    describe('when there are several tests in the suite', function () {
      it('should return the number', function () {
        this.suite.addTest(new Test('a child test'));
        this.suite.addTest(new Test('another child test'));

        var n = 0;
        function fn () { n++; }
        this.suite.eachTest(fn);
        n.should.equal(2);
      });
    });

    describe('when there are several levels of nested suites', function () {
      it('should return the number', function () {
        this.suite.addTest(new Test('a child test'));
        var suite = new Suite('a child suite');
        suite.addTest(new Test('a test in a child suite'));
        this.suite.addSuite(suite);

        var n = 0;
        function fn () { n++; }
        this.suite.eachTest(fn);
        n.should.equal(2);
      });
    });
  });

  describe('initialization', function () {
    /* eslint no-new: off */
    it('should throw an error if the title isn\'t a string', function () {
      (function () {
        new Suite(undefined, 'root');
      }).should.throw();

      (function () {
        new Suite(function () {}, 'root');
      }).should.throw();
    });

    it('should not throw if the title is a string', function () {
      (function () {
        new Suite('Bdd suite', 'root');
      }).should.not.throw();
    });
  });
});

describe('Test', function () {
  describe('initialization', function () {
    it('should throw an error if the title isn\'t a string', function () {
      (function () {
        new Test(function () {});
      }).should.throw();

      (function () {
        new Test(undefined, function () {});
      }).should.throw();
    });

    it('should not throw if the title is a string', function () {
      (function () {
        new Test('test-case', function () {});
      }).should.not.throw();
    });
  });
});