Skip to content

Commit 69176f8

Browse files
authored
fix(mock-doc): add missing properties of object returned by matchMedia (#2880)
matchMedia is supposed to return a MediaQueryList which inherits from the EventTarget interface. As such the stub object returned from the mock should have addEventListener, removeEventListener, and dispatchEvent methods. It was also missing the media property.
1 parent 0b42ed4 commit 69176f8

3 files changed

Lines changed: 41 additions & 2 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { MockWindow } from '../window';
2+
3+
describe('matchMedia', () => {
4+
let win: MockWindow;
5+
let media: ReturnType<MockWindow['matchMedia']>;
6+
beforeEach(() => {
7+
win = new MockWindow(`
8+
<html>
9+
<head>
10+
</head>
11+
</head>
12+
`);
13+
media = win.matchMedia('(prefers-color-scheme: dark)');
14+
});
15+
16+
it('MediaQueryList.media', () => {
17+
expect(media.media).toBe('(prefers-color-scheme: dark)');
18+
});
19+
it('MediaQueryList.matches', () => {
20+
expect(media.matches).toBe(false);
21+
});
22+
it('MediaQueryList.addEventListener', () => {
23+
expect(media.addEventListener).toBeDefined();
24+
});
25+
it('MediaQueryList.dispatchEvent', () => {
26+
expect(media.dispatchEvent).toBeDefined();
27+
});
28+
it('MediaQueryList.removeEventListener', () => {
29+
expect(media.removeEventListener).toBeDefined();
30+
});
31+
it('MediaQueryList.onchange', () => {
32+
expect(media.onchange).toBe(null);
33+
});
34+
});

src/mock-doc/window.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,14 @@ export class MockWindow {
337337
}
338338
}
339339

340-
matchMedia() {
340+
matchMedia(media: string) {
341341
return {
342+
media,
342343
matches: false,
344+
addEventListener,
345+
dispatchEvent,
346+
removeEventListener,
347+
onchange: null as ((this: MediaQueryList, ev: MediaQueryListEvent) => any) | null,
343348
};
344349
}
345350

test/karma/test-app/slot-no-default/karma.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('slot-no-default', function () {
99
});
1010
afterEach(tearDownDom);
1111

12-
it('only renders slots that havea location', async () => {
12+
it('only renders slots that have a location', async () => {
1313
const root = app.querySelector('slot-no-default');
1414

1515
const a = root.querySelector('a');

0 commit comments

Comments
 (0)