Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8eafa5c

Browse files
committedJul 11, 2023
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
1 parent 5a33952 commit 8eafa5c

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed
 

‎src/drive/web/modules/viewer/FilesViewer.jsx

+4-7
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,9 @@ const FilesViewer = ({ filesQuery, files, onClose, onChange }) => {
8282
[onChange]
8383
)
8484

85-
const getCurrentIndex = useCallback(
86-
() => files.findIndex(f => f.id === fileId),
87-
[files, fileId]
88-
)
89-
90-
const currentIndex = useMemo(() => getCurrentIndex(), [getCurrentIndex])
85+
const currentIndex = useMemo(() => {
86+
return files.findIndex(f => f.id === fileId)
87+
}, [files, fileId])
9188
const hasCurrentIndex = useMemo(() => currentIndex != -1, [currentIndex])
9289
const viewerFiles = useMemo(
9390
() => (hasCurrentIndex ? files : [currentFile]),
@@ -110,7 +107,7 @@ const FilesViewer = ({ filesQuery, files, onClose, onChange }) => {
110107
// the containing folder (it comes from a fetchMore...) ; we load the file attributes
111108
// directly as a contingency measure
112109
const fetchFileIfNecessary = async () => {
113-
if (getCurrentIndex() !== -1) return
110+
if (hasCurrentIndex) return
114111
if (currentFile && isMounted) {
115112
setCurrentFile(null)
116113
}

‎src/drive/web/modules/viewer/FilesViewer.spec.jsx

+10-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ jest.mock('drive/lib/encryption', () => ({
2222

2323
jest.mock('drive/hooks')
2424

25+
jest.mock('cozy-ui/transpiled/react/Viewer', () => () => <div>Viewer</div>)
26+
2527
const sleep = duration => new Promise(resolve => setTimeout(resolve, duration))
2628

2729
describe('FilesViewer', () => {
@@ -107,9 +109,7 @@ describe('FilesViewer', () => {
107109
})
108110
})
109111

110-
// https://github.com/cozy/cozy-drive/issues/2910
111-
// TODO: Fix this flaky test
112-
it.skip('should fetch more files if necessary', async () => {
112+
it('should fetch more files if necessary', async () => {
113113
const client = new CozyClient({})
114114
client.query = jest.fn().mockResolvedValue({
115115
data: generateFile({ i: '51' })
@@ -137,11 +137,12 @@ describe('FilesViewer', () => {
137137
expect(fetchMore).toHaveBeenCalledTimes(1)
138138
})
139139

140-
it('should get decyrption key when file is encrypted', async () => {
140+
it('should get decryption key when file is encrypted', async () => {
141141
const client = new CozyClient({})
142142
client.query = jest.fn().mockResolvedValue({
143143
data: generateFile({ i: '0', encrypted: true })
144144
})
145+
145146
await act(async () => {
146147
const { root } = await setup({
147148
client,
@@ -150,9 +151,13 @@ describe('FilesViewer', () => {
150151
fileId: 'file-foobar0',
151152
isEncrypted: true
152153
})
154+
155+
// Let promise resolve
156+
await sleep(0)
157+
153158
root.update()
154-
expect(root.find(Viewer).length).toBe(1)
155159

160+
expect(root.find(Viewer).length).toBe(1)
156161
expect(getEncryptionKeyFromDirId).toHaveBeenCalled()
157162
})
158163
})

0 commit comments

Comments
 (0)
Please sign in to comment.