extendify
/
tests
/
unit
/
Agent
/
workflows
/
settings
/
components
/
UpdateSettingConfirm.test.jsx
UpdateSettingConfirm.test.jsx in Extendify 3.1.1, at tests/unit/Agent/workflows/settings/components/UpdateSettingConfirm.test.jsx
| 1 | import { UpdateSettingConfirm } from '@agent/workflows/settings/components/UpdateSettingConfirm'; |
| 2 | import { render, screen } from '@testing-library/react'; |
| 3 | import userEvent from '@testing-library/user-event'; |
| 4 | |
| 5 | describe('UpdateSettingConfirm — undo on cancel/unmount', () => { |
| 6 | let header; |
| 7 | |
| 8 | beforeEach(() => { |
| 9 | header = document.createElement('div'); |
| 10 | header.className = 'wp-block-site-title'; |
| 11 | header.textContent = 'Original Title'; |
| 12 | document.body.appendChild(header); |
| 13 | }); |
| 14 | |
| 15 | afterEach(() => { |
| 16 | header.remove(); |
| 17 | }); |
| 18 | |
| 19 | const inputs = { settingName: 'title', newSettingValue: 'New Title' }; |
| 20 | |
| 21 | test('reverts the title when unmounted without confirming', () => { |
| 22 | const { unmount } = render( |
| 23 | <UpdateSettingConfirm |
| 24 | inputs={inputs} |
| 25 | onConfirm={jest.fn()} |
| 26 | onCancel={jest.fn()} |
| 27 | />, |
| 28 | ); |
| 29 | |
| 30 | expect(header.textContent).toBe('New Title'); |
| 31 | unmount(); |
| 32 | expect(header.textContent).toBe('Original Title'); |
| 33 | }); |
| 34 | |
| 35 | test('does not revert the title after confirming', async () => { |
| 36 | const onConfirm = jest.fn(); |
| 37 | const user = userEvent.setup(); |
| 38 | |
| 39 | const { unmount } = render( |
| 40 | <UpdateSettingConfirm |
| 41 | inputs={inputs} |
| 42 | onConfirm={onConfirm} |
| 43 | onCancel={jest.fn()} |
| 44 | />, |
| 45 | ); |
| 46 | |
| 47 | await user.click(screen.getByRole('button', { name: /confirm/i })); |
| 48 | expect(onConfirm).toHaveBeenCalledWith({ |
| 49 | data: inputs, |
| 50 | shouldRefreshPage: true, |
| 51 | }); |
| 52 | |
| 53 | unmount(); |
| 54 | expect(header.textContent).toBe('New Title'); |
| 55 | }); |
| 56 | }); |
| 57 |