Skip to content

Commit

Permalink
refactor(useDevicePixelRatio.test.ts): use stubglobal method to defin…
Browse files Browse the repository at this point in the history
…e devicepixelratio in tests
  • Loading branch information
rupeq committed Feb 2, 2025
1 parent 3dd1a91 commit 820d12e
Showing 1 changed file with 12 additions and 42 deletions.
54 changes: 12 additions & 42 deletions src/hooks/useDevicePixelRatio/useDevicePixelRatio.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,24 @@ import { useDevicePixelRatio } from './useDevicePixelRatio';

describe('useDevicePixelRatio', () => {
describe('in unsupported environment', () => {
it('should return supported as false when devicePixelRatio is not present', () => {
const originalDPR = window.devicePixelRatio;
Object.defineProperty(window, 'devicePixelRatio', {
value: undefined,
configurable: true
});
afterEach(() => void vi.unstubAllGlobals());

it('should return supported as false when devicePixelRatio is not present', () => {
vi.stubGlobal('devicePixelRatio', undefined);
const { result } = renderHook(() => useDevicePixelRatio());
expect(result.current.supported).toBeFalsy();
expect(result.current.ratio).toBeNull();

Object.defineProperty(window, 'devicePixelRatio', {
value: originalDPR,
configurable: true
});
});

it('should return supported as false when matchMedia is not a function', () => {
const originalMatchMedia = window.matchMedia;
Object.defineProperty(window, 'matchMedia', {
value: undefined,
writable: true
});

vi.stubGlobal('matchMedia', undefined);
const { result } = renderHook(() => useDevicePixelRatio());
expect(result.current.supported).toBeFalsy();
expect(result.current.ratio).toBeNull();

Object.defineProperty(window, 'matchMedia', {
value: originalMatchMedia,
writable: true
});
});
});

describe('in supported environment', () => {
let originalMatchMedia: typeof window.matchMedia;
let originalDPR: number;
const mediaQueryListMock = {
matches: false,
onchange: null,
Expand All @@ -53,26 +33,16 @@ describe('useDevicePixelRatio', () => {
};

beforeEach(() => {
originalMatchMedia = window.matchMedia;
originalDPR = window.devicePixelRatio;

Object.defineProperty(window, 'devicePixelRatio', {
value: 2,
configurable: true
});

window.matchMedia = vi
.fn<[string], MediaQueryList>()
.mockImplementation((query) => ({ ...mediaQueryListMock, media: query }));
vi.stubGlobal('devicePixelRatio', 2);
vi.stubGlobal(
'matchMedia',
vi
.fn<[string], MediaQueryList>()
.mockImplementation((query) => ({ ...mediaQueryListMock, media: query }))
);
});

afterEach(() => {
window.matchMedia = originalMatchMedia;
Object.defineProperty(window, 'devicePixelRatio', {
value: originalDPR,
configurable: true
});
});
afterEach(() => void vi.unstubAllGlobals());

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

0 comments on commit 820d12e

Please sign in to comment.