|
| 1 | +import React from 'react' |
| 2 | +import { render } from '@testing-library/react' |
| 3 | +import { createMockClient } from 'cozy-client' |
| 4 | +import { DOCTYPE_FILES_ENCRYPTION } from 'drive/lib/doctypes' |
| 5 | +import AppLike from 'test/components/AppLike' |
| 6 | +import { generateFile } from 'test/generate' |
| 7 | +import FileList, { isInvalidMoveTarget } from 'drive/web/modules/move/FileList' |
| 8 | + |
| 9 | +jest.mock('cozy-keys-lib', () => ({ |
| 10 | + useVaultUnlockContext: jest |
| 11 | + .fn() |
| 12 | + .mockReturnValue({ showUnlockForm: jest.fn() }) |
| 13 | +})) |
| 14 | +const client = createMockClient({}) |
| 15 | + |
| 16 | +const setup = ({ files, entries, navigateTo }) => { |
| 17 | + const root = render( |
| 18 | + <AppLike client={client}> |
| 19 | + <FileList files={files} entries={entries} navigateTo={navigateTo} /> |
| 20 | + </AppLike> |
| 21 | + ) |
| 22 | + |
| 23 | + return { root } |
| 24 | +} |
| 25 | + |
| 26 | +describe('FileList', () => { |
| 27 | + it('should display files', () => { |
| 28 | + const entries = [generateFile({ i: '1', type: 'file' })] |
| 29 | + const files = [ |
| 30 | + generateFile({ i: '1', type: 'directory', prefix: '' }), |
| 31 | + generateFile({ i: '2', type: 'file', prefix: '' }) |
| 32 | + ] |
| 33 | + |
| 34 | + const { root } = setup({ files, entries, navigateTo: () => null }) |
| 35 | + const { queryAllByText } = root |
| 36 | + |
| 37 | + expect(queryAllByText('directory-1')).toBeTruthy() |
| 38 | + expect(queryAllByText('file-2')).toBeTruthy() |
| 39 | + }) |
| 40 | +}) |
| 41 | + |
| 42 | +describe('isInvalidMoveTarget', () => { |
| 43 | + it('should return true when target is a file', () => { |
| 44 | + const target = { _id: '1', type: 'file' } |
| 45 | + const entries = [{ _id: 'dir1', type: 'directory' }] |
| 46 | + expect(isInvalidMoveTarget(entries, target)).toBe(true) |
| 47 | + }) |
| 48 | + |
| 49 | + it('should return true when subject is the target', () => { |
| 50 | + const target = { _id: '1', type: 'directory' } |
| 51 | + const entries = [{ _id: '1', type: 'directory' }] |
| 52 | + expect(isInvalidMoveTarget(entries, target)).toBe(true) |
| 53 | + }) |
| 54 | + |
| 55 | + it('should return true when target is an encrypted directory and entries has non-encrypted folder', () => { |
| 56 | + const target = { |
| 57 | + _id: '1', |
| 58 | + type: 'directory', |
| 59 | + referenced_by: [{ type: DOCTYPE_FILES_ENCRYPTION, id: 'encrypted-key-1' }] |
| 60 | + } |
| 61 | + const entries = [{ _id: 'dir1', type: 'directory' }] |
| 62 | + expect(isInvalidMoveTarget(entries, target)).toBe(true) |
| 63 | + }) |
| 64 | + |
| 65 | + it('shold return true when target is an encrypted dir and entries include both encrytped and non-encrypted dir', () => { |
| 66 | + const target = { |
| 67 | + _id: '1', |
| 68 | + type: 'directory', |
| 69 | + referenced_by: [{ type: DOCTYPE_FILES_ENCRYPTION, id: 'encrypted-key-1' }] |
| 70 | + } |
| 71 | + const entries = [ |
| 72 | + { |
| 73 | + _id: 'dir1', |
| 74 | + type: 'directory', |
| 75 | + referenced_by: [ |
| 76 | + { type: DOCTYPE_FILES_ENCRYPTION, id: 'encrypted-key-1' } |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + _id: 'dir2', |
| 81 | + type: 'directory' |
| 82 | + } |
| 83 | + ] |
| 84 | + expect(isInvalidMoveTarget(entries, target)).toBe(true) |
| 85 | + }) |
| 86 | + |
| 87 | + it('should return false when both target and entries are encrypted', () => { |
| 88 | + const target = { |
| 89 | + _id: '1', |
| 90 | + type: 'directory', |
| 91 | + referenced_by: [{ type: DOCTYPE_FILES_ENCRYPTION, id: 'encrypted-key-1' }] |
| 92 | + } |
| 93 | + const entries = [ |
| 94 | + { |
| 95 | + _id: 'dir1', |
| 96 | + type: 'directory', |
| 97 | + referenced_by: [ |
| 98 | + { type: DOCTYPE_FILES_ENCRYPTION, id: 'encrypted-key-1' } |
| 99 | + ] |
| 100 | + } |
| 101 | + ] |
| 102 | + expect(isInvalidMoveTarget(entries, target)).toBe(false) |
| 103 | + }) |
| 104 | + |
| 105 | + it('should return false when both target and entries are regular folders', () => { |
| 106 | + const target = { _id: '1', type: 'directory' } |
| 107 | + const entries = [{ _id: 'dir1', type: 'directory' }] |
| 108 | + expect(isInvalidMoveTarget(entries, target)).toBe(false) |
| 109 | + }) |
| 110 | + |
| 111 | + it('should return false when subject is a mix of regular file and folder and target a regular folder', () => { |
| 112 | + const target = { _id: '1', type: 'directory' } |
| 113 | + const entries = [ |
| 114 | + { _id: 'dir1', type: 'file' }, |
| 115 | + { _id: 'file1', type: 'file' } |
| 116 | + ] |
| 117 | + expect(isInvalidMoveTarget(entries, target)).toBe(false) |
| 118 | + }) |
| 119 | +}) |
0 commit comments