UNPKG

1.42 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var Location = /** @class */ (function () {
4 function Location() {
5 this.isUsingMockLocation = false;
6 }
7 Location.prototype.mock = function () {
8 if (this.isUsingMockLocation) {
9 throw new Error('You tried to mock window.location when it was already mocked.');
10 }
11 // required to make it possible to write to location.search in tests
12 // https://github.com/facebook/jest/issues/890
13 Reflect.defineProperty(window.location, 'search', {
14 writable: true,
15 value: '',
16 });
17 this.assignSpy = jest.spyOn(window.location, 'assign');
18 this.reloadSpy = jest.spyOn(window.location, 'reload');
19 this.replaceSpy = jest.spyOn(window.location, 'replace');
20 this.isUsingMockLocation = true;
21 };
22 Location.prototype.restore = function () {
23 if (!this.isUsingMockLocation) {
24 throw new Error('You tried to restore window.location when it was already restored.');
25 }
26 location.search = '';
27 this.assignSpy.mockRestore();
28 this.reloadSpy.mockRestore();
29 this.replaceSpy.mockRestore();
30 this.isUsingMockLocation = false;
31 };
32 Location.prototype.isMocked = function () {
33 return this.isUsingMockLocation;
34 };
35 return Location;
36}());
37exports.default = Location;