UNPKG

2.8 kBJavaScriptView Raw
1const should = require('should');
2const MockDate = require('..');
3
4describe('MockDate', function() {
5
6 var mockDate = '1/1/2000';
7 var currentYear = new Date().getFullYear();
8 var nativeToString = Date.toString();
9 var mockDateRealOffset = new Date(mockDate).getTimezoneOffset();
10 var currentDateRealOffset = new Date().getTimezoneOffset();
11
12 beforeEach(function () {
13 MockDate.set(new Date(mockDate));
14 });
15
16 afterEach(function () {
17 MockDate.reset();
18 });
19
20 it('should throw for bad date', function() {
21 should.throws(function () {
22 MockDate.set('40/40/2000');
23 }, 'mockdate: The time set is an invalid date: 40/40/2000');
24
25 should.throws(function () {
26 MockDate.set(NaN);
27 }, 'mockdate: The time set is an invalid date: NaN');
28 });
29
30 it('should override new Date()', function() {
31 should.equal(new Date().toString(), new Date(mockDate).toString());
32 should.equal(new Date().getFullYear(), 2000);
33 });
34
35 it('should override Date.now()', function() {
36 should.equal(Date.now(), new Date(mockDate).valueOf());
37 });
38
39 it('should override Date.parse()', function() {
40 should.equal('807926400000', Date.parse('Wed, 09 Aug 1995 00:00:00 GMT'));
41 });
42
43 it('should allow mock dates to show up as real dates using instanceof', function() {
44 should.ok(new Date() instanceof Date);
45 });
46
47 it('should have the same toString as the native Date object does', function() {
48 should.equal(Date.toString(), nativeToString);
49 });
50
51 it('should be able to create a specific date from a timestamp', function() {
52 var date = new Date(807926400000);
53 should.equal('Wed, 09 Aug 1995 00:00:00 GMT', date.toUTCString());
54 });
55
56 it('should be able to create a specific date from year, month', function() {
57 var locDate = new Date(1995, 7);
58 var utcMs = locDate.valueOf()-locDate.getTimezoneOffset()*60*1000;
59 var utcDate = new Date(utcMs);
60 should.equal('Tue, 01 Aug 1995 00:00:00 GMT', utcDate.toUTCString());
61 });
62
63 it('should be able to create a specific date from year, month, date', function() {
64 var locDate = new Date(1995, 7, 9);
65 var utcMs = locDate.valueOf()-locDate.getTimezoneOffset()*60*1000;
66 var utcDate = new Date(utcMs);
67 should.equal('Wed, 09 Aug 1995 00:00:00 GMT', utcDate.toUTCString());
68 });
69
70 it('should respect a date of 0', function() {
71 var locDate = new Date(1995, 7, 0);
72 should.equal(locDate.getDate(), 31);
73 });
74
75 it('should be able to create a date correctly from the epoch', function() {
76 MockDate.set(0);
77 should.equal('Thu, 01 Jan 1970 00:00:00 GMT', new Date().toUTCString());
78 });
79
80 it('should revert correctly', function() {
81 MockDate.reset();
82 should.equal(new Date().getFullYear(), currentYear);
83 should.ok(Date.toString().indexOf('native'));
84 });
85});