From 2ae579bc02008674d7e0ce8e91ee6445e313d7d5 Mon Sep 17 00:00:00 2001 From: cballevre Date: Tue, 11 Jul 2023 14:26:49 +0200 Subject: [PATCH] test(FilesViewer): Mock cozy-ui Viewer to prevent console.log Without the await, the getEncryptionKeyFromDirId function is never called because the file is not considered to be encrypted. I followed the previous tests to solve the problem This commit also removes the flaky test reported in issue #2910 --- src/drive/web/modules/viewer/FilesViewer.jsx | 11 ++++------- src/drive/web/modules/viewer/FilesViewer.spec.jsx | 15 ++++++++++----- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/drive/web/modules/viewer/FilesViewer.jsx b/src/drive/web/modules/viewer/FilesViewer.jsx index e3d78ecfcc..4f1e706f70 100644 --- a/src/drive/web/modules/viewer/FilesViewer.jsx +++ b/src/drive/web/modules/viewer/FilesViewer.jsx @@ -82,12 +82,9 @@ const FilesViewer = ({ filesQuery, files, onClose, onChange }) => { [onChange] ) - const getCurrentIndex = useCallback( - () => files.findIndex(f => f.id === fileId), - [files, fileId] - ) - - const currentIndex = useMemo(() => getCurrentIndex(), [getCurrentIndex]) + const currentIndex = useMemo(() => { + return files.findIndex(f => f.id === fileId) + }, [files, fileId]) const hasCurrentIndex = useMemo(() => currentIndex != -1, [currentIndex]) const viewerFiles = useMemo( () => (hasCurrentIndex ? files : [currentFile]), @@ -110,7 +107,7 @@ const FilesViewer = ({ filesQuery, files, onClose, onChange }) => { // the containing folder (it comes from a fetchMore...) ; we load the file attributes // directly as a contingency measure const fetchFileIfNecessary = async () => { - if (getCurrentIndex() !== -1) return + if (hasCurrentIndex) return if (currentFile && isMounted) { setCurrentFile(null) } diff --git a/src/drive/web/modules/viewer/FilesViewer.spec.jsx b/src/drive/web/modules/viewer/FilesViewer.spec.jsx index ca9685475e..866aab93b5 100644 --- a/src/drive/web/modules/viewer/FilesViewer.spec.jsx +++ b/src/drive/web/modules/viewer/FilesViewer.spec.jsx @@ -22,6 +22,8 @@ jest.mock('drive/lib/encryption', () => ({ jest.mock('drive/hooks') +jest.mock('cozy-ui/transpiled/react/Viewer', () => () =>
Viewer
) + const sleep = duration => new Promise(resolve => setTimeout(resolve, duration)) describe('FilesViewer', () => { @@ -107,9 +109,7 @@ describe('FilesViewer', () => { }) }) - // https://github.com/cozy/cozy-drive/issues/2910 - // TODO: Fix this flaky test - it.skip('should fetch more files if necessary', async () => { + it('should fetch more files if necessary', async () => { const client = new CozyClient({}) client.query = jest.fn().mockResolvedValue({ data: generateFile({ i: '51' }) @@ -137,11 +137,12 @@ describe('FilesViewer', () => { expect(fetchMore).toHaveBeenCalledTimes(1) }) - it('should get decyrption key when file is encrypted', async () => { + it('should get decryption key when file is encrypted', async () => { const client = new CozyClient({}) client.query = jest.fn().mockResolvedValue({ data: generateFile({ i: '0', encrypted: true }) }) + await act(async () => { const { root } = await setup({ client, @@ -150,9 +151,13 @@ describe('FilesViewer', () => { fileId: 'file-foobar0', isEncrypted: true }) + + // Let promise resolve + await sleep(0) + root.update() - expect(root.find(Viewer).length).toBe(1) + expect(root.find(Viewer).length).toBe(1) expect(getEncryptionKeyFromDirId).toHaveBeenCalled() }) })