Skip to content

Commit

Permalink
test(FilesViewer): Mock cozy-ui Viewer to prevent console.log
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cballevre committed Jul 11, 2023
1 parent 9d86b08 commit 2ae579b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
11 changes: 4 additions & 7 deletions src/drive/web/modules/viewer/FilesViewer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand All @@ -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)
}
Expand Down
15 changes: 10 additions & 5 deletions src/drive/web/modules/viewer/FilesViewer.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ jest.mock('drive/lib/encryption', () => ({

jest.mock('drive/hooks')

jest.mock('cozy-ui/transpiled/react/Viewer', () => () => <div>Viewer</div>)

const sleep = duration => new Promise(resolve => setTimeout(resolve, duration))

describe('FilesViewer', () => {
Expand Down Expand Up @@ -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' })
Expand Down Expand Up @@ -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,
Expand All @@ -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()
})
})
Expand Down

0 comments on commit 2ae579b

Please sign in to comment.