1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var decorators_1 = require("@shopify/decorators");
|
5 | var SupportedDimension;
|
6 | (function (SupportedDimension) {
|
7 | SupportedDimension["OffsetWidth"] = "offsetWidth";
|
8 | SupportedDimension["OffsetHeight"] = "offsetHeight";
|
9 | SupportedDimension["ScrollWidth"] = "scrollWidth";
|
10 | SupportedDimension["ScrollHeight"] = "scrollHeight";
|
11 | })(SupportedDimension || (SupportedDimension = {}));
|
12 | function isGetterFunction(mock) {
|
13 | return mock != null && typeof mock === 'function';
|
14 | }
|
15 | var Dimension = (function () {
|
16 | function Dimension() {
|
17 | this.isUsingMock = false;
|
18 | this.overwrittenImplementations = [];
|
19 | }
|
20 | Dimension.prototype.mock = function (mocks) {
|
21 | if (this.isUsingMock) {
|
22 | throw new Error('Dimensions are already mocked, but you tried to mock them again.');
|
23 | }
|
24 | else if (Object.keys(mocks).length === 0) {
|
25 | throw new Error('No dimensions provided for mocking');
|
26 | }
|
27 | this.mockDOMMethods(mocks);
|
28 | this.isUsingMock = true;
|
29 | };
|
30 | Dimension.prototype.restore = function () {
|
31 | if (!this.isUsingMock) {
|
32 | throw new Error("Dimensions haven't been mocked, but you are trying to restore them.");
|
33 | }
|
34 | this.restoreDOMMethods();
|
35 | this.isUsingMock = false;
|
36 | };
|
37 | Dimension.prototype.isMocked = function () {
|
38 | return this.isUsingMock;
|
39 | };
|
40 | Object.defineProperty(Dimension.prototype, "nativeImplementations", {
|
41 | get: function () {
|
42 | var _a;
|
43 | return _a = {},
|
44 | _a[SupportedDimension.OffsetWidth] = HTMLElement.prototype,
|
45 | _a[SupportedDimension.OffsetHeight] = HTMLElement.prototype,
|
46 | _a[SupportedDimension.ScrollWidth] = Element.prototype,
|
47 | _a[SupportedDimension.ScrollHeight] = Element.prototype,
|
48 | _a;
|
49 | },
|
50 | enumerable: true,
|
51 | configurable: true
|
52 | });
|
53 | Dimension.prototype.mockDOMMethods = function (mocks) {
|
54 | var _this = this;
|
55 | Object.keys(mocks).forEach(function (method) {
|
56 | var nativeSource = _this.nativeImplementations[method];
|
57 | var mock = mocks[method];
|
58 | _this.overwrittenImplementations.push(method);
|
59 | if (isGetterFunction(mock)) {
|
60 | Object.defineProperty(nativeSource, method, {
|
61 | get: function () {
|
62 | return mock.call(this, this);
|
63 | },
|
64 | configurable: true,
|
65 | });
|
66 | }
|
67 | else {
|
68 | Object.defineProperty(nativeSource, method, {
|
69 | value: mocks[method],
|
70 | configurable: true,
|
71 | });
|
72 | }
|
73 | });
|
74 | };
|
75 | Dimension.prototype.restoreDOMMethods = function () {
|
76 | var _this = this;
|
77 | this.overwrittenImplementations.forEach(function (method) {
|
78 | var nativeSource = _this.nativeImplementations[method];
|
79 | if (nativeSource == null) {
|
80 | return;
|
81 | }
|
82 | delete nativeSource[method];
|
83 | });
|
84 | this.overwrittenImplementations = [];
|
85 | };
|
86 | tslib_1.__decorate([
|
87 | decorators_1.memoize()
|
88 | ], Dimension.prototype, "nativeImplementations", null);
|
89 | return Dimension;
|
90 | }());
|
91 | exports.default = Dimension;
|