|
| 1 | +import React from 'react'; |
| 2 | +import { fireEvent, render, screen, waitFor } from '@testing-library/react'; |
| 3 | +import '@testing-library/jest-dom/extend-expect'; |
| 4 | + |
| 5 | +import Popconfirm from '../index'; |
| 6 | + |
| 7 | +describe('Popconfirm', () => { |
| 8 | + test('should render Popconfirm success render', async () => { |
| 9 | + render(<Popconfirm title="Are you sure?">Click me</Popconfirm>); |
| 10 | + fireEvent.click(screen.getByText('Click me')); |
| 11 | + expect(screen.getByText('Are you sure?')).toBeInTheDocument(); |
| 12 | + await waitFor(() => |
| 13 | + expect(document.querySelector('.ant-popover-message')?.firstChild).toHaveAttribute( |
| 14 | + 'data-mock-icon', |
| 15 | + 'InformationFilled' |
| 16 | + ) |
| 17 | + ); |
| 18 | + await waitFor(() => expect(document.querySelector('.ant-popover')).toMatchSnapshot()); |
| 19 | + await waitFor(() => expect(document.querySelector('.dtc-popconfirm')).toBeInTheDocument()); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should show overlay when trigger is clicked', async () => { |
| 23 | + const popconfirm = render( |
| 24 | + <Popconfirm title="code" trigger="click" autoAdjustOverflow={false}> |
| 25 | + <span>show me your code</span> |
| 26 | + </Popconfirm> |
| 27 | + ); |
| 28 | + |
| 29 | + expect(popconfirm.container.querySelector('.ant-popover')).toBe(null); |
| 30 | + |
| 31 | + const triggerNode = popconfirm.container.querySelectorAll('span')[0]; |
| 32 | + fireEvent.click(triggerNode); |
| 33 | + |
| 34 | + await waitFor(() => expect(document.querySelector('.ant-popover')).not.toBeNull()); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should render correctly with warning type and warning icon', async () => { |
| 38 | + render( |
| 39 | + <Popconfirm title="Warning!" type="warning"> |
| 40 | + Click me |
| 41 | + </Popconfirm> |
| 42 | + ); |
| 43 | + fireEvent.click(screen.getByText('Click me')); |
| 44 | + await waitFor(() => expect(screen.getByText('Warning!')).toBeInTheDocument()); |
| 45 | + await waitFor(() => |
| 46 | + expect(document.querySelector('.ant-popover-message')?.firstChild).toHaveAttribute( |
| 47 | + 'data-mock-icon', |
| 48 | + 'WarningFilled' |
| 49 | + ) |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should render correctly with danger type and close icon', async () => { |
| 54 | + render( |
| 55 | + <Popconfirm title="Danger!" type="danger"> |
| 56 | + Click me |
| 57 | + </Popconfirm> |
| 58 | + ); |
| 59 | + fireEvent.click(screen.getByText('Click me')); |
| 60 | + await waitFor(() => expect(screen.getByText('Danger!')).toBeInTheDocument()); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should not render icon when showIcon is false', async () => { |
| 64 | + render( |
| 65 | + <Popconfirm title="No Icon" showIcon={false}> |
| 66 | + Click me |
| 67 | + </Popconfirm> |
| 68 | + ); |
| 69 | + fireEvent.click(screen.getByText('Click me')); |
| 70 | + await waitFor(() => expect(screen.getByText('No Icon')).toBeInTheDocument()); |
| 71 | + await waitFor(() => |
| 72 | + expect(document.querySelector('.ant-popover-message')?.children.length).toBe(1) |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should use custom icon when provided', async () => { |
| 77 | + const CustomIcon = () => <div data-testid="custom-icon">Custom</div>; |
| 78 | + render( |
| 79 | + <Popconfirm title="Custom Icon" icon={<CustomIcon />}> |
| 80 | + Click me |
| 81 | + </Popconfirm> |
| 82 | + ); |
| 83 | + fireEvent.click(screen.getByText('Click me')); |
| 84 | + await waitFor(() => expect(screen.getByTestId('custom-icon')).toBeInTheDocument()); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should trigger onConfirm when OK button is clicked', async () => { |
| 88 | + const onConfirm = jest.fn(); |
| 89 | + render( |
| 90 | + <Popconfirm title="Confirm?" onConfirm={onConfirm}> |
| 91 | + Click me |
| 92 | + </Popconfirm> |
| 93 | + ); |
| 94 | + fireEvent.click(screen.getByText('Click me')); |
| 95 | + fireEvent.click(await screen.findByText('OK')); |
| 96 | + expect(onConfirm).toHaveBeenCalled(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should trigger onCancel when Cancel button is clicked', async () => { |
| 100 | + const onCancel = jest.fn(); |
| 101 | + render( |
| 102 | + <Popconfirm title="Cancel?" onCancel={onCancel}> |
| 103 | + Click me |
| 104 | + </Popconfirm> |
| 105 | + ); |
| 106 | + fireEvent.click(screen.getByText('Click me')); |
| 107 | + fireEvent.click(await screen.findByText('Cancel')); |
| 108 | + expect(onCancel).toHaveBeenCalled(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should apply danger style to OK button for danger type', async () => { |
| 112 | + render( |
| 113 | + <Popconfirm title="Danger!" type="danger"> |
| 114 | + Click me |
| 115 | + </Popconfirm> |
| 116 | + ); |
| 117 | + fireEvent.click(screen.getByText('Click me')); |
| 118 | + const okButton = await screen.findByText('OK'); |
| 119 | + expect(okButton.closest('button')).toHaveClass('ant-btn-dangerous'); |
| 120 | + }); |
| 121 | +}); |
0 commit comments