-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[@mantine/hooks] use-media-query: Add initial value support (#1244)
* Add default value to use-media-query for ssr * [@mantine/hooks] use-media-query: Update docs to show usage for ssr * [@mantine/hooks] use-media-query: Replace SSR demo with code sample * [@mantine/hooks] use-media-query: Run prettier:write
- Loading branch information
1 parent
8516a5c
commit 6251998
Showing
3 changed files
with
78 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
42 changes: 42 additions & 0 deletions
42
src/mantine-hooks/src/use-media-query/use-media-query.test.ts
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 { useMediaQuery } from './use-media-query'; | ||
|
||
describe('@mantine/hooks/use-media-query', () => { | ||
beforeEach(() => { | ||
const mediaMatches = { | ||
'(min-width: 500px)': true, | ||
'(min-width: 1000px)': false, | ||
}; | ||
window.matchMedia = (query) => | ||
({ | ||
matches: mediaMatches[query] ?? false, | ||
addListener: jest.fn(), | ||
removeListener: jest.fn(), | ||
} as any); | ||
}); | ||
it('should return true if media query matches', () => { | ||
const { result } = renderHook(() => useMediaQuery('(min-width: 500px)')); | ||
expect(result.current).toBe(true); | ||
}); | ||
it('should return false if media query does not match', () => { | ||
const { result } = renderHook(() => useMediaQuery('(min-width: 1200px)')); | ||
expect(result.current).toBe(false); | ||
}); | ||
it('should return default state before hydration', () => { | ||
const { result } = renderHookSSR(() => useMediaQuery('(min-width: 500px)', false)); | ||
expect(result.current).toBe(false); | ||
}); | ||
it('should return media query result after hydration 500px', async () => { | ||
const { result, hydrate } = renderHookSSR(() => useMediaQuery('(min-width: 500px)', false)); | ||
expect(result.current).toBe(false); | ||
hydrate(); | ||
expect(result.current).toBe(true); | ||
}); | ||
it('should return media query result after hydration 1200px', async () => { | ||
const { result, hydrate } = renderHookSSR(() => useMediaQuery('(min-width: 1200px)', true)); | ||
expect(result.current).toBe(true); | ||
hydrate(); | ||
expect(result.current).toBe(false); | ||
}); | ||
}); |
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