Skip to content

Commit 6d560f4

Browse files
committed
refactor(useDevicePixelRatio.test.ts): use stubglobal method to define devicepixelratio in tests
1 parent fb7a712 commit 6d560f4

File tree

1 file changed

+12
-42
lines changed

1 file changed

+12
-42
lines changed

src/hooks/useDevicePixelRatio/useDevicePixelRatio.test.ts

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,24 @@ import { useDevicePixelRatio } from './useDevicePixelRatio';
44

55
describe('useDevicePixelRatio', () => {
66
describe('in unsupported environment', () => {
7-
it('should return supported as false when devicePixelRatio is not present', () => {
8-
const originalDPR = window.devicePixelRatio;
9-
Object.defineProperty(window, 'devicePixelRatio', {
10-
value: undefined,
11-
configurable: true
12-
});
7+
afterEach(() => void vi.unstubAllGlobals());
138

9+
it('should return supported as false when devicePixelRatio is not present', () => {
10+
vi.stubGlobal('devicePixelRatio', undefined);
1411
const { result } = renderHook(() => useDevicePixelRatio());
1512
expect(result.current.supported).toBeFalsy();
1613
expect(result.current.ratio).toBeNull();
17-
18-
Object.defineProperty(window, 'devicePixelRatio', {
19-
value: originalDPR,
20-
configurable: true
21-
});
2214
});
2315

2416
it('should return supported as false when matchMedia is not a function', () => {
25-
const originalMatchMedia = window.matchMedia;
26-
Object.defineProperty(window, 'matchMedia', {
27-
value: undefined,
28-
writable: true
29-
});
30-
17+
vi.stubGlobal('matchMedia', undefined);
3118
const { result } = renderHook(() => useDevicePixelRatio());
3219
expect(result.current.supported).toBeFalsy();
3320
expect(result.current.ratio).toBeNull();
34-
35-
Object.defineProperty(window, 'matchMedia', {
36-
value: originalMatchMedia,
37-
writable: true
38-
});
3921
});
4022
});
4123

4224
describe('in supported environment', () => {
43-
let originalMatchMedia: typeof window.matchMedia;
44-
let originalDPR: number;
4525
const mediaQueryListMock = {
4626
matches: false,
4727
onchange: null,
@@ -53,26 +33,16 @@ describe('useDevicePixelRatio', () => {
5333
};
5434

5535
beforeEach(() => {
56-
originalMatchMedia = window.matchMedia;
57-
originalDPR = window.devicePixelRatio;
58-
59-
Object.defineProperty(window, 'devicePixelRatio', {
60-
value: 2,
61-
configurable: true
62-
});
63-
64-
window.matchMedia = vi
65-
.fn<[string], MediaQueryList>()
66-
.mockImplementation((query) => ({ ...mediaQueryListMock, media: query }));
36+
vi.stubGlobal('devicePixelRatio', 2);
37+
vi.stubGlobal(
38+
'matchMedia',
39+
vi
40+
.fn<[string], MediaQueryList>()
41+
.mockImplementation((query) => ({ ...mediaQueryListMock, media: query }))
42+
);
6743
});
6844

69-
afterEach(() => {
70-
window.matchMedia = originalMatchMedia;
71-
Object.defineProperty(window, 'devicePixelRatio', {
72-
value: originalDPR,
73-
configurable: true
74-
});
75-
});
45+
afterEach(() => void vi.unstubAllGlobals());
7646

7747
it('should return initial devicePixelRatio and supported', () => {
7848
const { result } = renderHook(() => useDevicePixelRatio());

0 commit comments

Comments
 (0)