|
| 1 | +import React from 'react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import { IntlProvider } from '@edx/frontend-platform/i18n'; |
| 4 | +import { render, screen } from '@testing-library/react'; |
| 5 | + |
| 6 | +import TypeXToConfirmModal from './TypeXToConfirmModal'; |
| 7 | + |
| 8 | +const defaultProps = () => ({ |
| 9 | + label: 'Delete item', |
| 10 | + bodyText: 'Dangerous action', |
| 11 | + confirmLabel: 'Delete', |
| 12 | + cancelLabel: 'Cancel', |
| 13 | + X: 'DELETE', |
| 14 | + confirmPayload: { id: 7 }, |
| 15 | + isOpen: true, |
| 16 | + onConfirm: jest.fn(), |
| 17 | + onCancel: jest.fn(), |
| 18 | + setConfirmPayload: jest.fn(), |
| 19 | +}); |
| 20 | + |
| 21 | +const renderModal = (props = defaultProps()) => |
| 22 | + render( |
| 23 | + <IntlProvider locale="en" messages={{}}> |
| 24 | + <TypeXToConfirmModal {...props} /> |
| 25 | + </IntlProvider>, |
| 26 | + ); |
| 27 | + |
| 28 | +describe('TypeXToConfirmModal', () => { |
| 29 | + it('renders the required confirmation phrase with strong emphasis', () => { |
| 30 | + renderModal(); |
| 31 | + |
| 32 | + expect(screen.getByText('DELETE', { selector: 'strong' })).toBeInTheDocument(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('keeps the destructive confirm button disabled until the typed value exactly matches the required confirmation phrase', async () => { |
| 36 | + const user = userEvent.setup(); |
| 37 | + renderModal(); |
| 38 | + |
| 39 | + const input = screen.getByRole('textbox'); |
| 40 | + const confirmButton = screen.getByRole('button', { name: 'Delete' }); |
| 41 | + |
| 42 | + expect(confirmButton).toBeDisabled(); |
| 43 | + await user.type(input, 'DEL'); |
| 44 | + expect(confirmButton).toBeDisabled(); |
| 45 | + await user.type(input, 'ETE'); |
| 46 | + expect(confirmButton).toBeEnabled(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('does not enable confirmation for partial, differently cased, or whitespace-padded confirmation text', async () => { |
| 50 | + const user = userEvent.setup(); |
| 51 | + renderModal(); |
| 52 | + |
| 53 | + const input = screen.getByRole('textbox'); |
| 54 | + const confirmButton = screen.getByRole('button', { name: 'Delete' }); |
| 55 | + |
| 56 | + for (const value of ['DEL', 'delete', ' DELETE', 'DELETE ', ' Delete ']) { |
| 57 | + await user.clear(input); |
| 58 | + await user.type(input, value); |
| 59 | + expect(confirmButton).toBeDisabled(); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + it('requires explicit activation of the enabled destructive confirm button', async () => { |
| 64 | + const user = userEvent.setup(); |
| 65 | + const props = defaultProps(); |
| 66 | + renderModal(props); |
| 67 | + |
| 68 | + const input = screen.getByRole('textbox'); |
| 69 | + const confirmButton = screen.getByRole('button', { name: 'Delete' }); |
| 70 | + |
| 71 | + await user.click(input); |
| 72 | + await user.keyboard('{Enter}'); |
| 73 | + expect(props.onConfirm).not.toHaveBeenCalled(); |
| 74 | + |
| 75 | + await user.type(input, 'DELETE'); |
| 76 | + await user.keyboard('{Enter}'); |
| 77 | + expect(props.onConfirm).not.toHaveBeenCalled(); |
| 78 | + |
| 79 | + await user.click(confirmButton); |
| 80 | + expect(props.onConfirm).toHaveBeenCalledWith(props.confirmPayload); |
| 81 | + }); |
| 82 | + |
| 83 | + it('resets confirmation state when the modal closes so a reopened dialog starts disabled again', async () => { |
| 84 | + const user = userEvent.setup(); |
| 85 | + const props = defaultProps(); |
| 86 | + const { rerender } = renderModal(props); |
| 87 | + |
| 88 | + await user.type(screen.getByRole('textbox'), 'DELETE'); |
| 89 | + expect(screen.getByRole('button', { name: 'Delete' })).toBeEnabled(); |
| 90 | + |
| 91 | + rerender( |
| 92 | + <IntlProvider locale="en" messages={{}}> |
| 93 | + <TypeXToConfirmModal {...props} isOpen={false} /> |
| 94 | + </IntlProvider>, |
| 95 | + ); |
| 96 | + |
| 97 | + rerender( |
| 98 | + <IntlProvider locale="en" messages={{}}> |
| 99 | + <TypeXToConfirmModal {...props} isOpen /> |
| 100 | + </IntlProvider>, |
| 101 | + ); |
| 102 | + |
| 103 | + expect(screen.getByRole('button', { name: 'Delete' })).toBeDisabled(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('clears the provided confirm payload when the modal is closed without confirming', () => { |
| 107 | + const props = defaultProps(); |
| 108 | + const { rerender } = renderModal(props); |
| 109 | + |
| 110 | + rerender( |
| 111 | + <IntlProvider locale="en" messages={{}}> |
| 112 | + <TypeXToConfirmModal {...props} isOpen={false} /> |
| 113 | + </IntlProvider>, |
| 114 | + ); |
| 115 | + |
| 116 | + expect(props.setConfirmPayload).toHaveBeenCalledWith(null); |
| 117 | + expect(props.onConfirm).not.toHaveBeenCalled(); |
| 118 | + }); |
| 119 | +}); |
0 commit comments