| 1 |
import { UpdateLogoConfirm } from '@agent/workflows/theme/components/UpdateLogoConfirm'; |
| 2 |
import { render } from '@testing-library/react'; |
| 3 |
|
| 4 |
jest.mock('@agent/components/ImageUploader', () => ({ |
| 5 |
ImageUploader: ({ onSave, onCancel }) => ( |
| 6 |
<div> |
| 7 |
<button type="button" onClick={() => onSave({ imageId: 1 })}> |
| 8 |
Save |
| 9 |
</button> |
| 10 |
<button type="button" onClick={onCancel}> |
| 11 |
Cancel |
| 12 |
</button> |
| 13 |
</div> |
| 14 |
), |
| 15 |
})); |
| 16 |
jest.mock('@quick-edit/state/store', () => ({ |
| 17 |
useQuickEditStore: (selector) => selector({ agentBlock: { id: 'logo-1' } }), |
| 18 |
})); |
| 19 |
|
| 20 |
describe('UpdateLogoConfirm — undo on cancel/unmount', () => { |
| 21 |
let logoImg; |
| 22 |
|
| 23 |
beforeEach(() => { |
| 24 |
const logoWrapper = document.createElement('div'); |
| 25 |
logoWrapper.className = 'wp-block-site-logo'; |
| 26 |
logoImg = document.createElement('img'); |
| 27 |
logoImg.src = 'http://example.com/original-logo.png'; |
| 28 |
logoWrapper.appendChild(logoImg); |
| 29 |
document.body.appendChild(logoWrapper); |
| 30 |
}); |
| 31 |
|
| 32 |
afterEach(() => { |
| 33 |
document.querySelector('.wp-block-site-logo')?.remove(); |
| 34 |
}); |
| 35 |
|
| 36 |
test('restores original logo src on unmount without confirming', () => { |
| 37 |
const { unmount } = render( |
| 38 |
<UpdateLogoConfirm onConfirm={jest.fn()} onCancel={jest.fn()} />, |
| 39 |
); |
| 40 |
// Simulate a selection changing the logo |
| 41 |
logoImg.src = 'http://example.com/new-logo.png'; |
| 42 |
unmount(); |
| 43 |
expect(logoImg.src).toBe('http://example.com/original-logo.png'); |
| 44 |
}); |
| 45 |
|
| 46 |
test('does not restore logo after confirming', async () => { |
| 47 |
const onConfirm = jest.fn(); |
| 48 |
const { unmount, getByText } = render( |
| 49 |
<UpdateLogoConfirm onConfirm={onConfirm} onCancel={jest.fn()} />, |
| 50 |
); |
| 51 |
logoImg.src = 'http://example.com/new-logo.png'; |
| 52 |
const saveBtn = getByText('Save'); |
| 53 |
saveBtn.click(); |
| 54 |
await Promise.resolve(); // flush async onConfirm |
| 55 |
unmount(); |
| 56 |
// Should not have restored because confirmed was set |
| 57 |
expect(logoImg.src).toBe('http://example.com/new-logo.png'); |
| 58 |
}); |
| 59 |
}); |
| 60 |
|