| 1 |
import { render, screen } from '@testing-library/react'; |
| 2 |
import userEvent from '@testing-library/user-event'; |
| 3 |
import { UpdateMenuConfirm } from '../UpdateMenuConfirm'; |
| 4 |
|
| 5 |
jest.mock('@wordpress/api-fetch', () => ({ |
| 6 |
__esModule: true, |
| 7 |
default: jest.fn(() => Promise.resolve('<li>New Menu</li>')), |
| 8 |
})); |
| 9 |
|
| 10 |
describe('UpdateMenuConfirm — undo on cancel/unmount', () => { |
| 11 |
let nav; |
| 12 |
|
| 13 |
beforeEach(() => { |
| 14 |
nav = document.createElement('nav'); |
| 15 |
nav.setAttribute('data-extendify-menu-id', 'nav-1'); |
| 16 |
nav.innerHTML = '<li>Original Menu</li>'; |
| 17 |
document.body.appendChild(nav); |
| 18 |
}); |
| 19 |
|
| 20 |
afterEach(() => { |
| 21 |
nav.remove(); |
| 22 |
}); |
| 23 |
|
| 24 |
const inputs = { |
| 25 |
id: 'nav-1', |
| 26 |
replacements: [ |
| 27 |
{ original: '<li>Original Menu</li>', updated: '<li>New Menu</li>' }, |
| 28 |
], |
| 29 |
}; |
| 30 |
|
| 31 |
test('restores original menu HTML when unmounted without confirming', async () => { |
| 32 |
const { unmount } = render( |
| 33 |
<UpdateMenuConfirm |
| 34 |
inputs={inputs} |
| 35 |
onConfirm={jest.fn()} |
| 36 |
onCancel={jest.fn()} |
| 37 |
/>, |
| 38 |
); |
| 39 |
// apiFetch updates nav asynchronously |
| 40 |
await screen.findByText(/review and confirm/i); |
| 41 |
unmount(); |
| 42 |
expect(nav.innerHTML).toBe('<li>Original Menu</li>'); |
| 43 |
}); |
| 44 |
|
| 45 |
test('does not restore menu after confirming', async () => { |
| 46 |
const onConfirm = jest.fn(); |
| 47 |
const user = userEvent.setup(); |
| 48 |
const { unmount } = render( |
| 49 |
<UpdateMenuConfirm |
| 50 |
inputs={inputs} |
| 51 |
onConfirm={onConfirm} |
| 52 |
onCancel={jest.fn()} |
| 53 |
/>, |
| 54 |
); |
| 55 |
await screen.findByText(/review and confirm/i); |
| 56 |
await user.click(screen.getByRole('button', { name: /save/i })); |
| 57 |
expect(onConfirm).toHaveBeenCalled(); |
| 58 |
unmount(); |
| 59 |
// Menu should NOT be restored since it was confirmed |
| 60 |
expect(nav.innerHTML).not.toBe('<li>Original Menu</li>'); |
| 61 |
}); |
| 62 |
}); |
| 63 |
|