Skip to content

Commit f64b209

Browse files
committed
Better manual mocks
1 parent 17c4b40 commit f64b209

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"scripts": {
2727
"start": "tsdx watch",
2828
"build": "tsdx build",
29-
"test": "tsdx test --passWithNoTests",
29+
"test": "tsdx test",
3030
"lint": "tsdx lint",
3131
"prepare": "tsdx build",
3232
"size": "size-limit",

src/mocks/resize-observer.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const state: State = {
88
nodeObservers: new Map(),
99
};
1010

11-
class MockedResizeObserver {
11+
class MockedResizeObserver implements ResizeObserver {
1212
nodes: HTMLElement[] = [];
1313
callback: ResizeObserverCallback;
1414

@@ -64,9 +64,12 @@ class MockedResizeObserver {
6464
};
6565
}
6666

67-
function elementToEntry(element: HTMLElement) {
67+
function elementToEntry(element: HTMLElement): ResizeObserverEntry {
6868
const boundingClientRect = element.getBoundingClientRect();
6969

70+
// @ts-ignore
71+
// for some reason, the typescript type definition for ResizeObserverEntry
72+
// is mismatched between tsdx typecheck and vscode typecheck
7073
return {
7174
borderBoxSize: [
7275
{
@@ -81,14 +84,25 @@ function elementToEntry(element: HTMLElement) {
8184
},
8285
],
8386
contentRect: boundingClientRect,
87+
// devicePixelContentBoxSize: [
88+
// // assume device pixel ratio of 1
89+
// {
90+
// blockSize: boundingClientRect.width,
91+
// inlineSize: boundingClientRect.height,
92+
// },
93+
// ],
8494
target: element,
8595
};
8696
}
8797

8898
function mockResizeObserver() {
8999
const savedImplementation = window.ResizeObserver;
90100

91-
window.ResizeObserver = MockedResizeObserver;
101+
Object.defineProperty(window, 'ResizeObserver', {
102+
writable: true,
103+
configurable: true,
104+
value: MockedResizeObserver,
105+
});
92106

93107
afterAll(() => {
94108
window.ResizeObserver = savedImplementation;

src/mocks/viewport.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,29 @@ function mockViewport(desc: ViewportDescription): MockViewport {
5252
state.listenerHandlers.splice(index, 1);
5353
};
5454

55-
window.matchMedia = jest.fn().mockImplementation(query => ({
56-
get matches() {
57-
return mediaQuery.match(query, state.currentDesc);
58-
},
59-
media: query,
60-
onchange: null,
61-
addListener, // deprecated
62-
removeListener, // deprecated
63-
addEventListener: (eventType: string, handler: Handler) => {
64-
if (eventType === 'change') {
65-
addListener(handler);
66-
}
67-
},
68-
removeEventListener: (eventType: string, handler: Handler) => {
69-
if (eventType === 'change') {
70-
removeListener(handler);
71-
}
72-
},
73-
dispatchEvent: jest.fn(),
74-
}));
55+
Object.defineProperty(window, 'matchMedia', {
56+
writable: true,
57+
value: jest.fn().mockImplementation(query => ({
58+
get matches() {
59+
return mediaQuery.match(query, state.currentDesc);
60+
},
61+
media: query,
62+
onchange: null,
63+
addListener, // deprecated
64+
removeListener, // deprecated
65+
addEventListener: (eventType: string, handler: Handler) => {
66+
if (eventType === 'change') {
67+
addListener(handler);
68+
}
69+
},
70+
removeEventListener: (eventType: string, handler: Handler) => {
71+
if (eventType === 'change') {
72+
removeListener(handler);
73+
}
74+
},
75+
dispatchEvent: jest.fn(),
76+
})),
77+
});
7578

7679
return {
7780
cleanup: () => {

0 commit comments

Comments
 (0)