-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(useOrientationChange): rm useGlobalEventListener; make SSR r…
…eady (#7811) h2. Описание - SSR: инициализируем как `'portrait'`, а в эффекте меняем значение на актуальное - useGlobalEventListener: избавляемся, чтобы облегчить хук от лишних операций - покрываем тестами - реэкспоритурем тип `Orientation` h2. Release notes h2. Улучшения - useOrientationChange: - поправлена проблема с гидрациией при SSR; - добавлен экспорт типа `Orientation`.
- Loading branch information
Showing
3 changed files
with
58 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { useOrientationChange } from './useOrientationChange'; | ||
|
||
const mockScreenOrientation = (angle = 0, useDeprecated = false) => { | ||
if (useDeprecated) { | ||
Object.defineProperty(window, 'orientation', { | ||
writable: true, | ||
value: angle, | ||
}); | ||
} | ||
|
||
Object.defineProperty(window.screen, 'orientation', { | ||
writable: true, | ||
value: useDeprecated ? undefined : { angle }, | ||
}); | ||
}; | ||
|
||
describe(useOrientationChange, () => { | ||
it.each([ | ||
{ angle: 0, type: 'portrait', useDeprecated: false }, | ||
{ angle: 90, type: 'landscape', useDeprecated: false }, | ||
{ angle: 0, type: 'portrait', useDeprecated: true }, | ||
{ angle: 90, type: 'landscape', useDeprecated: true }, | ||
])( | ||
'returns $type if angle is $angle (useDeprecated="$useDeprecated")', | ||
({ angle, type, useDeprecated }) => { | ||
mockScreenOrientation(angle, useDeprecated); | ||
|
||
const { result } = renderHook(useOrientationChange); | ||
expect(result.current).toBe(type); | ||
}, | ||
); | ||
}); |
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