|
1 | 1 | import * as React from 'react' |
2 | | -import { render, fireEvent, waitFor } from '@testing-library/react' |
3 | | -import '@testing-library/jest-dom' |
4 | 2 | import { useEffect } from 'react' |
| 3 | +import { fireEvent, render, waitFor } from '@testing-library/react' |
| 4 | +import '@testing-library/jest-dom' |
| 5 | +import { Button, Radio, Space } from '@nutui/nutui-react' |
5 | 6 | import Form, { FormInstance } from '@/packages/form' |
6 | 7 | import Input from '@/packages/input' |
7 | 8 |
|
@@ -167,7 +168,9 @@ test('form set required', () => { |
167 | 168 | </Form.Item> |
168 | 169 | </Form> |
169 | 170 | ) |
170 | | - expect(container.querySelectorAll('.required')).toHaveLength(1) |
| 171 | + expect( |
| 172 | + container.querySelectorAll('.nut-form-item-label-required') |
| 173 | + ).toHaveLength(1) |
171 | 174 | }) |
172 | 175 |
|
173 | 176 | test('form set change value', async () => { |
@@ -310,3 +313,126 @@ test('no-style and render function', async () => { |
310 | 313 | expect(relatedInput).toBeTruthy() |
311 | 314 | }) |
312 | 315 | }) |
| 316 | + |
| 317 | +test('reset usename filed', async () => { |
| 318 | + const Demo1 = () => { |
| 319 | + const [form] = Form.useForm() |
| 320 | + return ( |
| 321 | + <> |
| 322 | + <Form |
| 323 | + form={form} |
| 324 | + labelPosition="right" |
| 325 | + footer={ |
| 326 | + <> |
| 327 | + <div |
| 328 | + id="reset" |
| 329 | + onClick={() => { |
| 330 | + form.resetFields(['username']) |
| 331 | + }} |
| 332 | + > |
| 333 | + Reset |
| 334 | + </div> |
| 335 | + </> |
| 336 | + } |
| 337 | + > |
| 338 | + <Form.Item |
| 339 | + align="center" |
| 340 | + required |
| 341 | + label="字段A" |
| 342 | + name="username" |
| 343 | + rules={[{ max: 5, message: '字段A不能超过5个字' }]} |
| 344 | + > |
| 345 | + <Input placeholder="请输入字段A" type="text" /> |
| 346 | + </Form.Item> |
| 347 | + </Form> |
| 348 | + </> |
| 349 | + ) |
| 350 | + } |
| 351 | + const { container } = render(<Demo1 />) |
| 352 | + const input = container.querySelector('input') |
| 353 | + const reset = container.querySelector('#reset') |
| 354 | + if (input) { |
| 355 | + fireEvent.change(input, { target: { value: 'NutUI React Taro' } }) |
| 356 | + await waitFor(() => { |
| 357 | + expect( |
| 358 | + container.querySelector('.nut-form-item-body-tips') |
| 359 | + ).toHaveTextContent('字段A不能超过5个字') |
| 360 | + }) |
| 361 | + } |
| 362 | + if (reset) { |
| 363 | + fireEvent.click(reset) |
| 364 | + await waitFor(() => { |
| 365 | + expect(container.querySelector('.nut-form-item-body-tips')).toBeNull() |
| 366 | + }) |
| 367 | + } |
| 368 | +}) |
| 369 | + |
| 370 | +test('useWatch', async () => { |
| 371 | + const Demo = () => { |
| 372 | + const [form] = Form.useForm() |
| 373 | + const account = Form.useWatch('account', form) |
| 374 | + const loginMethod = Form.useWatch('loginMethod', form) |
| 375 | + return ( |
| 376 | + <Form |
| 377 | + form={form} |
| 378 | + initialValues={{ loginMethod: 'mobile', account: '123' }} |
| 379 | + footer={ |
| 380 | + <> |
| 381 | + <div |
| 382 | + style={{ |
| 383 | + width: '100%', |
| 384 | + }} |
| 385 | + > |
| 386 | + <div |
| 387 | + className="result" |
| 388 | + style={{ |
| 389 | + fontSize: '12px', |
| 390 | + textAlign: 'center', |
| 391 | + marginBottom: '20px', |
| 392 | + }} |
| 393 | + > |
| 394 | + 你将使用 {loginMethod === 'mobile' ? '手机号' : '邮箱'}{' '} |
| 395 | + {account} 登录 |
| 396 | + </div> |
| 397 | + <Button block type="primary" size="large" nativeType="submit"> |
| 398 | + 提交 |
| 399 | + </Button> |
| 400 | + </div> |
| 401 | + </> |
| 402 | + } |
| 403 | + > |
| 404 | + <Form.Item name="loginMethod" label="登录方式"> |
| 405 | + <Radio.Group> |
| 406 | + <Space> |
| 407 | + <Radio value="mobile">手机号</Radio> |
| 408 | + <Radio className="clickTest" value="email"> |
| 409 | + 邮箱 |
| 410 | + </Radio> |
| 411 | + </Space> |
| 412 | + </Radio.Group> |
| 413 | + </Form.Item> |
| 414 | + |
| 415 | + <> |
| 416 | + {loginMethod === 'mobile' && ( |
| 417 | + <Form.Item name="account" label="手机号"> |
| 418 | + <Input placeholder="请输入手机号" /> |
| 419 | + </Form.Item> |
| 420 | + )} |
| 421 | + {loginMethod === 'email' && ( |
| 422 | + <Form.Item name="account" label="邮箱"> |
| 423 | + <Input placeholder="请输入邮箱" /> |
| 424 | + </Form.Item> |
| 425 | + )} |
| 426 | + </> |
| 427 | + </Form> |
| 428 | + ) |
| 429 | + } |
| 430 | + |
| 431 | + const { container } = render(<Demo />) |
| 432 | + const clickTest = container.querySelector('.clickTest') |
| 433 | + if (clickTest) { |
| 434 | + fireEvent.click(clickTest) |
| 435 | + const result = container.querySelector('.result') |
| 436 | + expect(result).toHaveTextContent('你将使用 邮箱 123 登录') |
| 437 | + } |
| 438 | +}) |
0 commit comments