|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, expect, it, jest } from '@jest/globals'; |
| 6 | +import { act, render } from '@testing-library/react-native'; |
| 7 | +import * as React from 'react'; |
| 8 | +import { NativeSafeAreaProvider } from '../NativeSafeAreaProvider.web'; |
| 9 | +import type { InsetChangeNativeCallback } from '../SafeArea.types'; |
| 10 | + |
| 11 | +const setDocumentDimensions = (width: number, height: number) => { |
| 12 | + Object.defineProperty(document.documentElement, 'offsetWidth', { |
| 13 | + configurable: true, |
| 14 | + value: width, |
| 15 | + }); |
| 16 | + Object.defineProperty(document.documentElement, 'offsetHeight', { |
| 17 | + configurable: true, |
| 18 | + value: height, |
| 19 | + }); |
| 20 | +}; |
| 21 | + |
| 22 | +const mockComputedPadding = (padding: { |
| 23 | + paddingTop: string; |
| 24 | + paddingBottom: string; |
| 25 | + paddingLeft: string; |
| 26 | + paddingRight: string; |
| 27 | +}) => { |
| 28 | + jest |
| 29 | + .spyOn(window, 'getComputedStyle') |
| 30 | + .mockReturnValue(padding as unknown as CSSStyleDeclaration); |
| 31 | +}; |
| 32 | + |
| 33 | +describe('NativeSafeAreaProvider.web', () => { |
| 34 | + it('updates insets and frame on window resize', () => { |
| 35 | + setDocumentDimensions(800, 600); |
| 36 | + mockComputedPadding({ |
| 37 | + paddingTop: '20px', |
| 38 | + paddingBottom: '10px', |
| 39 | + paddingLeft: '0px', |
| 40 | + paddingRight: '0px', |
| 41 | + }); |
| 42 | + const onInsetsChange = jest.fn<InsetChangeNativeCallback>(); |
| 43 | + render(<NativeSafeAreaProvider onInsetsChange={onInsetsChange} />); |
| 44 | + expect(onInsetsChange).toHaveBeenCalledWith({ |
| 45 | + nativeEvent: { |
| 46 | + insets: { top: 20, bottom: 10, left: 0, right: 0 }, |
| 47 | + frame: { x: 0, y: 0, width: 800, height: 600 }, |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + onInsetsChange.mockClear(); |
| 52 | + setDocumentDimensions(1024, 768); |
| 53 | + mockComputedPadding({ |
| 54 | + paddingTop: '40px', |
| 55 | + paddingBottom: '10px', |
| 56 | + paddingLeft: '0px', |
| 57 | + paddingRight: '0px', |
| 58 | + }); |
| 59 | + act(() => { |
| 60 | + window.dispatchEvent(new Event('resize')); |
| 61 | + }); |
| 62 | + expect(onInsetsChange).toHaveBeenCalledWith({ |
| 63 | + nativeEvent: { |
| 64 | + insets: { top: 40, bottom: 10, left: 0, right: 0 }, |
| 65 | + frame: { x: 0, y: 0, width: 1024, height: 768 }, |
| 66 | + }, |
| 67 | + }); |
| 68 | + }); |
| 69 | +}); |
0 commit comments