-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(use-media-query): no longer supports Safari < 14 (#1782)
* fix: remove unused files * refactor: update comment for the `calcMaxWidth` function * feat: 🎸 useMediaQuery hook no longer supports Safari < 14 BREAKING CHANGE: 🧨 useMediaQuery hook no longer supports Safari < 14 * test: add `istanbul ignore` comment * refactor(use-media-query): replace state with matches * test: add `instnbul ignore` comment
- Loading branch information
Showing
4 changed files
with
71 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,70 @@ | ||
// @vitest-environment jsdom | ||
|
||
import { describe, beforeEach, vi, it, expect } from 'vitest'; | ||
import { renderHook } from '@testing-library/react'; | ||
import { describe, beforeAll, beforeEach, vi, it, expect } from 'vitest'; | ||
import { configure, renderHook } from '@testing-library/react'; | ||
import { useMediaQuery } from './index'; | ||
|
||
describe('useMediaQuery hook', () => { | ||
// Arrange | ||
const matches = { | ||
'(min-width: 500px)': true, | ||
'(min-width: 1000px)': false, | ||
}; | ||
let matches = null; | ||
|
||
describe('In Safari Browser less than v14', () => { | ||
// Arrange | ||
beforeEach(() => { | ||
window.matchMedia = vi.fn((query) => ({ | ||
matches: matches[query] || false, | ||
media: query, | ||
onchange: null, | ||
addListener: vi.fn(), | ||
removeListener: vi.fn(), | ||
})); | ||
}); | ||
beforeAll(() => { | ||
configure({ reactStrictMode: true }); | ||
|
||
it('returns true if media query matches', () => { | ||
// Act | ||
const { result } = renderHook(() => useMediaQuery('(min-width: 500px)')); | ||
|
||
// Assert | ||
expect(result.current).toBe(true); | ||
}); | ||
|
||
it('returns false if media query does not match', () => { | ||
// Act | ||
const { result } = renderHook(() => useMediaQuery('(min-width: 1200px)')); | ||
|
||
// Assert | ||
expect(result.current).toBe(false); | ||
}); | ||
|
||
it('updates when media query changes', () => { | ||
// Act | ||
const { result, rerender } = renderHook( | ||
({ query }) => useMediaQuery(query), | ||
{ initialProps: { query: '(min-width: 500px)' } } | ||
); | ||
|
||
// Assert | ||
expect(result.current).toBe(true); | ||
|
||
// Act | ||
rerender({ query: '(max-width: 800px)' }); | ||
|
||
// Assert | ||
expect(result.current).toBe(false); | ||
}); | ||
matches = { | ||
'(min-width: 500px)': true, | ||
'(min-width: 1000px)': false, | ||
}; | ||
}); | ||
|
||
describe('In other browsers', () => { | ||
// Arrange | ||
beforeEach(() => { | ||
window.matchMedia = vi.fn((query) => ({ | ||
matches: matches[query] || false, | ||
media: query, | ||
onchange: null, | ||
addEventListener: vi.fn(), | ||
removeEventListener: vi.fn(), | ||
})); | ||
}); | ||
// Arrange | ||
beforeEach(() => { | ||
window.matchMedia = vi.fn((query) => ({ | ||
matches: matches[query] || false, | ||
media: query, | ||
onchange: null, | ||
addEventListener: vi.fn(), | ||
removeEventListener: vi.fn(), | ||
})); | ||
}); | ||
|
||
it('returns true if media query matches', () => { | ||
// Act | ||
const { result } = renderHook(() => useMediaQuery('(min-width: 500px)')); | ||
it('returns true if media query matches', () => { | ||
// Act | ||
const { result, unmount } = renderHook(() => | ||
useMediaQuery('(min-width: 500px)') | ||
); | ||
|
||
// Assert | ||
expect(result.current).toBe(true); | ||
}); | ||
// Assert | ||
expect(result.current).toBe(true); | ||
unmount(); | ||
}); | ||
|
||
it('returns false if media query does not match', () => { | ||
// Act | ||
const { result } = renderHook(() => useMediaQuery('(min-width: 1200px)')); | ||
it('returns false if media query does not match', () => { | ||
// Act | ||
const { result, unmount } = renderHook(() => | ||
useMediaQuery('(min-width: 1200px)') | ||
); | ||
|
||
// Assert | ||
expect(result.current).toBe(false); | ||
}); | ||
// Assert | ||
expect(result.current).toBe(false); | ||
unmount(); | ||
}); | ||
|
||
it('updates when media query changes', () => { | ||
// Act | ||
const { result, rerender } = renderHook( | ||
({ query }) => useMediaQuery(query), | ||
{ initialProps: { query: '(min-width: 500px)' } } | ||
); | ||
it('updates when media query changes', () => { | ||
// Act | ||
const { result, rerender, unmount } = renderHook( | ||
({ query }) => useMediaQuery(query), | ||
{ initialProps: { query: '(min-width: 500px)' } } | ||
); | ||
|
||
// Assert | ||
expect(result.current).toBe(true); | ||
// Assert | ||
expect(result.current).toBe(true); | ||
|
||
// Act | ||
rerender({ query: '(max-width: 800px)' }); | ||
// Act | ||
rerender({ query: '(max-width: 800px)' }); | ||
|
||
// Assert | ||
expect(result.current).toBe(false); | ||
}); | ||
// Assert | ||
expect(result.current).toBe(false); | ||
unmount(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1322,6 +1322,11 @@ | |
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.5.6.tgz#c65b2bfce1bec346582c07724e3f8c1017a20339" | ||
integrity sha512-dn1l8LaMea/IjDoHNd9J52uBbInB796CDffS6VdIxvqYCPSG0V0DzHp76GpaWnlhg88uYyPbXCDIowa86ybd5A== | ||
|
||
"@types/unist@^2.0.0": | ||
version "2.0.10" | ||
resolved "https://registry.yarnpkg.com/@types/unist/-/unist-2.0.10.tgz#04ffa7f406ab628f7f7e97ca23e290cd8ab15efc" | ||
integrity sha512-IfYcSBWE3hLpBg8+X2SEa8LVkJdJEkT2Ese2aaLs3ptGdVtABxndrMaxuFlQ1qdFf9Q5rDvDpxI3WwgvKFAsQA== | ||
|
||
"@typescript-eslint/[email protected]": | ||
version "6.21.0" | ||
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-6.21.0.tgz#ea8a9bfc8f1504a6ac5d59a6df308d3a0630a2b1" | ||
|