Skip to content

Commit a56c1eb

Browse files
authored
test: 补充输入框联动键盘的单测 (#7017)
1 parent 57ddfa6 commit a56c1eb

File tree

1 file changed

+105
-4
lines changed

1 file changed

+105
-4
lines changed

src/components/virtual-input/tests/virtual-input.test.tsx

Lines changed: 105 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import userEvent from '@testing-library/user-event'
12
import React, { createRef } from 'react'
23
import { act, fireEvent, render, screen, waitFor } from 'testing'
34
import { getDefaultConfig } from '../../config-provider'
@@ -115,14 +116,114 @@ describe('VirtualInput', () => {
115116
expect(ref.current?.blur).toBeDefined()
116117
})
117118

118-
test('focus and blur', async () => {
119-
render(<VirtualInput data-testid='virtualInput' clearable value='abc' />)
120-
fireEvent.focus(document.querySelector(`.${classPrefix}-content`)!)
119+
test('focus and blur with keyboard', async () => {
120+
const user = userEvent.setup()
121+
render(
122+
<VirtualInput
123+
data-testid='virtualInput'
124+
clearable
125+
keyboard={<NumberKeyboard confirmText={'go'} />}
126+
/>
127+
)
128+
await user.click(document.querySelector(`.${classPrefix}-content`)!)
129+
expect(document.querySelector(`.${classPrefix}-caret`)).toBeInTheDocument()
130+
expect(document.querySelector('.adm-number-keyboard-popup')).toBeVisible()
131+
132+
await user.click(document.body) // 点击空白处
133+
await new Promise(r => setTimeout(r, 500)) // 键盘收起有个动画,大约 300ms
134+
135+
expect(
136+
document.querySelector(`.${classPrefix}-caret`)
137+
).not.toBeInTheDocument()
138+
expect(
139+
document.querySelector('.adm-number-keyboard-popup')
140+
).not.toBeVisible()
141+
142+
// 重新聚焦
143+
await user.click(document.querySelector(`.${classPrefix}-content`)!)
144+
expect(document.querySelector(`.${classPrefix}-caret`)).toBeInTheDocument()
145+
expect(document.querySelector('.adm-number-keyboard-popup')).toBeVisible()
146+
147+
// 检查输入
148+
await user.click(screen.getByText('7'))
149+
await new Promise(r => setTimeout(r, 500)) // 键盘收起有个动画,大约 300ms
150+
expect(document.querySelector('.adm-number-keyboard-popup')).toBeVisible() // 点击键盘上数字,不会让键盘收起
151+
152+
await user.click(screen.getByText('go'))
153+
await new Promise(r => setTimeout(r, 500)) // 键盘收起有个动画,大约 300ms
154+
expect(
155+
document.querySelector('.adm-number-keyboard-popup')
156+
).not.toBeVisible() // 点击确定,收起键盘
157+
158+
expect(document.querySelector(`.${classPrefix}-content`)!.textContent).toBe(
159+
'7'
160+
)
161+
})
162+
163+
test('focus and blur with external keyboard', async () => {
164+
const user = userEvent.setup()
165+
const Wrapper = () => {
166+
const [visible, setVisible] = React.useState(false)
167+
const [value, setValue] = React.useState('')
168+
const ref = React.useRef<VirtualInputRef>(null)
169+
return (
170+
<>
171+
<VirtualInput
172+
data-testid='virtualInput'
173+
clearable
174+
value={value}
175+
ref={ref}
176+
onFocus={() => setVisible(true)}
177+
onBlur={() => setVisible(false)}
178+
/>
179+
<NumberKeyboard
180+
visible={visible}
181+
confirmText={'go'}
182+
onInput={v => setValue(ov => ov + v)}
183+
onDelete={() => setValue(ov => ov.slice(0, ov.length - 1))}
184+
onClose={() => {
185+
setVisible(false)
186+
ref.current?.blur()
187+
}}
188+
/>
189+
</>
190+
)
191+
}
192+
render(<Wrapper />)
193+
expect(document.querySelector(`.${classPrefix}-content`)!.textContent).toBe(
194+
''
195+
)
196+
await user.click(document.querySelector(`.${classPrefix}-content`)!) // 聚焦输入框
197+
expect(document.querySelector(`.${classPrefix}-caret`)).toBeInTheDocument()
198+
expect(document.querySelector('.adm-number-keyboard-popup')).toBeVisible()
199+
200+
await user.click(screen.getByText('8'))
201+
await new Promise(r => setTimeout(r, 500)) // 键盘收起有个动画,大约 300ms
202+
expect(document.querySelector('.adm-number-keyboard-popup')).toBeVisible() // 点击键盘上数字,不会触发 onBlur 让键盘收起
203+
204+
await user.click(screen.getByText('go'))
205+
await new Promise(r => setTimeout(r, 500)) // 键盘收起有个动画,大约 300ms
206+
expect(
207+
document.querySelector('.adm-number-keyboard-popup')
208+
).not.toBeVisible() // 点击确定,收起键盘
209+
210+
expect(document.querySelector(`.${classPrefix}-content`)!.textContent).toBe(
211+
'8'
212+
)
213+
214+
fireEvent.focus(document.querySelector(`.${classPrefix}-content`)!) // 重新聚焦,唤起键盘
215+
await new Promise(r => setTimeout(r, 500)) // 键盘收起有个动画,大约 300ms
121216
expect(document.querySelector(`.${classPrefix}-caret`)).toBeInTheDocument()
122-
fireEvent.click(document.body) // 点击空白处
217+
expect(document.querySelector('.adm-number-keyboard-popup')).toBeVisible()
218+
219+
await user.click(document.body)
220+
await new Promise(r => setTimeout(r, 500)) // 键盘收起有个动画,大约 300ms
123221
expect(
124222
document.querySelector(`.${classPrefix}-caret`)
125223
).not.toBeInTheDocument()
224+
expect(
225+
document.querySelector('.adm-number-keyboard-popup')
226+
).not.toBeVisible()
126227
})
127228

128229
test('ref should works', async () => {

0 commit comments

Comments
 (0)