|
1 | | -import { fireEvent, render } from '@testing-library/react'; |
2 | 1 | import { resetWarned } from '@rc-component/util/lib/warning'; |
| 2 | +import { fireEvent, render } from '@testing-library/react'; |
3 | 3 | import React from 'react'; |
4 | 4 | import sinon from 'sinon'; |
5 | 5 | import { format } from 'util'; |
@@ -1040,6 +1040,71 @@ describe('uploader', () => { |
1040 | 1040 | ); |
1041 | 1041 | }); |
1042 | 1042 |
|
| 1043 | + describe('AcceptConfig', () => { |
| 1044 | + let uploader: ReturnType<typeof render>; |
| 1045 | + const handlers: UploadProps = {}; |
| 1046 | + |
| 1047 | + const props: UploadProps = { |
| 1048 | + action: '/test', |
| 1049 | + data: { a: 1, b: 2 }, |
| 1050 | + directory: true, // Enable format filtering |
| 1051 | + onStart(file) { |
| 1052 | + if (handlers.onStart) { |
| 1053 | + handlers.onStart(file); |
| 1054 | + } |
| 1055 | + }, |
| 1056 | + }; |
| 1057 | + |
| 1058 | + function testAcceptConfig(desc: string, accept: any, files: object[], expectCallTimes: number) { |
| 1059 | + it(desc, done => { |
| 1060 | + uploader = render(<Upload {...props} accept={accept} />); |
| 1061 | + const input = uploader.container.querySelector('input')!; |
| 1062 | + fireEvent.change(input, { target: { files } }); |
| 1063 | + const mockStart = jest.fn(); |
| 1064 | + handlers.onStart = mockStart; |
| 1065 | + |
| 1066 | + setTimeout(() => { |
| 1067 | + expect(mockStart.mock.calls.length).toBe(expectCallTimes); |
| 1068 | + done(); |
| 1069 | + }, 100); |
| 1070 | + }); |
| 1071 | + } |
| 1072 | + |
| 1073 | + testAcceptConfig( |
| 1074 | + 'should work with format only', |
| 1075 | + { format: '.png' }, |
| 1076 | + [{ name: 'test.png' }, { name: 'test.jpg' }], |
| 1077 | + 1, |
| 1078 | + ); |
| 1079 | + |
| 1080 | + testAcceptConfig( |
| 1081 | + 'should work with filter: native', |
| 1082 | + { format: '.png', filter: 'native' }, |
| 1083 | + [{ name: 'test.png' }, { name: 'test.jpg' }], |
| 1084 | + 2, // native filter bypasses format check |
| 1085 | + ); |
| 1086 | + |
| 1087 | + testAcceptConfig( |
| 1088 | + 'should work with custom filter function', |
| 1089 | + { |
| 1090 | + format: '.png', |
| 1091 | + filter: (file: any) => file.name.includes('custom'), |
| 1092 | + }, |
| 1093 | + [{ name: 'custom.jpg' }, { name: 'test.png' }], |
| 1094 | + 1, // only custom.jpg passes custom filter |
| 1095 | + ); |
| 1096 | + |
| 1097 | + testAcceptConfig( |
| 1098 | + 'should work with MIME type format', |
| 1099 | + { format: 'image/*' }, |
| 1100 | + [ |
| 1101 | + { name: 'test.png', type: 'image/png' }, |
| 1102 | + { name: 'doc.txt', type: 'text/plain' }, |
| 1103 | + ], |
| 1104 | + 1, // only image file passes |
| 1105 | + ); |
| 1106 | + }); |
| 1107 | + |
1043 | 1108 | describe('transform file before request', () => { |
1044 | 1109 | let uploader: ReturnType<typeof render>; |
1045 | 1110 | beforeEach(() => { |
|
0 commit comments