import { UpdateMenuConfirm } from '@agent/workflows/theme/components/UpdateMenuConfirm';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
jest.mock('@wordpress/api-fetch', () => ({
__esModule: true,
default: jest.fn(() => Promise.resolve('
New Menu')),
}));
describe('UpdateMenuConfirm — undo on cancel/unmount', () => {
let nav;
beforeEach(() => {
nav = document.createElement('nav');
nav.setAttribute('data-extendify-menu-id', 'nav-1');
nav.innerHTML = 'Original Menu';
document.body.appendChild(nav);
});
afterEach(() => {
nav.remove();
});
const inputs = {
id: 'nav-1',
replacements: [
{ original: 'Original Menu', updated: 'New Menu' },
],
};
test('restores original menu HTML when unmounted without confirming', async () => {
const { unmount } = render(
,
);
// apiFetch updates nav asynchronously
await screen.findByText(/review and confirm/i);
unmount();
expect(nav.innerHTML).toBe('Original Menu');
});
test('does not restore menu after confirming', async () => {
const onConfirm = jest.fn();
const user = userEvent.setup();
const { unmount } = render(
,
);
await screen.findByText(/review and confirm/i);
await user.click(screen.getByRole('button', { name: /save/i }));
expect(onConfirm).toHaveBeenCalled();
unmount();
// Menu should NOT be restored since it was confirmed
expect(nav.innerHTML).not.toBe('Original Menu');
});
});