| 1 |
import { SelectThemeVariation } from '@agent/workflows/theme/components/SelectThemeVariation'; |
| 2 |
import { render, screen } from '@testing-library/react'; |
| 3 |
import userEvent from '@testing-library/user-event'; |
| 4 |
|
| 5 |
const mockUndoChange = jest.fn(); |
| 6 |
jest.mock('@agent/hooks/useVariationOverride', () => ({ |
| 7 |
useVariationOverride: () => ({ undoChange: mockUndoChange }), |
| 8 |
})); |
| 9 |
jest.mock('@agent/hooks/useThemeVariations', () => ({ |
| 10 |
useThemeVariations: () => ({ |
| 11 |
variations: [ |
| 12 |
{ |
| 13 |
title: 'Sunset', |
| 14 |
css: '.sunset{}', |
| 15 |
settings: { |
| 16 |
color: { |
| 17 |
palette: { |
| 18 |
theme: [ |
| 19 |
{ slug: 'background', color: '#fff' }, |
| 20 |
{ slug: 'primary', color: '#f00' }, |
| 21 |
], |
| 22 |
}, |
| 23 |
}, |
| 24 |
}, |
| 25 |
}, |
| 26 |
], |
| 27 |
isLoading: false, |
| 28 |
}), |
| 29 |
})); |
| 30 |
jest.mock('@agent/state/chat', () => ({ |
| 31 |
useChatStore: () => ({ addMessage: jest.fn(), messages: [] }), |
| 32 |
})); |
| 33 |
|
| 34 |
beforeEach(() => mockUndoChange.mockClear()); |
| 35 |
|
| 36 |
describe('SelectThemeVariation — undo on cancel/unmount', () => { |
| 37 |
test('calls undoChange on unmount without confirming', () => { |
| 38 |
const { unmount } = render( |
| 39 |
<SelectThemeVariation |
| 40 |
onConfirm={jest.fn()} |
| 41 |
onCancel={jest.fn()} |
| 42 |
onLoad={jest.fn()} |
| 43 |
/>, |
| 44 |
); |
| 45 |
unmount(); |
| 46 |
expect(mockUndoChange).toHaveBeenCalledTimes(1); |
| 47 |
}); |
| 48 |
|
| 49 |
test('does not call undoChange after confirming', async () => { |
| 50 |
const onConfirm = jest.fn(); |
| 51 |
const user = userEvent.setup(); |
| 52 |
const { unmount } = render( |
| 53 |
<SelectThemeVariation |
| 54 |
onConfirm={onConfirm} |
| 55 |
onCancel={jest.fn()} |
| 56 |
onLoad={jest.fn()} |
| 57 |
/>, |
| 58 |
); |
| 59 |
// Select a variation first |
| 60 |
const btn = screen |
| 61 |
.getAllByRole('button') |
| 62 |
.find((b) => !b.textContent.match(/cancel|save/i)); |
| 63 |
await user.click(btn); |
| 64 |
await user.click(screen.getByRole('button', { name: /save/i })); |
| 65 |
expect(onConfirm).toHaveBeenCalled(); |
| 66 |
unmount(); |
| 67 |
expect(mockUndoChange).not.toHaveBeenCalled(); |
| 68 |
}); |
| 69 |
}); |
| 70 |
|