Skip to content

Commit

Permalink
test: add test to cover the fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Nolesh committed Jul 7, 2023
1 parent 4f46abe commit 2a25067
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/DefaultFileItemRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ const fileDataKeys: (keyof ILocalFileData)[] = [
'fileName',
'fileSize',
'fileType',
'previewData',
'readOnly',
'state',
];
Expand Down Expand Up @@ -341,6 +340,7 @@ const DefaultFileItemRenderer = ({
[
overrides?.thumbnailFieldStyles,
overrides?.thumbnailFieldComponent,
JSON.stringify(fileData.previewData),
root,
type,
...commonDeps,
Expand Down
65 changes: 65 additions & 0 deletions tests/FileManager.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2104,6 +2104,71 @@ describe('FileManager uploading', () => {

spy.mockRestore();
});

test('should update "previewData.src" in the "localFileData" by "generatePreview" function for uploaded files after upload process is completed', async () => {
const restoreObserver = setObserverToNull();

const { setCustomTimer, advanceTimersToNextTimer: finishGenImageThumbnail } =
useCustomTimer();

const spy = jest.spyOn(thumbnail, 'generateImageThumbnail');
spy.mockImplementation(
() => new Promise((res) => setCustomTimer(() => res('image-src'), 5))
);

const { advanceTimersToNextTimer, mockRestore } = mockUploadFunc();

let src = 'must be null';
const {
getByRole,
queryAllByRole,
findAllByRole,
findByText,
getByTestId,
queryByRole,
queryByTestId,
} = render(
<Manager
getUploadParams={(localFileData: ILocalFileData) => ({
URL: `/api/singleFileUpload`,
processResponse: (response) => {
src = localFileData.previewData.src;
return localFileData;
},
processError: getErrorMessage,
})}
autoUpload
/>
);

await findByText('dragNdrop');

const input = getByRole('fileinput', { hidden: true }) as HTMLInputElement;
const file = mockFile('img.png', 10 * 1024, 'image/png');
fireEvent.change(input, { target: { files: [file] } });

await waitFor(() => expect(queryAllByRole('fileitem').length).toEqual(1));

expect(getByTestId('loading-icon')).toBeInTheDocument();
expect(getByRole('progressbar')).toBeInTheDocument();

advanceTimersToNextTimer();
await waitFor(() => expect(queryByRole('progressbar')).not.toBeInTheDocument());

// expect(src).toEqual('image-src');
expect(src).toBeNull();

finishGenImageThumbnail();
await waitFor(() => expect(queryByRole('imagelazyloader')).toBeInTheDocument());

expect(
within(getByRole('imagelazyloader')).getByRole('img', { hidden: true })
).toHaveAttribute('src', 'image-src');

restoreObserver();
spy.mockRestore();
mockRestore();
});
});

describe('FileManager uploading (one request upload mode)', () => {
Expand Down

0 comments on commit 2a25067

Please sign in to comment.