Skip to content

Commit

Permalink
feat: update form test
Browse files Browse the repository at this point in the history
  • Loading branch information
ziqisia committed Sep 20, 2023
1 parent 5e029cd commit fa911bb
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 19 deletions.
5 changes: 3 additions & 2 deletions packages/arcodesign/components/form/__test__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import demoTest from '../../../tests/demoTest';
import mountTest from '../../../tests/mountTest';
import Form, { useForm } from '..';
import Input from '../../input';
import '@testing-library/jest-dom';

demoTest('form');

Expand Down Expand Up @@ -43,7 +44,7 @@ describe('Form input', () => {
formRef.current.form.validateFields().catch(() => {});
});
await waitFor(() => {
expect(screen.getByText('test 为必填项')).not.toBeUndefined();
expect(screen.getByText('test 为必填项')).toBeInTheDocument();
});
});
it('useForm', async () => {
Expand Down Expand Up @@ -119,7 +120,7 @@ describe('Form input', () => {
await userEvent.click(submitBtn);
const error = await screen.findByText('required');
await waitFor(() => {
expect(error).not.toBeUndefined();
expect(error).toBeInTheDocument();
});
await waitFor(() => {
expect(onSubmitFailed).toBeCalled();
Expand Down
212 changes: 195 additions & 17 deletions packages/arcodesign/components/image-picker/__test__/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React, { useState } from 'react';
import { mount } from 'enzyme';
import React from 'react';
import { render, waitFor, waitForElementToBeRemoved } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import demoTest from '../../../tests/demoTest';
import mountTest from '../../../tests/mountTest';
import { defaultContext } from '../../context-provider';
import ImagePicker from '..';
import '@testing-library/jest-dom';

demoTest('image-picker');

Expand All @@ -13,36 +15,212 @@ const prefix = `${defaultContext.prefixCls}-image-picker`;
const imagePrefix = `${defaultContext.prefixCls}-image`;

const IMG_URL = 'http://sf1-cdn-tos.toutiaostatic.com/obj/arco-mobile/_static_/large_image_1.jpg';

const IMG_DATA =
'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTQzIiBoZWlnaHQ9IjI0Ii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EtNS40NDUgNC45MDYtNS40NDV6bS43MjYgMi42NGMtMS42MzkgMC0yLjY4NCAxLjI1NC0yLjY4NCAyLjgwNSAwIDEuNTQgMS4wNDUgMi43OTQgMi42ODQgMi43OTQgMS41OTUgMCAyLjY3My0xLjIxIDIuNjczLTIuNzk0IDAtMS41OTUtMS4wNzgtMi44MDUtMi42NzMtMi44MDV6bTEzLjIzMy0yLjY0YzIuNDIgMCA0LjE0NyAxLjY4MyA0LjE0NyA0LjE1OHY2Ljc4N2gtMi44NzF2LTUuODYzYzAtMS41NC0uNzctMi4zODctMi4wNzktMi4zODctMS4yMzIgMC0yLjI1NS44MDMtMi4yNTUgMi40MDl2NS44NDFoLTIuODcxVjguMzM1aDIuNzk0djEuMjU0Yy43NTktMS4xMzMgMS45MjUtMS41NCAzLjEzNS0xLjU0eiIgZmlsbD0iIzFkMjEyOSIgZmlsbC1ydWxlPSJub256ZXJvIj48L3BhdGg+CiAgICA8L2c+Cjwvc3ZnPg==';

const mockImgFile = new File([IMG_DATA], 'img.png', { type: 'image/png' });

describe('ImagePicker', () => {
beforeEach(() => {
jest.spyOn(global, 'FileReader').mockImplementation(function () {
this.readAsDataURL = jest.fn();
this.onload = jest.fn();
this.onerror = jest.fn();
});
});
afterEach(() => {
jest.restoreAllMocks();
});
it('ImagePicker renders correctly', () => {
const wrapper = mount(<ImagePicker images={[]} />);
expect(wrapper.find(`.${prefix}-add`).length).toBe(1);
const { container } = render(<ImagePicker images={[]} />);
expect(container.querySelectorAll(`.${prefix}-add`).length).toBe(1);
});
it('ImagePicker renders correctly', () => {
const wrapper = mount(<ImagePicker images={[{ url: IMG_URL }, { url: IMG_URL }]} />);
expect(wrapper.find(`.${imagePrefix}`).length).toBe(2);
const { container } = render(<ImagePicker images={[{ url: IMG_URL }, { url: IMG_URL }]} />);
expect(container.querySelectorAll(`.${imagePrefix}`).length).toBe(2);
});
it('ImagePicker renders correctly', () => {
const wrapper = mount(
const { container } = render(
<ImagePicker images={[{ url: IMG_URL }, { url: IMG_URL }]} limit={1} />,
);
expect(wrapper.find(`.${imagePrefix}`).length).toBe(1);
expect(container.querySelectorAll(`.${imagePrefix}`).length).toBe(1);
});
it('ImagePicker renders correctly', () => {
const wrapper = mount(
const { container, rerender } = render(
<ImagePicker images={[{ url: IMG_URL }, { url: IMG_URL }]} limit={1} />,
);
expect(wrapper.find(`.${imagePrefix}`).length).toBe(1);
wrapper.setProps({
images: [],
});
expect(wrapper.find(`.${imagePrefix}`).length).toBe(0);
expect(container.querySelectorAll(`.${imagePrefix}`).length).toBe(1);
rerender(<ImagePicker images={[]} limit={1} />);
expect(container.querySelectorAll(`.${imagePrefix}`).length).toBe(0);
});
it('onChange listener correctly', () => {
const onChange = jest.fn();
const wrapper = mount(<ImagePicker images={[{ url: IMG_URL }]} onChange={onChange} />);
wrapper.find(`.${prefix}-close`).at(0).simulate('click', 0);

const { container } = render(
<ImagePicker images={[{ url: IMG_URL }]} onChange={onChange} />,
);
container.querySelector(`.${prefix}-close`);
userEvent.click(container.querySelector(`.${prefix}-close`));
expect(onChange.mock.calls.length).toBe(1);
});
it('upload correctly', async () => {
function App() {
const [images, setImages] = React.useState([]);
return <ImagePicker images={images} onChange={setImages} />;
}
const { container } = render(<App />);
const selector = container.querySelector(`.${prefix}-add`);
await userEvent.click(selector);
const input = container.querySelector('input');
await userEvent.upload(input, mockImgFile);
const reader = FileReader.mock.instances[0];
reader.onload({ target: { result: 'foo' } });
await waitFor(() => {
expect(container.querySelectorAll(`.${imagePrefix}`).length).toBe(1);
});
});
it('custom upload', async () => {
function App() {
async function mockUpload() {
return {
url: IMG_DATA,
};
}
const [images, setImages] = React.useState([]);
return <ImagePicker images={images} onChange={setImages} upload={mockUpload} />;
}
const { container } = render(<App />);
const selectors = container.querySelectorAll(`.${prefix}-add`);
const inputs = container.querySelectorAll('input');
await userEvent.click(selectors[0]);
await userEvent.upload(inputs[0], mockImgFile);
const reader = FileReader.mock.instances[0];
reader.onload({ target: { result: 'foo' } });
await waitFor(() => {
expect(container.querySelectorAll(`.${imagePrefix}`).length).toBe(1);
});
});
it('upload maxSize', async () => {
function App({ onMaxSizeExceed }) {
const [images, setImages] = React.useState([]);
return (
<ImagePicker
maxSize={1}
images={images}
onChange={setImages}
onMaxSizeExceed={onMaxSizeExceed}
/>
);
}
const handleMaxSizeExceed = jest.fn();
const { container } = render(<App onMaxSizeExceed={handleMaxSizeExceed} />);
const selector = container.querySelector(`.${prefix}-add`);
await userEvent.click(selector);
const input = container.querySelector('input');
await userEvent.upload(input, mockImgFile);
await waitFor(() => {
expect(container.querySelectorAll(`.${imagePrefix}`).length).toBe(0);
});
await waitFor(() => {
expect(handleMaxSizeExceed).toBeCalledTimes(1);
});
});
it('upload limit', async () => {
function App({ onLimitExceed }) {
const [images, setImages] = React.useState([]);
return (
<ImagePicker
multiple
limit={1}
images={images}
onChange={setImages}
onLimitExceed={onLimitExceed}
/>
);
}
const handlLimitExceed = jest.fn();
const { container } = render(<App onLimitExceed={handlLimitExceed} />);
const selector = container.querySelector(`.${prefix}-add`);
await userEvent.click(selector);
const input = container.querySelector('input');
await userEvent.upload(input, [mockImgFile, mockImgFile, mockImgFile]);
const reader = FileReader.mock.instances[0];
reader.onload({ target: { result: 'foo' } });
await waitFor(() => {
expect(container.querySelectorAll(`.${imagePrefix}`).length).toBe(1);
});
await waitFor(() => {
expect(handlLimitExceed).toBeCalledTimes(1);
});
});
it('click image correctly', () => {
const handleClick = jest.fn();
const { container } = render(
<ImagePicker images={[{ url: IMG_URL }]} onClick={handleClick} />,
);
userEvent.click(container.querySelector(`.${imagePrefix}`));
expect(handleClick).toBeCalledTimes(1);
});
it('selectAdapter', async () => {
const handleMaxSizeExceed = jest.fn();
const handleLimitExceed = jest.fn();
function App({ onMaxSizeExceed, onLimitExceed }) {
const [images, setImages] = React.useState([{ url: IMG_URL }, { url: IMG_URL }]);
const selectAdapter = () => {
return new Promise(resolve => {
const files = [
{
name: '11vnpmryrl4.jpeg',
size: 789020,
type: 'image/jpeg',
url: 'http://sf1-cdn-tos.toutiaostatic.com/obj/arco-mobile/_static_/large_image_1.jpg',
},
{
name: '11vnpmryrl2.jpeg',
size: 7820,
type: 'image/jpeg',
url: 'http://sf1-cdn-tos.toutiaostatic.com/obj/arco-mobile/_static_/large_image_2.jpg',
},
{
name: '11vnpmryrl1.jpeg',
size: 7820,
type: 'image/jpeg',
url: 'http://sf1-cdn-tos.toutiaostatic.com/obj/arco-mobile/_static_/large_image_3.jpg',
},
];
setTimeout(() => {
resolve({
files,
});
}, 1000);
});
};
return (
<ImagePicker
images={images}
onChange={setImages}
selectAdapter={selectAdapter}
limit={3}
maxSize={100}
multiple
onMaxSizeExceed={onMaxSizeExceed}
onLimitExceed={onLimitExceed}
/>
);
}
const { container } = render(
<App onMaxSizeExceed={handleMaxSizeExceed} onLimitExceed={handleLimitExceed} />,
);
const selector = container.querySelector(`.${prefix}-add`);
await userEvent.click(selector);
await waitFor(() => {
expect(container.querySelectorAll(`.${imagePrefix}`).length).toBe(3);
});
await waitFor(() => {
expect(handleMaxSizeExceed).toBeCalledTimes(1);
});
await waitFor(() => {
expect(handleLimitExceed).toBeCalledTimes(1);
});
});
});

0 comments on commit fa911bb

Please sign in to comment.