1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | function noop() { }
|
5 | var MatchMedia = (function () {
|
6 | function MatchMedia() {
|
7 | this.isUsingMockMatchMedia = false;
|
8 | }
|
9 | MatchMedia.prototype.mock = function (media) {
|
10 | if (media === void 0) { media = defaultMatcher; }
|
11 | if (this.isUsingMockMatchMedia) {
|
12 | throw new Error('You tried to mock window.matchMedia when it was already mocked.');
|
13 | }
|
14 | this.originalMatchMedia = window.matchMedia;
|
15 | this.isUsingMockMatchMedia = true;
|
16 | this.setMedia(media);
|
17 | };
|
18 | MatchMedia.prototype.restore = function () {
|
19 | if (!this.isUsingMockMatchMedia) {
|
20 | throw new Error('You tried to restore window.matchMedia when it was already restored.');
|
21 | }
|
22 | window.matchMedia = this.originalMatchMedia;
|
23 | this.isUsingMockMatchMedia = false;
|
24 | };
|
25 | MatchMedia.prototype.isMocked = function () {
|
26 | return this.isUsingMockMatchMedia;
|
27 | };
|
28 | MatchMedia.prototype.setMedia = function (media) {
|
29 | if (media === void 0) { media = defaultMatcher; }
|
30 | this.ensureMatchMediaIsMocked();
|
31 | window.matchMedia = function (query) { return mediaQueryList(media(query)); };
|
32 | };
|
33 | MatchMedia.prototype.ensureMatchMediaIsMocked = function () {
|
34 | if (!this.isUsingMockMatchMedia) {
|
35 | throw new Error('You must call matchMedia.mock() before interacting with the mock matchMedia.');
|
36 | }
|
37 | };
|
38 | return MatchMedia;
|
39 | }());
|
40 | exports.default = MatchMedia;
|
41 | function defaultMatcher() {
|
42 | return {
|
43 | media: '',
|
44 | addListener: noop,
|
45 | addEventListener: noop,
|
46 | removeListener: noop,
|
47 | removeEventListener: noop,
|
48 | onchange: noop,
|
49 | dispatchEvent: function () { return false; },
|
50 | matches: false,
|
51 | };
|
52 | }
|
53 | function mediaQueryList(values) {
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | return tslib_1.__assign({}, defaultMatcher(), values);
|
62 | }
|
63 | exports.mediaQueryList = mediaQueryList;
|