@shopify/jest-dom-mocks
Version:
Jest mocking utilities for working with the DOM
47 lines (40 loc) • 1.28 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var FakePromise = require('promise');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var FakePromise__default = /*#__PURE__*/_interopDefaultLegacy(FakePromise);
class Promise$1 {
constructor() {
this.originalPromise = global.Promise;
this.isUsingFakePromise = false;
}
mock() {
if (this.isUsingFakePromise) {
throw new Error('Promise is already mocked, but you tried to mock it again.');
}
jest.useFakeTimers();
global.Promise = FakePromise__default["default"];
this.isUsingFakePromise = true;
}
restore() {
if (!this.isUsingFakePromise) {
throw new Error('Promise is already real, but you tried to restore it again.');
}
jest.useRealTimers();
global.Promise = this.originalPromise;
this.isUsingFakePromise = false;
}
isMocked() {
return this.isUsingFakePromise;
}
runPending() {
this.ensureUsingFakePromise();
jest.runAllTimers();
}
ensureUsingFakePromise() {
if (!this.isUsingFakePromise) {
throw new Error('You must call Promise.mock() before interacting with the mock Promise.');
}
}
}
exports["default"] = Promise$1;