-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: useMedia SSR hydration bug with defaultState (#2216)
* fix: useMedia SSR hydration bug with defaultState Prevent a React hydration mismatch when a default value is provided by not defaulting to window.matchMedia(query).matches. * Refactor nested ifs
- Loading branch information
1 parent
46b401f
commit 5c01189
Showing
3 changed files
with
66 additions
and
4 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
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { renderHook as renderHookSSR } from '@testing-library/react-hooks/server'; | ||
import { useMedia } from '../src'; | ||
|
||
const createMockMediaMatcher = (matches: Record<string, boolean>) => (qs: string) => ({ | ||
matches: matches[qs] ?? false, | ||
addListener: () => {}, | ||
removeListener: () => {}, | ||
}); | ||
|
||
describe('useMedia', () => { | ||
beforeEach(() => { | ||
window.matchMedia = createMockMediaMatcher({ | ||
'(min-width: 500px)': true, | ||
'(min-width: 1000px)': false, | ||
}) as any; | ||
}); | ||
it('should return true if media query matches', () => { | ||
const { result } = renderHook(() => useMedia('(min-width: 500px)')); | ||
expect(result.current).toBe(true); | ||
}); | ||
it('should return false if media query does not match', () => { | ||
const { result } = renderHook(() => useMedia('(min-width: 1200px)')); | ||
expect(result.current).toBe(false); | ||
}); | ||
it('should return default state before hydration', () => { | ||
const { result } = renderHookSSR(() => useMedia('(min-width: 500px)', false)); | ||
expect(result.current).toBe(false); | ||
}); | ||
it('should return media query result after hydration', async () => { | ||
const { result, hydrate } = renderHookSSR(() => useMedia('(min-width: 500px)', false)); | ||
expect(result.current).toBe(false); | ||
hydrate(); | ||
expect(result.current).toBe(true); | ||
}); | ||
it('should return media query result after hydration', async () => { | ||
const { result, hydrate } = renderHookSSR(() => useMedia('(min-width: 1200px)', true)); | ||
expect(result.current).toBe(true); | ||
hydrate(); | ||
expect(result.current).toBe(false); | ||
}); | ||
}); |