UNPKG

1.66 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var lolex_1 = tslib_1.__importDefault(require("lolex"));
5var Clock = /** @class */ (function () {
6 function Clock() {
7 this.isUsingMockClock = false;
8 }
9 Clock.prototype.mock = function (now) {
10 if (now === void 0) { now = Date.now(); }
11 if (this.isUsingMockClock) {
12 throw new Error('The clock is already mocked, but you tried to mock it again.');
13 }
14 this.isUsingMockClock = true;
15 this.mockClock = lolex_1.default.install({ now: now });
16 };
17 Clock.prototype.restore = function () {
18 if (!this.isUsingMockClock) {
19 throw new Error('The clock is already real, but you tried to restore it again.');
20 }
21 this.isUsingMockClock = false;
22 if (this.mockClock) {
23 this.mockClock.uninstall();
24 delete this.mockClock;
25 }
26 };
27 Clock.prototype.isMocked = function () {
28 return this.isUsingMockClock;
29 };
30 Clock.prototype.tick = function (time) {
31 this.ensureClockIsMocked();
32 if (this.mockClock) {
33 this.mockClock.tick(time);
34 }
35 };
36 Clock.prototype.setTime = function (time) {
37 this.ensureClockIsMocked();
38 if (this.mockClock) {
39 this.mockClock.setSystemTime(time);
40 }
41 };
42 Clock.prototype.ensureClockIsMocked = function () {
43 if (!this.isUsingMockClock) {
44 throw new Error('You must call clock.mock() before interacting with the mock clock.');
45 }
46 };
47 return Clock;
48}());
49exports.default = Clock;