|
1 | 1 | import React from 'react';
|
2 |
| -import {render} from "@testing-library/react"; |
3 |
| -import axios from 'axios'; |
| 2 | +import {act, fireEvent, render, screen, waitFor} from "@testing-library/react"; |
4 | 3 | import FlowPage from "@/pages/flow/work";
|
5 | 4 |
|
6 | 5 | jest.mock('axios');
|
7 |
| -const mockedAxios = axios as jest.Mocked<typeof axios>; |
8 | 6 |
|
9 | 7 | // test description for the component
|
10 | 8 | describe('Welcome Component', () => {
|
11 |
| - test('renders with default initial value', () => { |
12 |
| - |
13 |
| - mockedAxios.get.mockImplementation((url) => { |
14 |
| - if (url === '/api/users/123') { |
15 |
| - return Promise.resolve({ |
16 |
| - data: { name: 'John Doe', email: '[email protected]' } |
17 |
| - }); |
18 |
| - } |
19 |
| - if (url === '/api/users/123/posts') { |
20 |
| - return Promise.resolve({ |
21 |
| - data: [{ id: 1, title: 'Post 1' }] |
22 |
| - }); |
23 |
| - } |
24 |
| - if (url === '/api/users/123/followers') { |
25 |
| - return Promise.resolve({ |
26 |
| - data: [{ id: 1, name: 'Follower 1' }] |
27 |
| - }); |
28 |
| - } |
29 |
| - return Promise.reject(new Error('Not found')); |
30 |
| - }); |
| 9 | + |
| 10 | + test('renders with default initial value', async () => { |
31 | 11 |
|
32 | 12 | // render the component
|
33 |
| - const {getByTestId} =render( |
34 |
| - <FlowPage/> |
| 13 | + render( |
| 14 | + <FlowPage/> |
35 | 15 | );
|
36 | 16 |
|
37 |
| - const flowTable = getByTestId("flow-table"); |
| 17 | + const flowTable = screen.getByTestId("flow-table"); |
38 | 18 | expect(flowTable).toBeInTheDocument();
|
| 19 | + |
| 20 | + const flowAddBtn = screen.getByTestId("flow-add-btn"); |
| 21 | + expect(flowAddBtn).toBeInTheDocument(); |
| 22 | + |
| 23 | + |
| 24 | + await act(async () => { |
| 25 | + fireEvent.click(flowAddBtn); |
| 26 | + }); |
| 27 | + |
| 28 | + await waitFor(() => { |
| 29 | + const flowEditor = screen.getByTestId("flow-editor"); |
| 30 | + expect(flowEditor).toBeInTheDocument(); |
| 31 | + }); |
| 32 | + |
| 33 | + const inputTitle = screen.getByLabelText('标题'); |
| 34 | + const inputCode = screen.getByLabelText('编码'); |
| 35 | + const inputDescription = screen.getByLabelText('描述'); |
| 36 | + const inputPostponedMax = screen.getByLabelText('最大延期次数'); |
| 37 | + const inputSkipIfSameApprover = screen.getByLabelText('是否跳过相同审批人'); |
| 38 | + const submitButton = screen.getByTestId('flow-editor-submit'); |
| 39 | + expect(submitButton).toBeInTheDocument(); |
| 40 | + |
| 41 | + |
| 42 | + await act(async () => { |
| 43 | + fireEvent.change(inputTitle, {target: {value: 'test'}}); |
| 44 | + expect(inputTitle).toHaveValue('test'); |
| 45 | + fireEvent.change(inputCode, {target: {value: 'test'}}); |
| 46 | + expect(inputCode).toHaveValue('test'); |
| 47 | + fireEvent.change(inputDescription, {target: {value: 'test'}}); |
| 48 | + expect(inputDescription).toHaveValue('test'); |
| 49 | + fireEvent.change(inputPostponedMax, {target: {value: '1'}}); |
| 50 | + expect(inputPostponedMax).toHaveValue('1'); |
| 51 | + fireEvent.change(inputSkipIfSameApprover, {target: {value: 'true'}}); |
| 52 | + expect(inputSkipIfSameApprover).toHaveValue('true'); |
| 53 | + fireEvent.click(submitButton); |
| 54 | + }); |
| 55 | + |
| 56 | + await waitFor(() => { |
| 57 | + // todo 不敢展示,却必须得展示才能正常 |
| 58 | + const submitButton = screen.getByTestId('flow-editor-submit'); |
| 59 | + expect(submitButton).toBeInTheDocument(); |
| 60 | + }); |
| 61 | + |
39 | 62 | });
|
40 | 63 | });
|
0 commit comments