ant-design/components/upload/__tests__/upload.test.tsx

996 lines
28 KiB
TypeScript
Raw Normal View History

import { produce } from 'immer';
import { cloneDeep } from 'lodash';
import type { UploadRequestOption } from 'rc-upload/lib/interface';
import React, { useEffect, useRef } from 'react';
import type { RcFile, UploadFile, UploadProps } from '..';
2017-02-22 16:06:08 +08:00
import Upload from '..';
2019-08-26 22:53:20 +08:00
import mountTest from '../../../tests/shared/mountTest';
feat: added rtl direction to all of ant-design components (#19380) * rtl demo change en-us description * change bundlesize css limit * RTL: modal component (exclude confirm) * RTL: table component * RTL: pagination component * cleanup rtl demo * fix pagination.tsx compile error * RTL: button and button-group * RTL: Steps component * fix rtl demo style * fix input suffix icon alignment * fix select component arrow issue * RTL: form component * add pagination rtl test * fix test lint error * RTL: rate component * RTL: radio and radio group components * RTL: tree-select component * some fixes to RTL components * RTL: badge component * fix rtl issue in inline form * fix input component rtl padding issue * fix switch component text rtl issue * fix table grouped header text-align * add rtl support to whole demo with RTL button * Update rtl demo responsive * RTL: page-header component * RTL: typography component * RTL: Dropdown (Partial) * update config-provider doc * RTL: input component * RTL: select component * RTL: switch component * RTL: tree component * fix rtl demo lint * rtl demo change en-us description * RTL: modal component (exclude confirm) * RTL: table component * RTL: pagination component * cleanup rtl demo * RTL: button and button-group * RTL: Steps component * fix rtl demo style * fix input suffix icon alignment * RTL: form component * RTL: rate component * RTL: radio and radio group components * RTL: tree-select component * RTL: badge component * fix rtl issue in inline form * fix input component rtl padding issue * add rtl support to whole demo with RTL button * fix lost changes after rebase * fix lint errors * RTL: Transfer Component * RTL: upload component * RTL: update avatar demo * RTL: comment component * RTL: collapse component * RTL: carousel component * update snapshots * RTL: Card component * RTL: descriptions component * RTL: Empty component * RTL: list component * RTL: slider component * slider component import/order * add shared rtlTest * RTL: Statistic component * RTL: tooltip components * RTL: popover component * RTL: timeline component * RTL: tag component * RTL: alert component * RTL: drawer component * RTL: Tab component * change direction definition * RTL: progress component * input.tsx, remove duplicate after rebase * fix demo.less after rebase * fix ant-row-rtl after rebase * fix upload issues in rtl * badge rtl demo margin fix * fix: tabs with icon margin * fix: radio-wrapper margin * fix: table component after rebase * fix: centered modal text-align * update slider snapshot * RTL: popconfirm component * RTL: back-top component * RTL: spin component * RTL: result component * RTL: skeleton component * RTL: menu component * RTL: time-picker component * RTL: calendar component * RTL: date-picker component * RTL: home page * update snapshots * test: add auto-complete rtl test * test: add avatar component rtl tests * test: add badge component rtl tests * test: add breadcrumb component rtl tests * test: add button components rtl tests * test: add card component rtl tests * test: add carousel component rtl tests * test: add cascader component rtl tests * test: add checkbox component rtl tests * test: add collapse component rtl tests * test: add comment component rtl tests * test: add dropdown component rtl tests * test: add empty component rtl tests * test: add form component rtl tests * test: add grid component rtl tests * test: add input component rtl tests * test: add search component rtl tests * test: add input-number component rtl tests * test: add layout component rtl tests * test: add list component rtl tests * test: add mentions component rtl tests * test: add modal component rtl tests * test: add page-header component rtl tests * test: add pagination component rtl tests * test: add radio component rtl tests * test: add rate component rtl tests * test: add select component rtl tests * test: add slider component rtl tests * test: add steps component rtl tests * test: add switch component rtl tests * test: add table component rtl tests * test: add transfer component rtl tests * test: add tree component rtl tests * test: add tree-select component rtl tests * test: add typography component rtl tests * test: add upload component rtl tests * test: add affix component rtl tests * update calendar tests * increase css file maxSize * update snapshots * remove workflows to allow push * remove duplicate reverse prop from slider * fix: remove table demo from config-provider * fix: remove table demo from config-provider * fix lint error * Added direction property to ConfigProvider * cascader rtl tests added * update config-provider doc * RTL: grid system * RTL: input component * RTL: switch component * fix rtl demo lint * RTL: modal component (exclude confirm) * RTL: table component * RTL: pagination component * cleanup rtl demo * fix pagination.tsx compile error * RTL: button and button-group * RTL: Steps component * fix rtl demo style * RTL: form component * add pagination rtl test * RTL: rate component * RTL: radio and radio group components * RTL: tree-select component * RTL: badge component * fix rtl issue in inline form * fix input component rtl padding issue * add rtl support to whole demo with RTL button * RTL: input component * RTL: select component * RTL: switch component * RTL: tree component * fix rtl demo lint * rtl demo change en-us description * RTL: modal component (exclude confirm) * RTL: table component * RTL: pagination component * cleanup rtl demo * RTL: button and button-group * RTL: Steps component * fix rtl demo style * fix input suffix icon alignment * RTL: form component * RTL: rate component * RTL: radio and radio group components * RTL: tree-select component * RTL: badge component * fix rtl issue in inline form * fix input component rtl padding issue * add rtl support to whole demo with RTL button * input.tsx, remove duplicate after rebase * fix ant-row-rtl after rebase * update snapshots * test: add cascader component rtl tests * test: add pagination component rtl tests * update calendar tests * update snapshots * fix: remove table demo from config-provider * fix: remove table demo from config-provider * fix lint error * update direction.md icons * dropdown and cascader default placement in rtl * update snapshots * fix lint errors * remove duplicate import * update snapshots * update snapshot * update calendar snapshot * update snapshots * integrate with new rc-picker * update snapshots * fix lint errors * update snapshot * update snapshots * update snapshots * update snapshots :| * update snapshots * fix compile error. * fix typo after rebase * update snapshots * remove workflows to allow push * update snapshots * update snapshots * fix dist error * front-page css fix * update snapshots * fix lint and test issues * restore cascader index.less * update snapshots * fix logo in rtl and demo controls * ci errors * resolve steps/index.tsx conflicts * tooltip family demo remove inline style * resolve table/Table.tsx conflicts * resolve modal/Modal.tsx conflicts * resolve cascader/index.tsx conflicts * add workflows from upstream * update snapshots * revert logo to default * fix codebox demo direction of placements * resolve tooltip overlayClassName conflicts * update snapshots * update popover test * fix: cascader miss popupClassName * fix: fix select missing dropdownClassName * chore: Update snapshot * chore: Adjust menu use rtl logic * docs: Update demo line color Co-authored-by: 二货机器人 <smith3816@gmail.com>
2020-01-02 19:10:16 +08:00
import rtlTest from '../../../tests/shared/rtlTest';
import { act, fireEvent, render, waitFakeTimer } from '../../../tests/utils';
2022-06-21 19:40:22 +08:00
import { resetWarned } from '../../_util/warning';
import Form from '../../form';
2022-06-21 19:40:22 +08:00
import { getFileItem, isImageUrl, removeFileItem } from '../utils';
import { setup, teardown } from './mock';
2017-02-22 16:06:08 +08:00
(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;
2017-02-22 16:06:08 +08:00
describe('Upload', () => {
2019-08-26 22:53:20 +08:00
mountTest(Upload);
feat: added rtl direction to all of ant-design components (#19380) * rtl demo change en-us description * change bundlesize css limit * RTL: modal component (exclude confirm) * RTL: table component * RTL: pagination component * cleanup rtl demo * fix pagination.tsx compile error * RTL: button and button-group * RTL: Steps component * fix rtl demo style * fix input suffix icon alignment * fix select component arrow issue * RTL: form component * add pagination rtl test * fix test lint error * RTL: rate component * RTL: radio and radio group components * RTL: tree-select component * some fixes to RTL components * RTL: badge component * fix rtl issue in inline form * fix input component rtl padding issue * fix switch component text rtl issue * fix table grouped header text-align * add rtl support to whole demo with RTL button * Update rtl demo responsive * RTL: page-header component * RTL: typography component * RTL: Dropdown (Partial) * update config-provider doc * RTL: input component * RTL: select component * RTL: switch component * RTL: tree component * fix rtl demo lint * rtl demo change en-us description * RTL: modal component (exclude confirm) * RTL: table component * RTL: pagination component * cleanup rtl demo * RTL: button and button-group * RTL: Steps component * fix rtl demo style * fix input suffix icon alignment * RTL: form component * RTL: rate component * RTL: radio and radio group components * RTL: tree-select component * RTL: badge component * fix rtl issue in inline form * fix input component rtl padding issue * add rtl support to whole demo with RTL button * fix lost changes after rebase * fix lint errors * RTL: Transfer Component * RTL: upload component * RTL: update avatar demo * RTL: comment component * RTL: collapse component * RTL: carousel component * update snapshots * RTL: Card component * RTL: descriptions component * RTL: Empty component * RTL: list component * RTL: slider component * slider component import/order * add shared rtlTest * RTL: Statistic component * RTL: tooltip components * RTL: popover component * RTL: timeline component * RTL: tag component * RTL: alert component * RTL: drawer component * RTL: Tab component * change direction definition * RTL: progress component * input.tsx, remove duplicate after rebase * fix demo.less after rebase * fix ant-row-rtl after rebase * fix upload issues in rtl * badge rtl demo margin fix * fix: tabs with icon margin * fix: radio-wrapper margin * fix: table component after rebase * fix: centered modal text-align * update slider snapshot * RTL: popconfirm component * RTL: back-top component * RTL: spin component * RTL: result component * RTL: skeleton component * RTL: menu component * RTL: time-picker component * RTL: calendar component * RTL: date-picker component * RTL: home page * update snapshots * test: add auto-complete rtl test * test: add avatar component rtl tests * test: add badge component rtl tests * test: add breadcrumb component rtl tests * test: add button components rtl tests * test: add card component rtl tests * test: add carousel component rtl tests * test: add cascader component rtl tests * test: add checkbox component rtl tests * test: add collapse component rtl tests * test: add comment component rtl tests * test: add dropdown component rtl tests * test: add empty component rtl tests * test: add form component rtl tests * test: add grid component rtl tests * test: add input component rtl tests * test: add search component rtl tests * test: add input-number component rtl tests * test: add layout component rtl tests * test: add list component rtl tests * test: add mentions component rtl tests * test: add modal component rtl tests * test: add page-header component rtl tests * test: add pagination component rtl tests * test: add radio component rtl tests * test: add rate component rtl tests * test: add select component rtl tests * test: add slider component rtl tests * test: add steps component rtl tests * test: add switch component rtl tests * test: add table component rtl tests * test: add transfer component rtl tests * test: add tree component rtl tests * test: add tree-select component rtl tests * test: add typography component rtl tests * test: add upload component rtl tests * test: add affix component rtl tests * update calendar tests * increase css file maxSize * update snapshots * remove workflows to allow push * remove duplicate reverse prop from slider * fix: remove table demo from config-provider * fix: remove table demo from config-provider * fix lint error * Added direction property to ConfigProvider * cascader rtl tests added * update config-provider doc * RTL: grid system * RTL: input component * RTL: switch component * fix rtl demo lint * RTL: modal component (exclude confirm) * RTL: table component * RTL: pagination component * cleanup rtl demo * fix pagination.tsx compile error * RTL: button and button-group * RTL: Steps component * fix rtl demo style * RTL: form component * add pagination rtl test * RTL: rate component * RTL: radio and radio group components * RTL: tree-select component * RTL: badge component * fix rtl issue in inline form * fix input component rtl padding issue * add rtl support to whole demo with RTL button * RTL: input component * RTL: select component * RTL: switch component * RTL: tree component * fix rtl demo lint * rtl demo change en-us description * RTL: modal component (exclude confirm) * RTL: table component * RTL: pagination component * cleanup rtl demo * RTL: button and button-group * RTL: Steps component * fix rtl demo style * fix input suffix icon alignment * RTL: form component * RTL: rate component * RTL: radio and radio group components * RTL: tree-select component * RTL: badge component * fix rtl issue in inline form * fix input component rtl padding issue * add rtl support to whole demo with RTL button * input.tsx, remove duplicate after rebase * fix ant-row-rtl after rebase * update snapshots * test: add cascader component rtl tests * test: add pagination component rtl tests * update calendar tests * update snapshots * fix: remove table demo from config-provider * fix: remove table demo from config-provider * fix lint error * update direction.md icons * dropdown and cascader default placement in rtl * update snapshots * fix lint errors * remove duplicate import * update snapshots * update snapshot * update calendar snapshot * update snapshots * integrate with new rc-picker * update snapshots * fix lint errors * update snapshot * update snapshots * update snapshots * update snapshots :| * update snapshots * fix compile error. * fix typo after rebase * update snapshots * remove workflows to allow push * update snapshots * update snapshots * fix dist error * front-page css fix * update snapshots * fix lint and test issues * restore cascader index.less * update snapshots * fix logo in rtl and demo controls * ci errors * resolve steps/index.tsx conflicts * tooltip family demo remove inline style * resolve table/Table.tsx conflicts * resolve modal/Modal.tsx conflicts * resolve cascader/index.tsx conflicts * add workflows from upstream * update snapshots * revert logo to default * fix codebox demo direction of placements * resolve tooltip overlayClassName conflicts * update snapshots * update popover test * fix: cascader miss popupClassName * fix: fix select missing dropdownClassName * chore: Update snapshot * chore: Adjust menu use rtl logic * docs: Update demo line color Co-authored-by: 二货机器人 <smith3816@gmail.com>
2020-01-02 19:10:16 +08:00
rtlTest(Upload);
2019-08-26 22:53:20 +08:00
2022-10-31 10:16:51 +08:00
beforeAll(() => {
jest.useFakeTimers();
});
2018-05-31 22:37:09 +08:00
beforeEach(() => setup());
2022-10-31 10:16:51 +08:00
afterAll(() => {
jest.useRealTimers();
});
afterEach(() => {
jest.clearAllTimers();
return teardown();
});
2018-05-31 22:37:09 +08:00
// Mock for rc-util raf
window.requestAnimationFrame = (callback) => window.setTimeout(callback, 16);
window.cancelAnimationFrame = (id) => window.clearTimeout(id);
2017-02-22 16:06:08 +08:00
// https://github.com/react-component/upload/issues/36
it('should get refs inside Upload in componentDidMount', () => {
2022-10-31 10:16:51 +08:00
let ref: React.RefObject<HTMLInputElement>;
const App: React.FC = () => {
const inputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
ref = inputRef;
}, []);
return (
<Upload supportServerRender={false}>
<input ref={inputRef} />
</Upload>
);
};
render(<App />);
expect(ref!).toBeDefined();
2017-02-22 16:06:08 +08:00
});
it('return promise in beforeUpload', async () => {
const data = jest.fn();
const done = jest.fn();
const props: UploadProps = {
action: 'http://upload.com',
2021-11-26 15:19:31 +08:00
beforeUpload: () =>
new Promise((resolve) => {
2021-11-26 15:19:31 +08:00
setTimeout(() => resolve('success'), 100);
}),
data,
onChange({ file }) {
if (file.status !== 'uploading') {
expect(data).toHaveBeenCalled();
done();
}
},
};
const { container: wrapper } = render(
<Upload {...props}>
<button type="button">upload</button>
2018-12-07 16:17:45 +08:00
</Upload>,
);
fireEvent.change(wrapper.querySelector('input')!, {
target: { files: [{ file: 'foo.png' }] },
});
2022-10-31 10:16:51 +08:00
await waitFakeTimer();
expect(done).toHaveBeenCalled();
});
it('beforeUpload can be falsy', async () => {
const done = jest.fn();
const props: UploadProps = {
action: 'http://upload.com',
beforeUpload: () => false,
onChange: ({ file }) => {
if (file.status !== 'uploading') {
done();
}
},
};
const { container: wrapper } = render(
<Upload {...props}>
<button type="button">upload</button>
</Upload>,
);
fireEvent.change(wrapper.querySelector('input')!, {
target: { files: [{ file: 'foo.png' }] },
});
2022-10-31 10:16:51 +08:00
await waitFakeTimer();
expect(done).toHaveBeenCalled();
});
it('upload promise return file in beforeUpload', async () => {
const done = jest.fn();
const data = jest.fn();
const props: UploadProps = {
action: 'http://upload.com',
beforeUpload: (file) =>
new Promise((resolve) => {
setTimeout(() => {
const result = file;
(result as any).name = 'test.png';
resolve(result);
}, 100);
}),
data,
onChange: ({ file }) => {
if (file.status !== 'uploading') {
expect(data).toHaveBeenCalled();
expect(file.name).toEqual('test.png');
done();
}
},
};
const { container: wrapper } = render(
<Upload {...props}>
<button type="button">upload</button>
</Upload>,
);
fireEvent.change(wrapper.querySelector('input')!, {
target: { files: [{ file: 'foo.png' }] },
});
2022-10-31 10:16:51 +08:00
await waitFakeTimer();
expect(done).toHaveBeenCalled();
});
it('should not stop upload when return value of beforeUpload is false', (done) => {
2018-12-07 16:17:45 +08:00
const fileList = [
{
uid: 'bar',
name: 'bar.png',
},
];
const mockFile = new File(['foo'], 'foo.png', {
type: 'image/png',
});
const data = jest.fn();
const props: UploadProps = {
action: 'http://upload.com',
fileList,
beforeUpload: () => false,
data,
onChange: ({ file, fileList: updatedFileList }) => {
expect(file instanceof File).toBe(true);
expect(updatedFileList.map((f) => f.name)).toEqual(['bar.png', 'foo.png']);
expect(data).not.toHaveBeenCalled();
done();
},
};
const { container: wrapper } = render(
<Upload {...props}>
<button type="button">upload</button>
2018-12-07 16:17:45 +08:00
</Upload>,
);
fireEvent.change(wrapper.querySelector('input')!, {
target: { files: [mockFile] },
});
});
it('should not stop upload when return value of beforeUpload is not false', (done) => {
const data = jest.fn();
const props = {
action: 'http://upload.com',
beforeUpload() {},
data,
onChange: () => {
expect(data).toHaveBeenCalled();
done();
},
};
const { container: wrapper } = render(
<Upload {...props}>
<button type="button">upload</button>
2018-12-07 16:17:45 +08:00
</Upload>,
);
fireEvent.change(wrapper.querySelector('input')!, {
target: {
2018-12-07 16:17:45 +08:00
files: [{ file: 'foo.png' }],
},
});
});
2018-05-02 13:56:54 +08:00
// https://github.com/ant-design/ant-design/issues/14779
it('should contain input file control if upload button is hidden', () => {
const { container, rerender } = render(
<Upload action="http://upload.com">
<button type="button">upload</button>
</Upload>,
);
expect(container.querySelectorAll('input[type="file"]')).toHaveLength(1);
rerender(<Upload action="http://upload.com" />);
expect(container.querySelectorAll('input[type="file"]')).toHaveLength(1);
});
// https://github.com/ant-design/ant-design/issues/14298
it('should not have id if upload children is null, avoid being triggered by label', () => {
const Demo: React.FC<{ children?: UploadProps['children'] }> = ({ children }) => (
<Form>
2019-08-29 23:41:46 +08:00
<Form.Item name="upload" label="Upload" valuePropName="fileList">
<Upload>{children}</Upload>
</Form.Item>
</Form>
);
const { container, rerender } = render(
<Demo>
<div>upload</div>
</Demo>,
);
expect(container.querySelector('input#upload')).toBeTruthy();
rerender(<Demo />);
expect(container.querySelector('input#upload')).toBeFalsy();
});
// https://github.com/ant-design/ant-design/issues/16478
it('should not have id if Upload is disabled, avoid being triggered by label', () => {
const Demo: React.FC<{ disabled?: UploadProps['disabled'] }> = ({ disabled }) => (
<Form>
2019-08-29 23:41:46 +08:00
<Form.Item name="upload" label="Upload" valuePropName="fileList">
<Upload disabled={disabled}>
<div>upload</div>
</Upload>
</Form.Item>
</Form>
);
const { container: wrapper, rerender } = render(<Demo />);
expect(wrapper.querySelectorAll('input#upload').length).toBe(1);
rerender(<Demo disabled />);
expect(wrapper.querySelectorAll('input#upload').length).toBe(0);
});
// https://github.com/ant-design/ant-design/issues/24197
it('should not have id if upload.Dragger is disabled, avoid being triggered by label', () => {
const Demo: React.FC<{ disabled?: UploadProps['disabled'] }> = ({ disabled }) => (
<Form>
<Form.Item name="upload" label="Upload" valuePropName="fileList">
<Upload.Dragger disabled={disabled}>
<div>upload</div>
</Upload.Dragger>
</Form.Item>
</Form>
);
const { container: wrapper, rerender } = render(<Demo />);
expect(wrapper.querySelectorAll('input#upload').length).toBe(1);
rerender(<Demo disabled />);
expect(wrapper.querySelectorAll('input#upload').length).toBe(0);
});
2022-10-31 10:16:51 +08:00
it('should be controlled by fileList', async () => {
2018-12-07 16:17:45 +08:00
const fileList = [
{
uid: '-1',
name: 'foo.png',
status: 'done',
url: 'http://www.baidu.com/xxx.png',
},
];
const ref = React.createRef<any>();
const { rerender } = render(<Upload ref={ref} />);
expect(ref.current.fileList).toEqual([]);
rerender(<Upload ref={ref} fileList={fileList as UploadProps['fileList']} />);
2022-10-31 10:16:51 +08:00
await waitFakeTimer();
expect(ref.current.fileList).toEqual(fileList);
});
it('should be able to get uid at first', () => {
const fileList = [
{
name: 'foo.png',
status: 'done',
url: 'http://www.baidu.com/xxx.png',
},
];
render(<Upload fileList={fileList as UploadProps['fileList']} />);
(fileList as UploadProps['fileList'])?.forEach((file) => {
expect(file.uid).toBeDefined();
});
});
2018-05-02 13:56:54 +08:00
describe('util', () => {
2018-12-01 18:30:05 +08:00
it('should be able to get fileItem', () => {
const file = { uid: '-1', name: 'item.jpg' } as RcFile;
2018-12-07 16:17:45 +08:00
const fileList = [
{
uid: '-1',
name: 'item.jpg',
},
];
2018-12-01 18:30:05 +08:00
const targetItem = getFileItem(file, fileList);
expect(targetItem).toBe(fileList[0]);
});
it('should be able to remove fileItem', () => {
const file = { uid: '-1', name: 'item.jpg' };
2018-12-07 16:17:45 +08:00
const fileList = [
{
uid: '-1',
name: 'item.jpg',
},
{
uid: '-2',
name: 'item2.jpg',
},
];
2018-12-01 18:30:05 +08:00
const targetItem = removeFileItem(file, fileList);
expect(targetItem).toEqual(fileList.slice(1));
});
merge Feature into master (#29758) * feat: add onCancel and onEnd option for editable (#29615) * feature: add onCancel and onEnd option for editable * doc: editable * doc: add EN doc * optimize: code Co-authored-by: chenliang <chenliang9@xiaomi.com> * feat: add parent class for different notification types (#29634) close #29417 * refactor: Upload use origin behavior (#29737) * refactor: Fallback of events * test: Fix test case * fix: Start file update logic * fix: remove status update * test: Remove wrapTest * test: Fix test case * chore: bump rc-upload * docs: About desc * feat: tab support moreIcon (#29744) * feat: Tabs support moreIcon * docs: Tabs support moreIcon * style: lint * docs: add english document * Update components/tabs/index.zh-CN.md Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> * Update components/tabs/index.en-US.md Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> * Update components/tabs/index.zh-CN.md * Update components/tabs/index.en-US.md Co-authored-by: zty <zty.dev@outlook.com> Co-authored-by: zty <zty.dev@icloud.com> Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: jueinin <1014397160@qq.com> Co-authored-by: chenliang <chenliang9@xiaomi.com> Co-authored-by: 不吃猫的鱼 <michael2ib1989@gmail.com> Co-authored-by: 二货机器人 <smith3816@gmail.com> Co-authored-by: Tianyuan Zhang <tianyuan233.zhang@gmail.com> Co-authored-by: zty <zty.dev@outlook.com> Co-authored-by: zty <zty.dev@icloud.com> Co-authored-by: xrkffgg <xrkffgg@vip.qq.com>
2021-03-13 23:46:32 +08:00
it('remove fileItem and fileList with immutable data', () => {
const file = { uid: '-3', name: 'item3.jpg' };
const fileList = produce(
[
{
uid: '-1',
name: 'item.jpg',
},
{
uid: '-2',
name: 'item2.jpg',
},
],
(draftState) => {
draftState.push({
uid: '-3',
name: 'item3.jpg',
});
},
);
const targetItem = removeFileItem(file, fileList);
expect(targetItem).toEqual(fileList.slice(0, 2));
});
2021-01-16 18:06:26 +08:00
2018-12-01 18:30:05 +08:00
it('should not be able to remove fileItem', () => {
const file = { uid: '-3', name: 'item.jpg' };
2018-12-07 16:17:45 +08:00
const fileList = [
{
uid: '-1',
name: 'item.jpg',
},
{
uid: '-2',
name: 'item2.jpg',
},
];
2018-12-01 18:30:05 +08:00
const targetItem = removeFileItem(file, fileList);
expect(targetItem).toBe(null);
});
it('isImageUrl should work correctly when file.url is null', () => {
const file = { url: null } as unknown as UploadFile;
expect(isImageUrl(file)).toBe(true);
});
2018-05-02 13:56:54 +08:00
});
it('should support linkProps as object', () => {
2018-12-07 16:17:45 +08:00
const fileList = [
{
uid: '-1',
name: 'foo.png',
status: 'done',
url: 'http://www.baidu.com/xxx.png',
linkProps: {
download: 'image',
rel: 'noopener',
},
},
2018-12-07 16:17:45 +08:00
];
const { container: wrapper } = render(
<Upload fileList={fileList as UploadProps['fileList']} />,
);
const linkNode = wrapper.querySelector('a.ant-upload-list-item-name');
expect(linkNode?.getAttribute('download')).toBe('image');
expect(linkNode?.getAttribute('rel')).toBe('noopener');
});
it('should support linkProps as json stringify', () => {
const linkPropsString = JSON.stringify({
download: 'image',
rel: 'noopener',
});
2018-12-07 16:17:45 +08:00
const fileList = [
{
uid: '-1',
name: 'foo.png',
status: 'done',
url: 'http://www.baidu.com/xxx.png',
linkProps: linkPropsString,
},
];
const { container: wrapper } = render(
<Upload fileList={fileList as UploadProps['fileList']} />,
);
const linkNode = wrapper.querySelector('a.ant-upload-list-item-name');
expect(linkNode?.getAttribute('download')).toBe('image');
expect(linkNode?.getAttribute('rel')).toBe('noopener');
});
2019-01-12 18:08:10 +08:00
2022-10-31 10:16:51 +08:00
it('should stop remove when return value of onRemove is false', async () => {
2019-01-12 18:08:10 +08:00
const mockRemove = jest.fn(() => false);
const props: UploadProps = {
2019-01-12 18:08:10 +08:00
onRemove: mockRemove,
fileList: [
{
uid: '-1',
name: 'foo.png',
status: 'done',
url: 'http://www.baidu.com/xxx.png',
},
],
};
const { container: wrapper } = render(<Upload {...props} />);
2019-01-12 18:08:10 +08:00
fireEvent.click(wrapper.querySelector('div.ant-upload-list-item .anticon-delete')!);
2019-01-12 18:08:10 +08:00
2022-10-31 10:16:51 +08:00
await waitFakeTimer();
expect(mockRemove).toHaveBeenCalled();
expect(props.fileList).toHaveLength(1);
expect(props.fileList?.[0]?.status).toBe('done');
2019-01-12 18:08:10 +08:00
});
// https://github.com/ant-design/ant-design/issues/18902
it('should not abort uploading until return value of onRemove is resolved as true', async () => {
const file = {
uid: '-1',
name: 'foo.png',
status: 'uploading',
url: 'http://www.baidu.com/xxx.png',
};
let removePromise: (value: boolean | Promise<void | boolean>) => void;
const onRemove: UploadProps['onRemove'] = () =>
new Promise((resolve) => {
expect(file.status).toBe('uploading');
removePromise = resolve;
});
const onChange = jest.fn();
const { container } = render(
<Upload
fileList={[file] as UploadProps['fileList']}
onChange={onChange}
onRemove={onRemove}
/>,
);
fireEvent.click(container.querySelector('div.ant-upload-list-item .anticon-delete')!);
// Delay return true for remove
2022-10-31 10:16:51 +08:00
await waitFakeTimer();
await act(async () => {
await removePromise(true);
});
expect(onChange).toHaveBeenCalled();
expect(file.status).toBe('removed');
});
2022-10-31 10:16:51 +08:00
it('should not stop download when return use onDownload', async () => {
const mockRemove = jest.fn(() => false);
const props: UploadProps = {
onRemove: mockRemove,
showUploadList: {
showDownloadIcon: true,
},
fileList: [
{
uid: '-1',
name: 'foo.png',
status: 'done',
url: 'http://www.baidu.com/xxx.png',
},
],
};
const { container: wrapper } = render(<Upload {...props} onDownload={() => {}} />);
fireEvent.click(wrapper.querySelector('div.ant-upload-list-item .anticon-download')!);
2022-10-31 10:16:51 +08:00
await waitFakeTimer();
expect(props.fileList).toHaveLength(1);
expect(props.fileList?.[0]?.status).toBe('done');
});
// https://github.com/ant-design/ant-design/issues/14439
it('should allow call abort function through upload instance', () => {
const ref = React.createRef<any>();
render(
<Upload ref={ref}>
<button type="button">upload</button>
</Upload>,
);
expect(typeof ref.current?.upload.abort).toBe('function');
2019-03-09 13:36:16 +08:00
});
it('correct dragCls when type is drag', () => {
2019-03-09 13:36:16 +08:00
const fileList = [{ status: 'uploading', uid: 'file' }];
const { container: wrapper } = render(
<Upload type="drag" fileList={fileList as UploadProps['fileList']}>
2019-03-09 13:36:16 +08:00
<button type="button">upload</button>
</Upload>,
);
expect(wrapper.querySelectorAll('.ant-upload-drag-uploading').length).toBe(1);
2019-03-09 13:36:16 +08:00
});
it('return when targetItem is null', () => {
const fileList = [{ uid: 'file' }];
const ref = React.createRef<any>();
render(
<Upload ref={ref} type="drag" fileList={fileList as UploadProps['fileList']}>
2019-03-09 13:36:16 +08:00
<button type="button">upload</button>
</Upload>,
);
expect(ref.current?.onSuccess('', { uid: 'fileItem' })).toBe(undefined);
expect(ref.current?.onProgress('', { uid: 'fileItem' })).toBe(undefined);
expect(ref.current?.onError('', '', { uid: 'fileItem' })).toBe(undefined);
});
it('should replace file when targetItem already exists', () => {
const fileList = [{ uid: 'file', name: 'file' }];
const ref = React.createRef<any>();
const { unmount } = render(
<Upload ref={ref} defaultFileList={fileList}>
<button type="button">upload</button>
</Upload>,
);
merge Feature into master (#29758) * feat: add onCancel and onEnd option for editable (#29615) * feature: add onCancel and onEnd option for editable * doc: editable * doc: add EN doc * optimize: code Co-authored-by: chenliang <chenliang9@xiaomi.com> * feat: add parent class for different notification types (#29634) close #29417 * refactor: Upload use origin behavior (#29737) * refactor: Fallback of events * test: Fix test case * fix: Start file update logic * fix: remove status update * test: Remove wrapTest * test: Fix test case * chore: bump rc-upload * docs: About desc * feat: tab support moreIcon (#29744) * feat: Tabs support moreIcon * docs: Tabs support moreIcon * style: lint * docs: add english document * Update components/tabs/index.zh-CN.md Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> * Update components/tabs/index.en-US.md Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> * Update components/tabs/index.zh-CN.md * Update components/tabs/index.en-US.md Co-authored-by: zty <zty.dev@outlook.com> Co-authored-by: zty <zty.dev@icloud.com> Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: jueinin <1014397160@qq.com> Co-authored-by: chenliang <chenliang9@xiaomi.com> Co-authored-by: 不吃猫的鱼 <michael2ib1989@gmail.com> Co-authored-by: 二货机器人 <smith3816@gmail.com> Co-authored-by: Tianyuan Zhang <tianyuan233.zhang@gmail.com> Co-authored-by: zty <zty.dev@outlook.com> Co-authored-by: zty <zty.dev@icloud.com> Co-authored-by: xrkffgg <xrkffgg@vip.qq.com>
2021-03-13 23:46:32 +08:00
const newFile = {
uid: 'file',
name: 'file1',
merge Feature into master (#29758) * feat: add onCancel and onEnd option for editable (#29615) * feature: add onCancel and onEnd option for editable * doc: editable * doc: add EN doc * optimize: code Co-authored-by: chenliang <chenliang9@xiaomi.com> * feat: add parent class for different notification types (#29634) close #29417 * refactor: Upload use origin behavior (#29737) * refactor: Fallback of events * test: Fix test case * fix: Start file update logic * fix: remove status update * test: Remove wrapTest * test: Fix test case * chore: bump rc-upload * docs: About desc * feat: tab support moreIcon (#29744) * feat: Tabs support moreIcon * docs: Tabs support moreIcon * style: lint * docs: add english document * Update components/tabs/index.zh-CN.md Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> * Update components/tabs/index.en-US.md Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> * Update components/tabs/index.zh-CN.md * Update components/tabs/index.en-US.md Co-authored-by: zty <zty.dev@outlook.com> Co-authored-by: zty <zty.dev@icloud.com> Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: jueinin <1014397160@qq.com> Co-authored-by: chenliang <chenliang9@xiaomi.com> Co-authored-by: 不吃猫的鱼 <michael2ib1989@gmail.com> Co-authored-by: 二货机器人 <smith3816@gmail.com> Co-authored-by: Tianyuan Zhang <tianyuan233.zhang@gmail.com> Co-authored-by: zty <zty.dev@outlook.com> Co-authored-by: zty <zty.dev@icloud.com> Co-authored-by: xrkffgg <xrkffgg@vip.qq.com>
2021-03-13 23:46:32 +08:00
};
act(() => {
ref.current?.onBatchStart([
merge Feature into master (#29758) * feat: add onCancel and onEnd option for editable (#29615) * feature: add onCancel and onEnd option for editable * doc: editable * doc: add EN doc * optimize: code Co-authored-by: chenliang <chenliang9@xiaomi.com> * feat: add parent class for different notification types (#29634) close #29417 * refactor: Upload use origin behavior (#29737) * refactor: Fallback of events * test: Fix test case * fix: Start file update logic * fix: remove status update * test: Remove wrapTest * test: Fix test case * chore: bump rc-upload * docs: About desc * feat: tab support moreIcon (#29744) * feat: Tabs support moreIcon * docs: Tabs support moreIcon * style: lint * docs: add english document * Update components/tabs/index.zh-CN.md Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> * Update components/tabs/index.en-US.md Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> * Update components/tabs/index.zh-CN.md * Update components/tabs/index.en-US.md Co-authored-by: zty <zty.dev@outlook.com> Co-authored-by: zty <zty.dev@icloud.com> Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: jueinin <1014397160@qq.com> Co-authored-by: chenliang <chenliang9@xiaomi.com> Co-authored-by: 不吃猫的鱼 <michael2ib1989@gmail.com> Co-authored-by: 二货机器人 <smith3816@gmail.com> Co-authored-by: Tianyuan Zhang <tianyuan233.zhang@gmail.com> Co-authored-by: zty <zty.dev@outlook.com> Co-authored-by: zty <zty.dev@icloud.com> Co-authored-by: xrkffgg <xrkffgg@vip.qq.com>
2021-03-13 23:46:32 +08:00
{
file: newFile,
parsedFile: newFile,
},
]);
});
merge Feature into master (#29758) * feat: add onCancel and onEnd option for editable (#29615) * feature: add onCancel and onEnd option for editable * doc: editable * doc: add EN doc * optimize: code Co-authored-by: chenliang <chenliang9@xiaomi.com> * feat: add parent class for different notification types (#29634) close #29417 * refactor: Upload use origin behavior (#29737) * refactor: Fallback of events * test: Fix test case * fix: Start file update logic * fix: remove status update * test: Remove wrapTest * test: Fix test case * chore: bump rc-upload * docs: About desc * feat: tab support moreIcon (#29744) * feat: Tabs support moreIcon * docs: Tabs support moreIcon * style: lint * docs: add english document * Update components/tabs/index.zh-CN.md Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> * Update components/tabs/index.en-US.md Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> * Update components/tabs/index.zh-CN.md * Update components/tabs/index.en-US.md Co-authored-by: zty <zty.dev@outlook.com> Co-authored-by: zty <zty.dev@icloud.com> Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: jueinin <1014397160@qq.com> Co-authored-by: chenliang <chenliang9@xiaomi.com> Co-authored-by: 不吃猫的鱼 <michael2ib1989@gmail.com> Co-authored-by: 二货机器人 <smith3816@gmail.com> Co-authored-by: Tianyuan Zhang <tianyuan233.zhang@gmail.com> Co-authored-by: zty <zty.dev@outlook.com> Co-authored-by: zty <zty.dev@icloud.com> Co-authored-by: xrkffgg <xrkffgg@vip.qq.com>
2021-03-13 23:46:32 +08:00
expect(ref.current.fileList.length).toBe(1);
expect(ref.current.fileList[0].originFileObj).toEqual({
name: 'file1',
uid: 'file',
});
merge Feature into master (#29758) * feat: add onCancel and onEnd option for editable (#29615) * feature: add onCancel and onEnd option for editable * doc: editable * doc: add EN doc * optimize: code Co-authored-by: chenliang <chenliang9@xiaomi.com> * feat: add parent class for different notification types (#29634) close #29417 * refactor: Upload use origin behavior (#29737) * refactor: Fallback of events * test: Fix test case * fix: Start file update logic * fix: remove status update * test: Remove wrapTest * test: Fix test case * chore: bump rc-upload * docs: About desc * feat: tab support moreIcon (#29744) * feat: Tabs support moreIcon * docs: Tabs support moreIcon * style: lint * docs: add english document * Update components/tabs/index.zh-CN.md Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> * Update components/tabs/index.en-US.md Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> * Update components/tabs/index.zh-CN.md * Update components/tabs/index.en-US.md Co-authored-by: zty <zty.dev@outlook.com> Co-authored-by: zty <zty.dev@icloud.com> Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: jueinin <1014397160@qq.com> Co-authored-by: chenliang <chenliang9@xiaomi.com> Co-authored-by: 不吃猫的鱼 <michael2ib1989@gmail.com> Co-authored-by: 二货机器人 <smith3816@gmail.com> Co-authored-by: Tianyuan Zhang <tianyuan233.zhang@gmail.com> Co-authored-by: zty <zty.dev@outlook.com> Co-authored-by: zty <zty.dev@icloud.com> Co-authored-by: xrkffgg <xrkffgg@vip.qq.com>
2021-03-13 23:46:32 +08:00
unmount();
2019-03-09 13:36:16 +08:00
});
it('warning if set `value`', () => {
resetWarned();
const value = { value: [] } as any;
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
render(<Upload {...value} />);
expect(errorSpy).toHaveBeenCalledWith(
'Warning: [antd: Upload] `value` is not a valid prop, do you mean `fileList`?',
);
errorSpy.mockRestore();
});
2020-09-15 11:54:47 +08:00
it('should be treated as file but not an image', () => {
docs: Site with single paging (#21360) * patch route * use data for promise * rename Article file * Article in TS * clean up * use collect * adjust css * card it * listing * res * hiring * add footer * english it * rm others * fix lint * fix navigation * add dropdown button * Header with TS * split input & logo * navigation out * use flex * flex everything * stretch * hide search when narrow * init category * global rules * more cat * pages * level 2 width * level 3 * level 4 * skip before ready * clean up * unqiue shadow variable * update eslintignore * update ignore * fix ssr * fix location param * replace images * shake more * fix top of navigation * remove home * reorder nav & adjust padding style * narrow res style * Update research-form-page.zh-CN.md (#21426) Co-authored-by: Golevka <huangsamfisher@163.com> * rm skip * narrow merge in * reset p style * update svg * fix lint * update ignore * update lint ignore * Update research-result-page.zh-CN.md (#21432) Co-authored-by: mihaideyu <zxy1010414746@163.com> * Update research-result-page.zh-CN.md * Update research-workbench.zh-CN.md (#21438) Co-authored-by: yingxirz <inseeing@gmail.com> * Update research-workbench.zh-CN.md * Update research-list-page.zh-CN.md (#21441) Co-authored-by: yingxirz <inseeing@gmail.com> * Update research-list-page.zh-CN.md * padding use 40px * add responsive of ref * fix p margin * Update research-workbench.zh-CN.md * Update buttons.md (#21456) Co-authored-by: yingxirz <inseeing@gmail.com> * Update resources.en-US.md (#21457) * Update research-navigation.zh-CN.md (#21453) * Update research-navigation.zh-CN.md * Update research-navigation.zh-CN.md * Update research-navigation.zh-CN.md * Update research-empty.zh-CN.md (#21451) * Update research-empty.zh-CN.md * Update research-empty.zh-CN.md * Update research-empty.zh-CN.md * Update research-empty.zh-CN.md Co-authored-by: 二货机器人 <smith3816@gmail.com> * Update research-exception-page.zh-CN.md (#21452) * Update research-exception-page.zh-CN.md * Update research-exception-page.zh-CN.md * Update research-message-and-feedback.zh-CN.md (#21463) * Update research-message-and-feedback.zh-CN.md (#21467) * Update research-message-and-feedback.zh-CN.md * Update research-message-and-feedback.zh-CN.md * Update research-message-and-feedback.zh-CN.md * Update research-message-and-feedback.zh-CN.md * replace background * fix lines margin * home padding * patch minTilda doc * Update resources.zh-CN.md (#21503) * clean up * Update research-workbench.zh-CN.md (#21512) Co-authored-by: yingxirz <inseeing@gmail.com> * Update research-overview.zh-CN.md (#21513) Co-authored-by: yingxirz <inseeing@gmail.com> * Update research-result-page.zh-CN.md (#21514) Co-authored-by: mihaideyu <zxy1010414746@163.com> * Update research-list-page.zh-CN.md (#21515) Co-authored-by: yingxirz <inseeing@gmail.com> * fix doc * Update research-message-and-feedback.zh-CN.md (#21516) * adjust home nav width * Update research-message-and-feedback.zh-CN.md (#21535) * Update research-form-page.zh-CN.md (#21554) Co-authored-by: Golevka <huangsamfisher@163.com> * patch file * clean up * update shadow.zh-CN update shadow.zh-CN * add shadow table * Update visual.zh-CN.md (#21577) Co-authored-by: shuwenliu0116 <33922479+shuwenliu0116@users.noreply.github.com> * Update docs/spec/shadow.zh-CN.md Co-Authored-By: 偏右 <afc163@gmail.com> * Update docs/spec/shadow.zh-CN.md Co-Authored-By: 偏右 <afc163@gmail.com> * Update docs/spec/shadow.zh-CN.md Co-Authored-By: 偏右 <afc163@gmail.com> * Update docs/spec/shadow.zh-CN.md Co-Authored-By: 偏右 <afc163@gmail.com> * Update docs/spec/shadow.zh-CN.md Co-Authored-By: 偏右 <afc163@gmail.com> * patch docs * fix doc * rm page suffix * fix style * mobile it * Update resources.en-US.md (#21561) * chart res * add qr code * adjust qr * Update visual.zh-CN.md (#21603) Co-authored-by: shuwenliu0116 <33922479+shuwenliu0116@users.noreply.github.com> * fix site qr * mobile height of it * book update * 3.x site link * isMobile is new cnotext * Update research-form.zh-CN.md (#21604) Co-authored-by: Golevka <huangsamfisher@163.com> * adjust form doc * mobile use link * adjust order * flush * fix ts * Update research-navigation.zh-CN.md (#21606) * Update research-navigation.zh-CN.md (#21609) * rm useless lines * add mobile icon * Update research-exception.zh-CN.md (#21612) * Update research-empty.zh-CN.md (#21610) * Update research-workbench.zh-CN.md * Update visual.zh-CN.md * adjust mobile icon * Update research-message-and-feedback.zh-CN.md (#21613) * Update research-list.zh-CN.md (#21617) Co-authored-by: yingxirz <inseeing@gmail.com> * Update research-workbench.zh-CN.md (#21618) Co-authored-by: yingxirz <inseeing@gmail.com> * Update visual.zh-CN.md (#21619) Co-authored-by: shuwenliu0116 <33922479+shuwenliu0116@users.noreply.github.com> * Update research-result.zh-CN.md (#21620) Co-authored-by: mihaideyu <zxy1010414746@163.com> * Update research-form.zh-CN.md (#21615) * Update research-message-and-feedback.zh-CN.md (#21616) * Update research-list.zh-CN.md (#21621) Co-authored-by: yingxirz <inseeing@gmail.com> * Update research-result.zh-CN.md (#21624) Co-authored-by: mihaideyu <zxy1010414746@163.com> * update ref color * update zip file * fix mobile icon * rm skip dark * fix cat * update zip link * update zip link Co-authored-by: Golevka <huangsamfisher@163.com> Co-authored-by: mihaideyu <zxy1010414746@163.com> Co-authored-by: yingxirz <inseeing@gmail.com> Co-authored-by: 偏右 <afc163@gmail.com> Co-authored-by: myeunhyuk <39618466+myeunhyuk@users.noreply.github.com> Co-authored-by: minTilda <31951290+minTilda@users.noreply.github.com> Co-authored-by: ziyuan-174774 <61226089+ziyuan-174774@users.noreply.github.com> Co-authored-by: 竹尔 <54707870+AntDesigners@users.noreply.github.com> Co-authored-by: shuwenliu0116 <33922479+shuwenliu0116@users.noreply.github.com>
2020-02-27 10:53:30 +08:00
const file = {
status: 'done',
uid: '-1',
type: 'video/mp4',
url: 'https://zos.alipayobjects.com/rmsportal/IQKRngzUuFzJzGzRJXUs.png',
};
const { container: wrapper } = render(
<Upload listType="picture-card" fileList={[file] as UploadProps['fileList']} />,
);
expect(wrapper.querySelectorAll('img').length).toBe(0);
});
// https://github.com/ant-design/ant-design/issues/25077
it('should support events', () => {
const onClick = jest.fn();
const onMouseEnter = jest.fn();
const onMouseLeave = jest.fn();
const props = { onClick, onMouseEnter, onMouseLeave };
const { container: wrapper } = render(
<Upload {...props}>
<button type="button">upload</button>
</Upload>,
);
fireEvent.click(wrapper.querySelectorAll('.ant-upload')[1]);
expect(onClick).toHaveBeenCalled();
fireEvent.mouseEnter(wrapper.querySelectorAll('.ant-upload')[1]);
expect(onMouseEnter).toHaveBeenCalled();
fireEvent.mouseLeave(wrapper.querySelectorAll('.ant-upload')[1]);
expect(onMouseLeave).toHaveBeenCalled();
});
// https://github.com/ant-design/ant-design/issues/26427
it('should sync file list with control mode', async () => {
const done = jest.fn();
let callTimes = 0;
const customRequest = jest.fn(async (options) => {
// stop here to make sure new fileList has been set and passed to Upload
// eslint-disable-next-line no-promise-executor-return
await new Promise((resolve) => setTimeout(resolve, 0));
options.onProgress({ percent: 0 });
const url = Promise.resolve('https://ant.design');
options.onProgress({ percent: 100 });
options.onSuccess({}, { ...options.file, url });
});
const Demo: React.FC = () => {
const [fileList, setFileList] = React.useState<UploadFile[]>([]);
const onChange: UploadProps['onChange'] = async (e) => {
const newFileList = Array.isArray(e) ? e : e.fileList;
setFileList(newFileList);
const file = newFileList[0];
callTimes += 1;
switch (callTimes) {
case 1:
case 2:
expect(file).toEqual(expect.objectContaining({ status: 'uploading', percent: 0 }));
break;
merge Feature into master (#29758) * feat: add onCancel and onEnd option for editable (#29615) * feature: add onCancel and onEnd option for editable * doc: editable * doc: add EN doc * optimize: code Co-authored-by: chenliang <chenliang9@xiaomi.com> * feat: add parent class for different notification types (#29634) close #29417 * refactor: Upload use origin behavior (#29737) * refactor: Fallback of events * test: Fix test case * fix: Start file update logic * fix: remove status update * test: Remove wrapTest * test: Fix test case * chore: bump rc-upload * docs: About desc * feat: tab support moreIcon (#29744) * feat: Tabs support moreIcon * docs: Tabs support moreIcon * style: lint * docs: add english document * Update components/tabs/index.zh-CN.md Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> * Update components/tabs/index.en-US.md Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> * Update components/tabs/index.zh-CN.md * Update components/tabs/index.en-US.md Co-authored-by: zty <zty.dev@outlook.com> Co-authored-by: zty <zty.dev@icloud.com> Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: jueinin <1014397160@qq.com> Co-authored-by: chenliang <chenliang9@xiaomi.com> Co-authored-by: 不吃猫的鱼 <michael2ib1989@gmail.com> Co-authored-by: 二货机器人 <smith3816@gmail.com> Co-authored-by: Tianyuan Zhang <tianyuan233.zhang@gmail.com> Co-authored-by: zty <zty.dev@outlook.com> Co-authored-by: zty <zty.dev@icloud.com> Co-authored-by: xrkffgg <xrkffgg@vip.qq.com>
2021-03-13 23:46:32 +08:00
case 3:
expect(file).toEqual(expect.objectContaining({ status: 'uploading', percent: 100 }));
break;
merge Feature into master (#29758) * feat: add onCancel and onEnd option for editable (#29615) * feature: add onCancel and onEnd option for editable * doc: editable * doc: add EN doc * optimize: code Co-authored-by: chenliang <chenliang9@xiaomi.com> * feat: add parent class for different notification types (#29634) close #29417 * refactor: Upload use origin behavior (#29737) * refactor: Fallback of events * test: Fix test case * fix: Start file update logic * fix: remove status update * test: Remove wrapTest * test: Fix test case * chore: bump rc-upload * docs: About desc * feat: tab support moreIcon (#29744) * feat: Tabs support moreIcon * docs: Tabs support moreIcon * style: lint * docs: add english document * Update components/tabs/index.zh-CN.md Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> * Update components/tabs/index.en-US.md Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> * Update components/tabs/index.zh-CN.md * Update components/tabs/index.en-US.md Co-authored-by: zty <zty.dev@outlook.com> Co-authored-by: zty <zty.dev@icloud.com> Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: jueinin <1014397160@qq.com> Co-authored-by: chenliang <chenliang9@xiaomi.com> Co-authored-by: 不吃猫的鱼 <michael2ib1989@gmail.com> Co-authored-by: 二货机器人 <smith3816@gmail.com> Co-authored-by: Tianyuan Zhang <tianyuan233.zhang@gmail.com> Co-authored-by: zty <zty.dev@outlook.com> Co-authored-by: zty <zty.dev@icloud.com> Co-authored-by: xrkffgg <xrkffgg@vip.qq.com>
2021-03-13 23:46:32 +08:00
case 4:
expect(file).toEqual(expect.objectContaining({ status: 'done', percent: 100 }));
break;
default:
// Do nothing
}
if (callTimes >= 4) {
done();
}
};
return (
<Upload customRequest={customRequest} onChange={onChange} fileList={fileList}>
<button type="button">Upload</button>
</Upload>
);
};
const { container: wrapper } = render(<Demo />);
fireEvent.change(wrapper.querySelector('input')!, {
target: { files: [{ file: 'foo.png' }] },
});
2022-10-31 10:16:51 +08:00
await waitFakeTimer();
expect(done).toHaveBeenCalled();
});
describe('maxCount', () => {
it('replace when only 1', async () => {
const onChange = jest.fn();
const fileList = [
{
uid: 'bar',
name: 'bar.png',
},
];
const props = {
action: 'http://upload.com',
fileList,
onChange,
maxCount: 1,
};
const { container: wrapper } = render(
<Upload {...props}>
<button type="button">upload</button>
</Upload>,
);
fireEvent.change(wrapper.querySelector('input')!, {
target: {
files: [new File(['foo'], 'foo.png', { type: 'image/png' })],
},
});
2022-10-31 10:16:51 +08:00
await waitFakeTimer();
expect(onChange.mock.calls[0][0].fileList).toHaveLength(1);
expect(onChange.mock.calls[0][0].fileList[0]).toEqual(
expect.objectContaining({
name: 'foo.png',
}),
);
});
it('maxCount > 1', async () => {
const onChange = jest.fn();
const fileList = [
{
uid: 'bar',
name: 'bar.png',
},
];
const props = {
action: 'http://upload.com',
fileList,
onChange,
maxCount: 2,
};
const { container: wrapper } = render(
<Upload {...props}>
<button type="button">upload</button>
</Upload>,
);
fireEvent.change(wrapper.querySelector('input')!, {
target: {
files: [
new File(['foo'], 'foo.png', {
type: 'image/png',
}),
new File(['invisible'], 'invisible.png', { type: 'image/png' }),
],
},
});
2022-10-31 10:16:51 +08:00
await waitFakeTimer();
expect(onChange.mock.calls[0][0].fileList).toHaveLength(2);
expect(onChange.mock.calls[0][0].fileList).toEqual([
expect.objectContaining({
name: 'bar.png',
}),
expect.objectContaining({
name: 'foo.png',
}),
]);
});
});
it('auto fill file uid', () => {
const fileList = [{ name: 'bamboo.png' }];
expect((fileList[0] as any).uid).toBeFalsy();
render(
<Upload fileList={fileList as UploadProps['fileList']}>
<button type="button">upload</button>
</Upload>,
);
expect((fileList[0] as any).uid).toBeTruthy();
});
it('Proxy should support deepClone', async () => {
const onChange = jest.fn();
const { container: wrapper } = render(
<Upload onChange={onChange}>
<button type="button">upload</button>
</Upload>,
);
fireEvent.change(wrapper.querySelector('input')!, {
target: {
files: [new File(['foo'], 'foo.png', { type: 'image/png' })],
},
});
2022-10-31 10:16:51 +08:00
await waitFakeTimer();
const { file } = onChange.mock.calls[0][0];
const clone = cloneDeep(file);
expect(Object.getOwnPropertyDescriptor(file, 'name')).toEqual(
expect.objectContaining({ value: 'foo.png' }),
);
['uid', 'name', 'lastModified', 'lastModifiedDate', 'size', 'type'].forEach((key) => {
expect(key in clone).toBeTruthy();
});
});
it('not break on freeze object', async () => {
const fileList = [
{
fileName: 'Test.png',
name: 'SupportIS App - potwierdzenie.png',
thumbUrl: null,
downloadUrl: 'https://localhost:5001/api/files/ff2917ce-e4b9-4542-84da-31cdbe7c273f',
status: 'done',
},
];
2022-10-31 10:16:51 +08:00
const image = cloneDeep(fileList[0]);
const frozenFileList = fileList.map(Object.freeze);
const { container: wrapper } = render(
<Upload fileList={frozenFileList as unknown as UploadProps['fileList']} />,
);
2022-09-05 22:02:23 +08:00
const rmBtn = wrapper.querySelectorAll('.ant-upload-list-item-action');
fireEvent.click(rmBtn[rmBtn.length - 1]);
// Wait for Upload async remove
2022-10-31 10:16:51 +08:00
await waitFakeTimer();
2022-10-31 10:16:51 +08:00
expect(image).toEqual(frozenFileList[0]);
});
// https://github.com/ant-design/ant-design/issues/30390
// IE11 Does not support the File constructor
it('should not break in IE if beforeUpload returns false', async () => {
const onChange = jest.fn();
const { container } = render(
<Upload beforeUpload={() => false} fileList={[]} onChange={onChange} />,
);
const fileConstructor = () => {
throw new TypeError("Object doesn't support this action");
};
const spyIE = jest.spyOn(global, 'File').mockImplementationOnce(fileConstructor);
fireEvent.change(container.querySelector('input')!, {
target: { files: [{ file: 'foo.png' }] },
});
// React 18 is async now
2022-10-31 10:16:51 +08:00
await waitFakeTimer();
expect(onChange.mock.calls[0][0].fileList).toHaveLength(1);
spyIE.mockRestore();
});
// https://github.com/ant-design/ant-design/issues/33819
it('should show the animation of the upload children leaving when the upload children becomes null', async () => {
const { container, rerender } = render(
<Upload listType="picture-card">
<button type="button">upload</button>
</Upload>,
);
rerender(<Upload listType="picture-card" />);
2022-06-24 11:50:13 +08:00
expect(container.querySelector('.ant-upload-select')).toHaveClass(
'ant-upload-animate-inline-leave-start',
);
2022-06-24 11:50:13 +08:00
expect(container.querySelector('.ant-upload-select')).toHaveStyle({
pointerEvents: 'none',
});
// Motion leave status change: start > active
2022-10-31 10:16:51 +08:00
await waitFakeTimer();
2022-09-05 22:02:23 +08:00
fireEvent.animationEnd(container.querySelector('.ant-upload-select')!);
expect(container.querySelector('.ant-upload-select')).not.toHaveClass(
'ant-upload-animate-inline-leave-start',
);
});
it('<Upload /> should pass <UploadList /> prefixCls', async () => {
const { container: wrapper } = render(<Upload />);
expect(wrapper.querySelectorAll('.ant-upload-list').length).toBeGreaterThan(0);
const { container: wrapper2 } = render(<Upload prefixCls="custom-upload" />);
expect(wrapper2.querySelectorAll('.custom-upload-list').length).toBeGreaterThan(0);
});
// https://github.com/ant-design/ant-design/issues/36869
it('Prevent auto batch', async () => {
const mockFile1 = new File(['bamboo'], 'bamboo.png', { type: 'image/png' });
const mockFile2 = new File(['light'], 'light.png', { type: 'image/png' });
let info1: UploadRequestOption;
let info2: UploadRequestOption;
const onChange = jest.fn();
const { container } = render(
<Upload
customRequest={(info) => {
if (info.file === mockFile1) {
info1 = info;
} else {
info2 = info;
}
}}
onChange={onChange}
/>,
);
fireEvent.change(container.querySelector('input')!, {
target: { files: [mockFile1, mockFile2] },
});
// React 18 is async now
2022-10-31 10:16:51 +08:00
await waitFakeTimer();
onChange.mockReset();
// Processing
act(() => {
(info1?.onProgress as any)?.({ percent: 10 }, mockFile1);
(info2?.onProgress as any)?.({ percent: 20 }, mockFile2);
});
expect(onChange).toHaveBeenCalledWith(
expect.objectContaining({
fileList: [
expect.objectContaining({ percent: 10 }),
expect.objectContaining({ percent: 20 }),
],
}),
);
});
it('prevent auto batch in control mode', async () => {
const mockFile1 = new File(['bamboo'], 'bamboo.png', { type: 'image/png' });
const mockFile2 = new File(['light'], 'light.png', { type: 'image/png' });
const customRequest = jest.fn(async (options) => {
// stop here to make sure new fileList has been set and passed to Upload
// eslint-disable-next-line no-promise-executor-return
await new Promise((resolve) => setTimeout(resolve, 0));
options.onProgress({ percent: 0 });
const url = Promise.resolve<string>('https://ant.design');
options.onProgress({ percent: 100 });
options.onSuccess({}, { ...options.file, url });
});
let fileListOut: UploadProps['fileList'] = [];
const Demo: React.FC = () => {
const [fileList, setFileList] = React.useState<UploadFile[]>([]);
const onChange: UploadProps['onChange'] = async (e) => {
const newFileList = Array.isArray(e) ? e : e.fileList;
setFileList(newFileList);
fileListOut = newFileList;
};
return (
<Upload customRequest={customRequest} onChange={onChange} fileList={fileList}>
<button type="button">Upload</button>
</Upload>
);
};
const { container } = render(<Demo />);
fireEvent.change(container.querySelector<HTMLInputElement>('input')!, {
target: { files: [mockFile1, mockFile2] },
});
// React 18 is async now
await waitFakeTimer();
fileListOut.forEach((file) => {
expect(file.status).toBe('done');
});
});
2017-02-22 16:06:08 +08:00
});