forked from langgenius/dify
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.spec.tsx
More file actions
146 lines (126 loc) · 4.53 KB
/
index.spec.tsx
File metadata and controls
146 lines (126 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { cleanup, fireEvent, render } from '@testing-library/react'
import * as React from 'react'
import { createReactI18nextMock } from '@/test/i18n-mock'
import InlineDeleteConfirm from './index'
// Mock react-i18next with custom translations for test assertions
vi.mock('react-i18next', () => createReactI18nextMock({
'operation.deleteConfirmTitle': 'Delete?',
'operation.yes': 'Yes',
'operation.no': 'No',
'operation.confirmAction': 'Please confirm your action.',
}))
afterEach(cleanup)
describe('InlineDeleteConfirm', () => {
describe('Rendering', () => {
it('should render with default text', () => {
const onConfirm = vi.fn()
const onCancel = vi.fn()
const { getByText } = render(
<InlineDeleteConfirm onConfirm={onConfirm} onCancel={onCancel} />,
)
expect(getByText('Delete?')).toBeInTheDocument()
expect(getByText('No')).toBeInTheDocument()
expect(getByText('Yes')).toBeInTheDocument()
})
it('should render with custom text', () => {
const onConfirm = vi.fn()
const onCancel = vi.fn()
const { getByText } = render(
<InlineDeleteConfirm
title="Remove?"
confirmText="Confirm"
cancelText="Cancel"
onConfirm={onConfirm}
onCancel={onCancel}
/>,
)
expect(getByText('Remove?')).toBeInTheDocument()
expect(getByText('Cancel')).toBeInTheDocument()
expect(getByText('Confirm')).toBeInTheDocument()
})
it('should have proper ARIA attributes', () => {
const onConfirm = vi.fn()
const onCancel = vi.fn()
const { container } = render(
<InlineDeleteConfirm onConfirm={onConfirm} onCancel={onCancel} />,
)
const wrapper = container.firstChild as HTMLElement
expect(wrapper).toHaveAttribute('aria-labelledby', 'inline-delete-confirm-title')
expect(wrapper).toHaveAttribute('aria-describedby', 'inline-delete-confirm-description')
})
})
describe('Button interactions', () => {
it('should call onCancel when cancel button is clicked', () => {
const onConfirm = vi.fn()
const onCancel = vi.fn()
const { getByText } = render(
<InlineDeleteConfirm onConfirm={onConfirm} onCancel={onCancel} />,
)
fireEvent.click(getByText('No'))
expect(onCancel).toHaveBeenCalledTimes(1)
expect(onConfirm).not.toHaveBeenCalled()
})
it('should call onConfirm when confirm button is clicked', () => {
const onConfirm = vi.fn()
const onCancel = vi.fn()
const { getByText } = render(
<InlineDeleteConfirm onConfirm={onConfirm} onCancel={onCancel} />,
)
fireEvent.click(getByText('Yes'))
expect(onConfirm).toHaveBeenCalledTimes(1)
expect(onCancel).not.toHaveBeenCalled()
})
})
describe('Variant prop', () => {
it('should render with delete variant by default', () => {
const onConfirm = vi.fn()
const onCancel = vi.fn()
const { getByText } = render(
<InlineDeleteConfirm onConfirm={onConfirm} onCancel={onCancel} />,
)
const confirmButton = getByText('Yes').closest('button')
expect(confirmButton?.className).toContain('btn-destructive')
})
it('should render without destructive class for warning variant', () => {
const onConfirm = vi.fn()
const onCancel = vi.fn()
const { getByText } = render(
<InlineDeleteConfirm
variant="warning"
onConfirm={onConfirm}
onCancel={onCancel}
/>,
)
const confirmButton = getByText('Yes').closest('button')
expect(confirmButton?.className).not.toContain('btn-destructive')
})
it('should render without destructive class for info variant', () => {
const onConfirm = vi.fn()
const onCancel = vi.fn()
const { getByText } = render(
<InlineDeleteConfirm
variant="info"
onConfirm={onConfirm}
onCancel={onCancel}
/>,
)
const confirmButton = getByText('Yes').closest('button')
expect(confirmButton?.className).not.toContain('btn-destructive')
})
})
describe('Custom className', () => {
it('should apply custom className to wrapper', () => {
const onConfirm = vi.fn()
const onCancel = vi.fn()
const { container } = render(
<InlineDeleteConfirm
className="custom-class"
onConfirm={onConfirm}
onCancel={onCancel}
/>,
)
const wrapper = container.firstChild as HTMLElement
expect(wrapper.className).toContain('custom-class')
})
})
})