-
Notifications
You must be signed in to change notification settings - Fork 12
/
folder.spec.js
112 lines (96 loc) · 3.13 KB
/
folder.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { MAGIC_FOLDERS, ensureMagicFolder, getReferencedFolder } from './folder'
describe('Folder model', () => {
const mockClient = {
collection: () => mockClient,
query: jest.fn(),
findReferencedBy: jest.fn(),
ensureDirectoryExists: jest.fn(),
addReferencesTo: jest.fn(),
get: jest.fn()
}
afterEach(() => {
jest.restoreAllMocks()
})
it('should expose magic folders', () => {
expect(MAGIC_FOLDERS).toBeDefined()
expect(MAGIC_FOLDERS.ADMINISTRATIVE).toBeDefined()
expect(MAGIC_FOLDERS.PHOTOS).toBeDefined()
expect(MAGIC_FOLDERS.PHOTOS_UPLOAD).toBeDefined()
expect(MAGIC_FOLDERS.PHOTOS_BACKUP).toBeDefined()
expect(MAGIC_FOLDERS.NOTES).toBeDefined()
expect(MAGIC_FOLDERS.HOME).toBeDefined()
expect(MAGIC_FOLDERS.PAPERS).toBeDefined()
expect(MAGIC_FOLDERS.COACH_CO2).toBeDefined()
})
describe('ensureMagicFolder', () => {
it('should return first existing magic folder', async () => {
const existingMagicFolders = [
{
path: '/Administrative',
created_at: '2020-03-02T14:57:07.661588+01:00'
},
{
path: '/Administrative2',
created_at: '2019-03-02T14:57:07.661588+01:00'
}
]
mockClient.query.mockResolvedValueOnce({
included: existingMagicFolders
})
const result = await ensureMagicFolder(
mockClient,
MAGIC_FOLDERS.ADMINISTRATIVE
)
expect(result).toEqual(existingMagicFolders[0])
})
it('should throw if magic folder id is invalid', async () => {
mockClient.findReferencedBy.mockResolvedValue({
included: []
})
await expect(
ensureMagicFolder(mockClient, 'io.cozy.apps/unexpected/magic/folder')
).rejects.toThrow()
})
it('should throw if path is missing', async () => {
await expect(
ensureMagicFolder(mockClient, MAGIC_FOLDERS.ADMINISTRATIVE)
).rejects.toThrow()
})
it('should create magic folder', async () => {
mockClient.query.mockResolvedValueOnce({
included: []
})
mockClient.ensureDirectoryExists.mockResolvedValue('dir-id')
mockClient.get.mockResolvedValue({ data: {} })
await ensureMagicFolder(
mockClient,
MAGIC_FOLDERS.ADMINISTRATIVE,
'/Administrative'
)
expect(mockClient.ensureDirectoryExists).toHaveBeenCalledWith(
'/Administrative'
)
expect(mockClient.addReferencesTo).toHaveBeenCalledWith(
{ _id: 'io.cozy.apps/administrative', _type: 'io.cozy.apps' },
[{ _id: 'dir-id' }]
)
})
})
describe('getReferencedFolder', () => {
it('should return the most recently created folder', async () => {
const oldFolder = {
path: '/Old/folder',
created_at: '2019-03-02T14:57:07.661588+01:00'
}
const recentFolder = {
path: '/Recent/folder',
created_at: '2020-03-02T14:57:07.661588+01:00'
}
mockClient.query.mockResolvedValueOnce({
included: [oldFolder, recentFolder]
})
const result = await getReferencedFolder(mockClient, 'ref')
expect(result).toBe(recentFolder)
})
})
})