|
| 1 | +import React from 'react' |
| 2 | +import { fireEvent } from '@testing-library/react' |
| 3 | +import { render, screen } from 'testSrc/helpers' |
| 4 | + |
| 5 | +import ReJSONConfirmDialog from './RejsonConfirmDialog' |
| 6 | + |
| 7 | +describe('ReJSONConfirmDialog', () => { |
| 8 | + it('should not render when open is false', () => { |
| 9 | + render( |
| 10 | + <ReJSONConfirmDialog |
| 11 | + open={false} |
| 12 | + onClose={vi.fn()} |
| 13 | + onConfirm={vi.fn()} |
| 14 | + />, |
| 15 | + ) |
| 16 | + |
| 17 | + expect( |
| 18 | + screen.queryByText('Duplicate JSON key detected'), |
| 19 | + ).not.toBeInTheDocument() |
| 20 | + }) |
| 21 | + |
| 22 | + it('should render when open is true', () => { |
| 23 | + render( |
| 24 | + <ReJSONConfirmDialog open={true} onClose={vi.fn()} onConfirm={vi.fn()} />, |
| 25 | + ) |
| 26 | + |
| 27 | + expect(screen.getByText('Duplicate JSON key detected')).toBeInTheDocument() |
| 28 | + expect( |
| 29 | + screen.getByText('You already have the same JSON key.'), |
| 30 | + ).toBeInTheDocument() |
| 31 | + expect( |
| 32 | + screen.getByText( |
| 33 | + 'If you proceed, a value of the existing JSON key will be overwritten.', |
| 34 | + ), |
| 35 | + ).toBeInTheDocument() |
| 36 | + }) |
| 37 | + |
| 38 | + it('should call onConfirm when Overwrite button is clicked', () => { |
| 39 | + const onConfirm = vi.fn() |
| 40 | + |
| 41 | + render( |
| 42 | + <ReJSONConfirmDialog |
| 43 | + open={true} |
| 44 | + onClose={vi.fn()} |
| 45 | + onConfirm={onConfirm} |
| 46 | + />, |
| 47 | + ) |
| 48 | + |
| 49 | + fireEvent.click(screen.getByTestId('confirm-btn')) |
| 50 | + |
| 51 | + expect(onConfirm).toHaveBeenCalledTimes(1) |
| 52 | + }) |
| 53 | + |
| 54 | + it('should call onClose when Cancel button is clicked', () => { |
| 55 | + const onClose = vi.fn() |
| 56 | + |
| 57 | + render( |
| 58 | + <ReJSONConfirmDialog open={true} onClose={onClose} onConfirm={vi.fn()} />, |
| 59 | + ) |
| 60 | + |
| 61 | + fireEvent.click(screen.getByTestId('cancel-btn')) |
| 62 | + |
| 63 | + expect(onClose).toHaveBeenCalledTimes(1) |
| 64 | + }) |
| 65 | + |
| 66 | + it('should call onClose when close (x in the corner) is clicked', () => { |
| 67 | + const onClose = vi.fn() |
| 68 | + |
| 69 | + render( |
| 70 | + <ReJSONConfirmDialog open={true} onClose={onClose} onConfirm={vi.fn()} />, |
| 71 | + ) |
| 72 | + |
| 73 | + const closeButton = screen.getByRole('button', { name: 'Close' }) |
| 74 | + fireEvent.click(closeButton) |
| 75 | + |
| 76 | + expect(onClose).toHaveBeenCalledTimes(1) |
| 77 | + }) |
| 78 | +}) |
0 commit comments