Skip to content

Commit 04b4e61

Browse files
committed
add confirm dialog tests
1 parent 4bf084c commit 04b4e61

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
})

src/webviews/src/modules/key-details/components/rejson-details/rejson-object/RejsonConfirmDialog.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ const ReJSONConfirmDialog = ({
3838
)}
3939
</p>
4040

41-
<RiButton className="absolute top-4 right-4" onClick={onClose}>
41+
<RiButton
42+
className="absolute top-4 right-4"
43+
onClick={onClose}
44+
aria-label="Close"
45+
>
4246
<VscClose />
4347
</RiButton>
4448

0 commit comments

Comments
 (0)