Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/apps/system/test/unit/date_picker_test.js
blob: 51d886773b40518825c4017c1dc9492d7ffa0eec (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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
requireApp('system/js/value_selector/date_picker.js');

suite('date picker', function() {
  var subject;
  var Calc;
  var triggerEvent;

  teardown(function() {
    var el = document.getElementById('test');
    el.parentNode.removeChild(el);
  });

  setup(function() {
    var div = document.createElement('div');
    div.id = 'test';
    document.body.appendChild(div);
    subject = new DatePicker(div);
    Calc = DatePicker.Calc;
    subject._position = new Date(2012, 1, 1);
  });

  suite('Calc', function() {
    var mocked = {};

    function mock(fn, value) {
      if (!(fn in mocked)) {
        mocked[fn] = subject[fn];
      }
      subject[fn] = function() {
        return value;
      };
    }

    teardown(function() {
      var key;
      for (key in mocked) {
        if (mocked.hasOwnProperty(key)) {
          subject[key] = mocked[key];
        }
      }
    });

    setup(function() {
      subject = DatePicker.Calc;
    });

    suite('#isSameDate', function() {

      test('same day', function() {
        assert.isTrue(subject.isSameDate(
          new Date(2012, 1, 1, 8),
          new Date(2012, 1, 1, 23)
        ));
      });

      test('same day different month', function() {
        assert.isFalse(subject.isSameDate(
          new Date(2012, 2, 1, 8),
          new Date(2012, 1, 1, 8)
        ));
      });
    });

    suite('#isToday', function() {
      test('when given is today', function() {
        var result = subject.isToday(new Date());

        assert.isTrue(result, 'should be true when given today');
      });

      test('when given is not today', function() {
        var now = new Date();
        now.setDate(now.getDate() - 1);
        var result = subject.isToday(now);

        assert.isFalse(result, 'should be false when given is not today');
      });
    });

    suite('#isPast', function() {
      test('when date is passed', function() {
        var date = new Date();
        date.setTime(Date.now() - 1000);
        var result = subject.isPast(date);

        assert.isTrue(result, 'should be true when given is in the past');
      });

      test('when given is in the future', function() {
        var date = new Date();
        date.setTime(Date.now() + 100);
        var result = subject.isPast(date);

        assert.isFalse(result,
                       'should return false when date is in the future');
      });

    });

    suite('#isFuture', function() {
      test('when date is passed', function() {
        var date = new Date();
        date.setTime(Date.now() - 100);
        var result = subject.isFuture(date);

        assert.isFalse(result);
      });

      test('when given is in the future', function() {
        var date = new Date(Date.now() + 100);
        var result = subject.isFuture(date);

        assert.isTrue(result);
      });

    });

    suite('#dateFromId', function() {
      var id;
      var result;
      var date = new Date(2012, 7, 3);

      setup(function() {
        id = subject.getDayId(date);
      });

      test('id to date', function() {
        assert.deepEqual(
          subject.dateFromId(id),
          date
        );
      });

    });

    test('#getDayId', function() {
      var result = subject.getDayId(
        new Date(2012, 3, 7)
      );

      assert.equal(result, '2012-3-7');
    });

    suite('#relativeState', function() {

      setup(function() {
        mock('isToday', false);
      });

      test('when in the past', function() {
        mock('isPast', true);
        var state = subject.relativeState(
          new Date(1991, 1, 1),
          new Date(1991, 1, 1)
        );

        assert.equal(state, subject.PAST);
      });

      test('when in the future', function() {
        mock('isPast', false);
        var state = subject.relativeState(
          new Date(1991, subject.today.getMonth(), 1),
          new Date(1991, subject.today.getMonth(), 1)
        );
        assert.equal(state, subject.FUTURE);
      });

      test('when is in a different month in the past', function() {
        mock('isPast', true);

        var state = subject.relativeState(
          new Date(1991, subject.today.getMonth() - 1, 1),
          new Date(1991, subject.today.getMonth(), 1)
        );

        assert.include(state, subject.PAST);
        assert.include(state, subject.OTHER_MONTH);
      });

      test('when is in a different month in the future', function() {
        mock('isPast', false);

        var state = subject.relativeState(
          new Date(1991, subject.today.getMonth() + 1, 1),
          new Date(1991, subject.today.getMonth(), 1)
        );

        assert.include(state, subject.FUTURE);
        assert.include(state, subject.OTHER_MONTH);
      });


      test('when is today', function() {
        mock('isToday', true);
        var state = subject.relativeState(new Date(1991, 1, 1));

        assert.equal(state, subject.PRESENT);
      });

    });

  });

  suite('#_daysIn', function() {
    test('leap year', function() {
      var result = subject._daysInMonth(2012, 1);
      assert.equal(result, 29);
    });

    test('normal', function() {
      var result = subject._daysInMonth(2012, 0);
      assert.equal(result, 31);
    });
  });

  suite('#_renderDay', function() {

    test('simple', function() {
      var date = new Date(2012, 1, 27);
      var result = subject._renderDay(date).firstChild;
      var html = result.outerHTML;

      assert.ok(html);
      assert.equal(result.dataset.date, Calc.getDayId(date));
      assert.include(html, '27');
    });

    test('today', function() {
      var date = new Date();
      var result = subject._renderDay(date);
      assert.ok(result.classList.contains('present'));
    });

    test('past', function() {
      var date = new Date(2009, 1, 1);
      var result = subject._renderDay(date);
      assert.ok(result.classList.contains('past'));
    });

    test('future', function() {
      var date = new Date();
      date.setDate(date.getDate() + 2);

      var result = subject._renderDay(date);
      assert.ok(result.classList.contains('future'));
    });
  });

  suite('#_renderWeek', function() {
    var days = [
      new Date(2012, 0, 29),
      new Date(2012, 0, 30),
      new Date(2012, 0, 31),
      new Date(2012, 1, 1),
      new Date(2012, 1, 2),
      new Date(2012, 1, 3),
      new Date(2012, 1, 4)
    ];

    var result;

    setup(function() {
      result = subject._renderWeek(days);
    });

    test('container', function() {
      assert.equal(result.tagName.toLowerCase(), 'ol');
      assert.ok(result.outerHTML);
    });

    days.forEach(function(day) {
      test('week day ' + day, function() {
        var expected = subject._renderDay(day);
        assert.include(result.outerHTML, expected.outerHTML);
      });
    });

  });

  suite('#_renderMonth', function() {

    function weekHtml(start, end) {
      var range = Calc.daysBetween(start, end);
      return subject._renderWeek(range).outerHTML;
    }

    test('Feb 2012', function() {
      var month = 1;
      var year = 2012;

      var result = subject._renderMonth(year, month);
      var html = result.outerHTML;

      assert.ok(html, 'has contents');

      assert.include(
        html,
        weekHtml(new Date(2012, 0, 29), new Date(2012, 1, 4)),
        'has first week'
      );

      assert.include(
        html,
        weekHtml(new Date(2012, 1, 5), new Date(2012, 1, 11)),
        'has second week'
      );

      assert.include(
        html,
        weekHtml(new Date(2012, 1, 12), new Date(2012, 1, 18)),
        'has third week'
      );

      assert.include(
        html,
        weekHtml(new Date(2012, 1, 19), new Date(2012, 1, 25)),
        'has fourth week'
      );


      assert.include(
        html,
        weekHtml(new Date(2012, 1, 26), new Date(2012, 2, 3)),
        'has fifth week'
      );
    });
  });

  suite('prev/next', function() {
    var calledWith;

    setup(function() {
      subject.display(2012, 0, 1);
    });

    test('#next', function() {
      subject.next();
      assert.equal(subject.year, 2012);
      assert.equal(subject.month, 1);
    });

    test('#previous', function() {
      subject.previous();
      assert.equal(subject.year, 2011);
      assert.equal(subject.month, 11);
    });

  });

  suite('prev/next with last day of a month', function() {
    var calledWith;

    setup(function() {
      // init as 2012/3/31
      subject.display(2012, 2, 31);
    });

    test('#next', function() {
      subject.next();

      // should be 2012/4/30
      assert.equal(subject.year, 2012);
      assert.equal(subject.month, 3);
      assert.equal(subject.date, 30);
    });

    test('#previous', function() {
      subject.previous();

      // should be 2012/2/29
      assert.equal(subject.year, 2012);
      assert.equal(subject.month, 1);
      assert.equal(subject.date, 29);
    });

  });

  suite('#display', function() {
    var year = 2012;
    var month = 11;
    var date = 1;
    var calledWith;

    setup(function() {
      calledWith = null;
      subject.onmonthchange = function() {
        calledWith = arguments;
      };

      subject.display(year, month, date);
    });

    test('initial render', function() {
      assert.deepEqual(
        calledWith[0],
        new Date(year, month),
        'should fire onmonthchange'
      );

      assert.equal(subject.year, 2012);
      assert.equal(subject.month, 11);
      assert.equal(subject.date, 1);
      assert.ok(subject.monthDisplay);
    });

    test('second rendering', function() {
      subject.display(2011, 2, 1);

      assert.deepEqual(
        calledWith[0],
        new Date(2011, 2),
        'should fire onmonthchange again'
      );

      assert.equal(subject.year, 2011);
      assert.equal(subject.month, 2);
      assert.equal(subject.date, 1);
      assert.ok(subject.monthDisplay);
    });
  });

  suite('setters', function() {
    test('#value', function() {
      var calledWith;
      var date;

      subject.onvaluechange = function() {
        calledWith = arguments;
      }

      subject.value = date = new Date(2012, 1, 1);

      assert.deepEqual(subject.value, date);
      assert.deepEqual(calledWith, [date, null]);
    });
  });

  suite('dom events', function() {
    function triggerEvent(element, eventName) {
      var event = document.createEvent('HTMLEvents');
      event.initEvent(eventName, true, true);
      element.dispatchEvent(event);
    }

    setup(function() {
      subject.display(2012, 1, 1);
    });

    test('select', function() {
      var calledWith;
      subject.onvaluechange = function() {
        calledWith = arguments;
      }

      var target = subject.element.querySelector('[data-date="2012-1-1"]');
      triggerEvent(target, 'click');

      assert.ok(target.classList.contains('selected'), 'adds selected class');

      assert.deepEqual(
        subject.value,
        new Date(2012, 1, 1),
        'changes value'
      );

      assert.deepEqual(
        calledWith,
        [new Date(2012, 1, 1), new Date(2012, 1, 1)],
        'calls onvaluechange'
      );

      triggerEvent(
        subject.element.querySelector('[data-date="2012-1-2"]'),
        'click'
      );

      assert.ok(!target.classList.contains('selected'), 'clears past selected');
    });
  });

});