-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: create unit test for useMobile hook
- Loading branch information
1 parent
8d21645
commit 3e69ce3
Showing
2 changed files
with
51 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import '@testing-library/jest-dom' | ||
import { renderHook } from '@testing-library/react-hooks' | ||
import { useIsMobile } from './useIsMobile' | ||
|
||
describe('useIsMobile', () => { | ||
beforeAll(() => { | ||
Object.defineProperty(window, 'matchMedia', { | ||
writable: true, | ||
value: jest.fn().mockImplementation((query) => ({ | ||
matches: false, | ||
media: query, | ||
onchange: null, | ||
addListener: jest.fn(), | ||
removeListener: jest.fn(), | ||
addEventListener: jest.fn(), | ||
removeEventListener: jest.fn(), | ||
dispatchEvent: jest.fn(), | ||
})), | ||
}) | ||
}) | ||
test('returns true when screen width is less than or equal to mobileScreenSize', () => { | ||
window.matchMedia = jest.fn().mockImplementation((query) => ({ | ||
matches: true, | ||
media: query, | ||
onchange: null, | ||
addListener: jest.fn(), | ||
removeListener: jest.fn(), | ||
addEventListener: jest.fn(), | ||
removeEventListener: jest.fn(), | ||
dispatchEvent: jest.fn(), | ||
})) | ||
const { result } = renderHook(() => useIsMobile()) | ||
expect(result.current).toBe(true) | ||
}) | ||
|
||
test('returns false when screen width is greater than mobileScreenSize', () => { | ||
window.matchMedia = jest.fn().mockImplementation((query) => ({ | ||
matches: false, | ||
media: query, | ||
onchange: null, | ||
addListener: jest.fn(), | ||
removeListener: jest.fn(), | ||
addEventListener: jest.fn(), | ||
removeEventListener: jest.fn(), | ||
dispatchEvent: jest.fn(), | ||
})) | ||
const { result } = renderHook(() => useIsMobile()) | ||
expect(result.current).toBe(false) | ||
}) | ||
}) |