diff --git a/README.md b/README.md index d05942afb..ad3796f30 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,9 @@ npm run build # 执行测试用例 npm test --workspaces +# 测试某个用例 +npm test --workspace amis inputImage + # 查看测试用例覆盖率 npm run coverage diff --git a/packages/amis/__tests__/renderers/Form/inputImage.test.tsx b/packages/amis/__tests__/renderers/Form/inputImage.test.tsx new file mode 100644 index 000000000..cdbea1806 --- /dev/null +++ b/packages/amis/__tests__/renderers/Form/inputImage.test.tsx @@ -0,0 +1,86 @@ +import React = require('react'); +import PageRenderer from '../../../../amis-core/src/renderers/Form'; +import * as renderer from 'react-test-renderer'; +import { + render, + fireEvent, + waitFor, + getByText, + prettyDOM +} from '@testing-library/react'; +import '../../../src'; +import {render as amisRender} from '../../../src'; +import {makeEnv, wait} from '../../helper'; + +test('Renderer:input-image autoFill', async () => { + const fetcher = jest.fn().mockImplementation(() => { + return Promise.resolve({ + data: { + status: 0, + msg: 'ok', + data: { + value: 'img.png', + filename: 'filename.png', + myUrl: 'http://amis.com/image.png' + } + } + }); + }); + global.URL.createObjectURL = jest.fn(); + + const { + debug, + container, + findByText, + getByLabelText, + findByPlaceholderText, + findByDisplayValue + } = render( + amisRender( + { + type: 'form', + api: '/api/xxx', + body: [ + { + type: 'input-image', + name: 'img', + label: 'img', + receiver: '/api/upload/file', + autoFill: { + // 不知道为啥这里不能用 ${url},可能是有什么地方和真实浏览器不一致 + myUrl: '${myUrl}' + } + }, + { + type: 'input-text', + name: 'myUrl' + } + ], + title: 'The form', + actions: [] + }, + {}, + makeEnv({fetcher}) + ) + ); + + const fileInput = container.querySelector( + 'input[type=file]' + )! as HTMLInputElement; + + const file = new File(['file'], 'ping.png', { + type: 'image/png' + }); + + fireEvent.change(fileInput, { + target: {files: [file]} + }); + + await wait(500); + + const textInput = container.querySelector( + 'input[name=myUrl]' + )! as HTMLInputElement; + + expect(textInput.value).toBe('http://amis.com/image.png'); +}); diff --git a/packages/amis/package.json b/packages/amis/package.json index 630cc7d4a..8a6063858 100644 --- a/packages/amis/package.json +++ b/packages/amis/package.json @@ -83,7 +83,7 @@ "@rollup/plugin-typescript": "^8.3.4", "@svgr/rollup": "^6.2.1", "@testing-library/jest-dom": "^5.16.4", - "@testing-library/react": "^13.0.0", + "@testing-library/react": "^13.4.0", "@types/async": "^2.0.45", "@types/codemirror": "^5.60.3", "@types/echarts": "^4.9.2",