-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into registry-script
- Loading branch information
Showing
13 changed files
with
95 additions
and
53 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
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
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,33 @@ | ||
import { cleanup } from '@testing-library/react'; | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
|
||
import { useSmallScreen } from './useSmallScreen'; | ||
|
||
/** | ||
* We need to mock `matchMedia` because JSON does not | ||
* implements it. | ||
*/ | ||
const mockMatchMedia = (matches: string[]) => | ||
jest.fn().mockImplementation(query => { | ||
return { | ||
matches: matches.includes(query), | ||
}; | ||
}); | ||
|
||
afterEach(cleanup); | ||
|
||
test('check of the value is truthy', () => { | ||
window.matchMedia = mockMatchMedia(['(max-width: 600px)']); | ||
|
||
const { result } = renderHook(() => useSmallScreen()); | ||
expect(result.current).toBeTruthy(); | ||
}); | ||
|
||
it('should verify if window exist', () => { | ||
// mocking if there's no window | ||
Object.defineProperty(global, 'window', { | ||
value: undefined, | ||
}); | ||
const { result } = renderHook(() => useSmallScreen()); | ||
expect(result.current).toBeFalsy(); | ||
}); |
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,26 @@ | ||
import { useCallback, useEffect, useState } from 'react'; | ||
|
||
export const useSmallScreen = (): boolean => { | ||
const getMatches = (): boolean => { | ||
if (typeof window == 'undefined') { | ||
return false; | ||
} | ||
return window.matchMedia('(max-width: 600px)').matches; | ||
}; | ||
|
||
const [matches, setMatches] = useState<boolean>(getMatches()); | ||
|
||
const handleResize = useCallback(() => { | ||
setMatches(getMatches()); | ||
}, []); | ||
useEffect(() => { | ||
// Triggered at the first client-side load and if query changes | ||
handleResize(); | ||
if (typeof window == 'undefined') return; | ||
window.addEventListener('resize', handleResize); | ||
return () => window.removeEventListener('resize', handleResize); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [handleResize]); | ||
|
||
return matches; | ||
}; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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