|
| 1 | +import * as React from 'react' |
| 2 | +import renderer from 'react-test-renderer' |
| 3 | + |
| 4 | +import { createBoundary } from 'utils/ErrorBoundary' |
| 5 | + |
| 6 | +import { FullWidthProvider, Container, useSetFullWidth } from './Container' |
| 7 | + |
| 8 | +const ErrorBoundary = createBoundary(() => (error: Error) => ( |
| 9 | + <span>Error: {error.message}</span> |
| 10 | +)) |
| 11 | + |
| 12 | +const EmptyContainer = () => ( |
| 13 | + <ErrorBoundary> |
| 14 | + <Container>{''}</Container> |
| 15 | + </ErrorBoundary> |
| 16 | +) |
| 17 | + |
| 18 | +describe('components/Layout/Container', () => { |
| 19 | + it('requires Provider', () => { |
| 20 | + jest.spyOn(console, 'error').mockImplementationOnce(jest.fn()) |
| 21 | + const tree = renderer.create(<EmptyContainer />) |
| 22 | + expect(tree).toMatchSnapshot() |
| 23 | + }) |
| 24 | + |
| 25 | + it('has restricted width by default', () => { |
| 26 | + const tree = renderer.create( |
| 27 | + <FullWidthProvider> |
| 28 | + <EmptyContainer /> |
| 29 | + </FullWidthProvider>, |
| 30 | + ) |
| 31 | + expect(tree.root.findByProps({ maxWidth: 'lg' })).toBeTruthy() |
| 32 | + expect(() => tree.root.findByProps({ maxWidth: false })).toThrow() |
| 33 | + expect(tree).toMatchSnapshot() |
| 34 | + }) |
| 35 | + |
| 36 | + it('has full width once set', () => { |
| 37 | + const SetFullWidth = () => { |
| 38 | + useSetFullWidth() |
| 39 | + return <>long content</> |
| 40 | + } |
| 41 | + const tree = renderer.create( |
| 42 | + <FullWidthProvider> |
| 43 | + <Container> |
| 44 | + <SetFullWidth /> |
| 45 | + </Container> |
| 46 | + </FullWidthProvider>, |
| 47 | + ) |
| 48 | + renderer.act(() => {}) |
| 49 | + expect(() => tree.root.findByProps({ maxWidth: 'lg' })).toThrow() |
| 50 | + expect(tree.root.findByProps({ maxWidth: false })).toBeTruthy() |
| 51 | + expect(tree).toMatchSnapshot() |
| 52 | + }) |
| 53 | + |
| 54 | + it('still has full width when other remove full width', () => { |
| 55 | + // TODO: Show some warning or throw error: |
| 56 | + // Some component is designed for narrow layout, |
| 57 | + // but is rendered in `fullWidth`, |
| 58 | + // because there are rendered other components designed for `fullWidth`. |
| 59 | + |
| 60 | + const SetFullWidth = () => { |
| 61 | + useSetFullWidth() |
| 62 | + return <>long content</> |
| 63 | + } |
| 64 | + const UnmountSetFullWidth = () => { |
| 65 | + const [x, setX] = React.useState(true) |
| 66 | + React.useEffect(() => { |
| 67 | + setX(false) |
| 68 | + }, []) |
| 69 | + return x ? <SetFullWidth /> : <>short content</> |
| 70 | + } |
| 71 | + const tree = renderer.create( |
| 72 | + <FullWidthProvider> |
| 73 | + <Container> |
| 74 | + <UnmountSetFullWidth /> |
| 75 | + <SetFullWidth /> |
| 76 | + <UnmountSetFullWidth /> |
| 77 | + <SetFullWidth /> |
| 78 | + <UnmountSetFullWidth /> |
| 79 | + </Container> |
| 80 | + </FullWidthProvider>, |
| 81 | + ) |
| 82 | + renderer.act(() => {}) |
| 83 | + expect(() => tree.root.findByProps({ maxWidth: 'lg' })).toThrow() |
| 84 | + expect(tree.root.findByProps({ maxWidth: false })).toBeTruthy() |
| 85 | + expect(tree).toMatchSnapshot() |
| 86 | + }) |
| 87 | +}) |
0 commit comments