diff --git a/admin-ui/src/jest.setup.ts b/admin-ui/src/jest.setup.ts index 27fffb57..c74d7b05 100644 --- a/admin-ui/src/jest.setup.ts +++ b/admin-ui/src/jest.setup.ts @@ -1,8 +1,8 @@ import '@testing-library/jest-dom'; +import {TextEncoder} from 'util'; - -// src/jest.setup.ts -import '@testing-library/jest-dom'; +// 添加全局对象 +global.TextEncoder = TextEncoder; // Mock window.matchMedia Object.defineProperty(window, 'matchMedia', { @@ -21,9 +21,14 @@ Object.defineProperty(window, 'matchMedia', { // Mock ResizeObserver global.ResizeObserver = class ResizeObserver { - observe() {} - unobserve() {} - disconnect() {} + observe() { + } + + unobserve() { + } + + disconnect() { + } }; // Suppress findDOMNode warnings in test output diff --git a/admin-ui/src/pages/flow/work/__test__/index.test.tsx b/admin-ui/src/pages/flow/work/__test__/index.test.tsx index a100b0ac..30686478 100644 --- a/admin-ui/src/pages/flow/work/__test__/index.test.tsx +++ b/admin-ui/src/pages/flow/work/__test__/index.test.tsx @@ -1,40 +1,63 @@ import React from 'react'; -import {render} from "@testing-library/react"; -import axios from 'axios'; +import {act, fireEvent, render, screen, waitFor} from "@testing-library/react"; import FlowPage from "@/pages/flow/work"; jest.mock('axios'); -const mockedAxios = axios as jest.Mocked; // test description for the component describe('Welcome Component', () => { - test('renders with default initial value', () => { - - mockedAxios.get.mockImplementation((url) => { - if (url === '/api/users/123') { - return Promise.resolve({ - data: { name: 'John Doe', email: 'john@example.com' } - }); - } - if (url === '/api/users/123/posts') { - return Promise.resolve({ - data: [{ id: 1, title: 'Post 1' }] - }); - } - if (url === '/api/users/123/followers') { - return Promise.resolve({ - data: [{ id: 1, name: 'Follower 1' }] - }); - } - return Promise.reject(new Error('Not found')); - }); + + test('renders with default initial value', async () => { // render the component - const {getByTestId} =render( - + render( + ); - const flowTable = getByTestId("flow-table"); + const flowTable = screen.getByTestId("flow-table"); expect(flowTable).toBeInTheDocument(); + + const flowAddBtn = screen.getByTestId("flow-add-btn"); + expect(flowAddBtn).toBeInTheDocument(); + + + await act(async () => { + fireEvent.click(flowAddBtn); + }); + + await waitFor(() => { + const flowEditor = screen.getByTestId("flow-editor"); + expect(flowEditor).toBeInTheDocument(); + }); + + const inputTitle = screen.getByLabelText('标题'); + const inputCode = screen.getByLabelText('编码'); + const inputDescription = screen.getByLabelText('描述'); + const inputPostponedMax = screen.getByLabelText('最大延期次数'); + const inputSkipIfSameApprover = screen.getByLabelText('是否跳过相同审批人'); + const submitButton = screen.getByTestId('flow-editor-submit'); + expect(submitButton).toBeInTheDocument(); + + + await act(async () => { + fireEvent.change(inputTitle, {target: {value: 'test'}}); + expect(inputTitle).toHaveValue('test'); + fireEvent.change(inputCode, {target: {value: 'test'}}); + expect(inputCode).toHaveValue('test'); + fireEvent.change(inputDescription, {target: {value: 'test'}}); + expect(inputDescription).toHaveValue('test'); + fireEvent.change(inputPostponedMax, {target: {value: '1'}}); + expect(inputPostponedMax).toHaveValue('1'); + fireEvent.change(inputSkipIfSameApprover, {target: {value: 'true'}}); + expect(inputSkipIfSameApprover).toHaveValue('true'); + fireEvent.click(submitButton); + }); + + await waitFor(() => { + // todo 不敢展示,却必须得展示才能正常 + const submitButton = screen.getByTestId('flow-editor-submit'); + expect(submitButton).toBeInTheDocument(); + }); + }); }); diff --git a/admin-ui/src/pages/flow/work/index.tsx b/admin-ui/src/pages/flow/work/index.tsx index 41268f7b..69d45222 100644 --- a/admin-ui/src/pages/flow/work/index.tsx +++ b/admin-ui/src/pages/flow/work/index.tsx @@ -178,6 +178,7 @@ const FlowPage = () => { toolBarRender={() => { return [