|
| 1 | +import { useUnmount } from 'ahooks' |
| 2 | +import React from 'react' |
| 3 | +import BaseDrawer from '..' |
| 4 | +import { act, fireEvent, render, sleep } from '../../../../../tests/utils' |
| 5 | + |
| 6 | +describe('BaseModal', () => { |
| 7 | + const clear = async function clear() { |
| 8 | + await act(async () => { |
| 9 | + jest.runAllTimers() |
| 10 | + await sleep() |
| 11 | + }) |
| 12 | + } |
| 13 | + |
| 14 | + it('custom classname should take effect', () => { |
| 15 | + render( |
| 16 | + <BaseDrawer |
| 17 | + drawerProps={{ visible: true, className: 'custom-classname' }} |
| 18 | + />, |
| 19 | + ) |
| 20 | + |
| 21 | + expect(document.body.querySelectorAll('.custom-classname').length).toBe(1) |
| 22 | + }) |
| 23 | + |
| 24 | + it('click children should open modal', async () => { |
| 25 | + const Demo = () => { |
| 26 | + return ( |
| 27 | + <BaseDrawer |
| 28 | + drawerProps={{ className: 'modal-open' }} |
| 29 | + > |
| 30 | + <button className='open-modal-btn'>open</button> |
| 31 | + </BaseDrawer> |
| 32 | + ) |
| 33 | + } |
| 34 | + |
| 35 | + const { container } = render(<Demo />) |
| 36 | + |
| 37 | + fireEvent.click(container.querySelectorAll('.open-modal-btn')[0]) |
| 38 | + |
| 39 | + await clear() |
| 40 | + |
| 41 | + expect(document.body.querySelectorAll('.modal-open')).toHaveLength(1) |
| 42 | + }) |
| 43 | + |
| 44 | + it('默认情况下,点击关闭时应该销毁弹窗内容', async () => { |
| 45 | + const unMountFn = jest.fn() |
| 46 | + |
| 47 | + const Content = () => { |
| 48 | + useUnmount(unMountFn) |
| 49 | + return <>Content</> |
| 50 | + } |
| 51 | + |
| 52 | + const { container } = render( |
| 53 | + <BaseDrawer drawerContent={<Content />}> |
| 54 | + <button className='open-modal-btn'>open</button> |
| 55 | + </BaseDrawer>, |
| 56 | + ) |
| 57 | + |
| 58 | + fireEvent.click(container.querySelectorAll('.open-modal-btn')[0]) |
| 59 | + |
| 60 | + await clear() |
| 61 | + |
| 62 | + // Click the close icon to close |
| 63 | + fireEvent.click(document.body.querySelectorAll('.ant-modal-close')[0]) |
| 64 | + |
| 65 | + await clear() |
| 66 | + |
| 67 | + expect(unMountFn).toHaveBeenCalled() |
| 68 | + }) |
| 69 | + |
| 70 | + it('存在 onClick 时,点击触发器应该触发 onClick 事件,默认不打开弹窗,需手动打开', async () => { |
| 71 | + const onClick = jest.fn() |
| 72 | + const { container } = render( |
| 73 | + <BaseDrawer |
| 74 | + onClick={onClick} |
| 75 | + drawerProps={{ className: 'modal-open' }} |
| 76 | + > |
| 77 | + <button className='open-modal-btn'>open</button> |
| 78 | + </BaseDrawer>, |
| 79 | + ) |
| 80 | + |
| 81 | + fireEvent.click(container.querySelectorAll('.open-modal-btn')[0]) |
| 82 | + |
| 83 | + await clear() |
| 84 | + |
| 85 | + expect(document.body.querySelectorAll('.modal-open')).toHaveLength(0) |
| 86 | + |
| 87 | + expect(onClick).toHaveBeenCalled() |
| 88 | + expect(onClick.mock.calls[0][1]).toEqual({ |
| 89 | + open: expect.any(Function), |
| 90 | + close: expect.any(Function), |
| 91 | + }) |
| 92 | + |
| 93 | + onClick.mockImplementation((_, modalAction) => { |
| 94 | + modalAction.open() |
| 95 | + }) |
| 96 | + |
| 97 | + fireEvent.click(container.querySelectorAll('.open-modal-btn')[0]) |
| 98 | + |
| 99 | + expect(document.body.querySelectorAll('.modal-open')).toHaveLength(1) |
| 100 | + }) |
| 101 | +}) |
0 commit comments