Skip to content

Commit

Permalink
feat: add test for image-picker
Browse files Browse the repository at this point in the history
  • Loading branch information
ziqisia committed Sep 20, 2023
1 parent fa911bb commit ee262e1
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions packages/arcodesign/components/image-picker/__test__/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';
import { render, waitFor, waitForElementToBeRemoved } from '@testing-library/react';
import { act, fireEvent, render, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import demoTest from '../../../tests/demoTest';
import mountTest from '../../../tests/mountTest';
import { createStartTouchEventObject } from '../../../tests/helpers/mockEvent';
import { defaultContext } from '../../context-provider';
import ImagePicker from '..';
import '@testing-library/jest-dom';
Expand All @@ -23,13 +24,15 @@ const mockImgFile = new File([IMG_DATA], 'img.png', { type: 'image/png' });

describe('ImagePicker', () => {
beforeEach(() => {
jest.useFakeTimers();
jest.spyOn(global, 'FileReader').mockImplementation(function () {
this.readAsDataURL = jest.fn();
this.onload = jest.fn();
this.onerror = jest.fn();
});
});
afterEach(() => {
jest.useRealTimers();
jest.restoreAllMocks();
});
it('ImagePicker renders correctly', () => {
Expand Down Expand Up @@ -161,6 +164,20 @@ describe('ImagePicker', () => {
userEvent.click(container.querySelector(`.${imagePrefix}`));
expect(handleClick).toBeCalledTimes(1);
});
it('press image correctly', () => {
const handleLongPress = jest.fn();
const { container } = render(
<ImagePicker images={[{ url: IMG_URL }]} onLongPress={handleLongPress} />,
);
act(() => {
fireEvent.touchStart(
container.querySelector(`.${prefix}-image-container`),
createStartTouchEventObject({ x: 0, y: 0 }),
);
jest.advanceTimersByTime(1000);
});
expect(handleLongPress).toBeCalledTimes(1);
});
it('selectAdapter', async () => {
const handleMaxSizeExceed = jest.fn();
const handleLimitExceed = jest.fn();
Expand Down Expand Up @@ -188,11 +205,7 @@ describe('ImagePicker', () => {
url: 'http://sf1-cdn-tos.toutiaostatic.com/obj/arco-mobile/_static_/large_image_3.jpg',
},
];
setTimeout(() => {
resolve({
files,
});
}, 1000);
resolve({ files });
});
};
return (
Expand Down

0 comments on commit ee262e1

Please sign in to comment.