UNPKG

la-gregory

Version:
104 lines 3.99 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var _1 = require("."); describe('jest-date-mock', function () { afterEach(function () { _1.clearDateMock(); }); it('can mock Date class methods', function () { _1.setDate(0); // Date isEqual expect(new Date()).toEqual(new Date(0)); // Date.now expect(Date.now()).toBe(0); expect(+new Date(10000)).toBe(10000); // getTime expect(new Date().getTime()).toBe(0); expect(new Date(10000).getTime()).toBe(10000); // instanceof expect(new Date()).toBeInstanceOf(Date); // 2018-05-27 08:00:00 expect(new Date(Date.UTC(2018, 5, 27, 0, 0, 0)).getTime()).toBe(1530057600000); expect(new Date(2018, 5, 27, 0, 0, 0)).toEqual(new Date('2018-06-27 00:00:00')); var DerivedDate = /** @class */ (function (_super) { __extends(DerivedDate, _super); function DerivedDate() { return _super !== null && _super.apply(this, arguments) || this; } return DerivedDate; }(Date)); var derivedDate = new DerivedDate(); expect(derivedDate).toBeInstanceOf(Date); expect(derivedDate).toBeInstanceOf(DerivedDate); expect(+derivedDate).toBe(0); expect(new Date()).not.toBe(new Date()); expect(Date.name).toBe('Date'); }); it('can mock Date.now', function () { _1.setDate(1000); expect(Date.now()).toBe(1000); _1.setDate(0); expect(Date.now()).toBe(0); _1.advanceDate(520); expect(Date.now()).toBe(520); }); it('can mock performance.now', function () { var performance = global.window ? global.window.performance : require('perf_hooks').performance; _1.setDate(1000); expect(performance.now()).toBe(1000); _1.setDate(0); expect(performance.now()).toBe(0); _1.advanceDate(520); expect(performance.now()).toBe(520); _1.setPerformanceOffset(-100); expect(performance.now()).toBe(420); }); it('setDate can advance time to argument', function () { _1.setDate(1000); expect(Date.now()).toBe(1000); _1.setDate(0); expect(Date.now()).toBe(0); }); it('advanceDate can advance time by argument', function () { _1.advanceDate(3000); var now = Date.now(); _1.advanceDate(4000); expect(Date.now() - now).toBe(4000); _1.advanceDate(-4000); expect(Date.now() - now).toBe(0); _1.advanceDate(0); expect(Date.now() - now).toBe(0); }); it('clearDateMock can remove date mocking', function () { _1.clearDateMock(); expect(Date.now() - new Date('2018-05-20').getTime() > 0).toBe(true); _1.setDate(0); expect(Date.now()).toBe(0); }); it('setDate can accept a Date instead of a number', function () { _1.setDate(new Date(2018, 5, 27, 0, 0, 0)); var now = Date.now(); // 0 timezone + timezone offset expect(now).toBe(1530057600000 + new Date().getTimezoneOffset() * 60000); _1.advanceDate(3000); expect(Date.now() - now).toBe(3000); _1.advanceDate(-1000); expect(Date.now() - now).toBe(2000); }); }); //# sourceMappingURL=index.spec.js.map