Skip to content

Commit

Permalink
fix(mock-doc): add missing properties of object returned by matchMedia (
Browse files Browse the repository at this point in the history
#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.
  • Loading branch information
cam-narzt committed Mar 24, 2023
1 parent 0b42ed4 commit 69176f8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
34 changes: 34 additions & 0 deletions src/mock-doc/test/match-media.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { MockWindow } from '../window';

describe('matchMedia', () => {
let win: MockWindow;
let media: ReturnType<MockWindow['matchMedia']>;
beforeEach(() => {
win = new MockWindow(`
<html>
<head>
</head>
</head>
`);
media = win.matchMedia('(prefers-color-scheme: dark)');
});

it('MediaQueryList.media', () => {
expect(media.media).toBe('(prefers-color-scheme: dark)');
});
it('MediaQueryList.matches', () => {
expect(media.matches).toBe(false);
});
it('MediaQueryList.addEventListener', () => {
expect(media.addEventListener).toBeDefined();
});
it('MediaQueryList.dispatchEvent', () => {
expect(media.dispatchEvent).toBeDefined();
});
it('MediaQueryList.removeEventListener', () => {
expect(media.removeEventListener).toBeDefined();
});
it('MediaQueryList.onchange', () => {
expect(media.onchange).toBe(null);
});
});
7 changes: 6 additions & 1 deletion src/mock-doc/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,14 @@ export class MockWindow {
}
}

matchMedia() {
matchMedia(media: string) {
return {
media,
matches: false,
addEventListener,
dispatchEvent,
removeEventListener,
onchange: null as ((this: MediaQueryList, ev: MediaQueryListEvent) => any) | null,
};
}

Expand Down
2 changes: 1 addition & 1 deletion test/karma/test-app/slot-no-default/karma.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('slot-no-default', function () {
});
afterEach(tearDownDom);

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

const a = root.querySelector('a');
Expand Down

0 comments on commit 69176f8

Please sign in to comment.