-
Notifications
You must be signed in to change notification settings - Fork 367
/
renameFolder.spec.ts
169 lines (143 loc) · 4.88 KB
/
renameFolder.spec.ts
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { createDbStoreCreator } from '../store'
import { MemoryLiteStorage } from 'ltstrg'
import { renderHook, act } from '@testing-library/react-hooks'
import { NoteStorage, NoteDoc } from '../types'
import { RouterProvider } from '../../router'
import { getFolderId } from '../utils'
function prepareDbStore() {
const memoryStorage = new MemoryLiteStorage()
const { result } = renderHook(
() => createDbStoreCreator(memoryStorage, 'memory')(),
{
wrapper: RouterProvider
}
)
return {
result,
memoryStorage
}
}
describe('Dbstore', () => {
describe('#renameFolder', () => {
it('removes a folder previous path', async () => {
// Given
const { result } = prepareDbStore()
let storage: NoteStorage
await act(async () => {
await result.current.initialize()
storage = await result.current.createStorage('test')
await result.current.createFolder(storage.id, '/test')
// When
await result.current.renameFolder(storage.id, '/test', '/testok')
})
// Then
expect(
result.current.storageMap[storage!.id]!.folderMap['/test']
).toBeUndefined()
const folderDoc = await storage!.db.getFolder('/test')
expect(folderDoc).toBeNull()
})
it('creates a folder with the new path', async () => {
// Given
const { result } = prepareDbStore()
let storage: NoteStorage
await act(async () => {
await result.current.initialize()
storage = await result.current.createStorage('test')
await result.current.createFolder(storage.id, '/test')
// When
await result.current.renameFolder(storage.id, '/test', '/testok')
})
// Then
const folderDoc = await storage!.db.getFolder('/testok')
expect(folderDoc).toMatchObject({
_id: getFolderId('/testok')
})
expect(
result.current.storageMap[storage!.id]!.folderMap['/testok']
).toBeDefined()
})
it('removes subfolders with the previous path', async () => {
// Given
const { result } = prepareDbStore()
let storage: NoteStorage
await act(async () => {
await result.current.initialize()
storage = await result.current.createStorage('test')
await result.current.createFolder(storage.id, '/test/subtest')
// When
await result.current.renameFolder(storage.id, '/test', '/testok')
})
// Then
expect(
result.current.storageMap[storage!.id]!.folderMap['/test/subtest']
).toBeUndefined()
const folderDoc = await storage!.db.getFolder('/test/subtest')
expect(folderDoc).toBeNull()
})
it('creates subfolders with the new path', async () => {
// Given
const { result } = prepareDbStore()
let storage: NoteStorage
await act(async () => {
await result.current.initialize()
storage = await result.current.createStorage('test')
await result.current.createFolder(storage.id, '/test/subtest')
// When
await result.current.renameFolder(storage.id, '/test', '/testok')
})
// Then
const folderDoc = await storage!.db.getFolder('/testok/subtest')
expect(folderDoc).toMatchObject({
_id: getFolderId('/testok/subtest')
})
expect(
result.current.storageMap[storage!.id]!.folderMap['/testok/subtest']
).toBeDefined()
})
it("changes the notes' folderpathname", async () => {
const { result } = prepareDbStore()
let storage: NoteStorage
let note: NoteDoc
await act(async () => {
await result.current.initialize()
storage = await result.current.createStorage('test')
note = (await result.current.createNote(storage.id, {
folderPathname: '/test'
})) as NoteDoc
// When
await result.current.renameFolder(storage.id, '/test', '/testok')
})
// Then
expect(
result.current.storageMap[storage!.id]!.noteMap[note!._id]
).toMatchObject({
folderPathname: '/testok'
})
})
it("changes the subfolders' notes' folderpathnames", async () => {
const { result } = prepareDbStore()
let storage: NoteStorage
let note: NoteDoc
await act(async () => {
await result.current.initialize()
storage = await result.current.createStorage('test')
note = (await result.current.createNote(storage.id, {
folderPathname: '/test/subfolder'
})) as NoteDoc
// When
await result.current.renameFolder(storage.id, '/test', '/testok')
})
// Then
expect(
result.current.storageMap[storage!.id]!.noteMap[note!._id]
).toMatchObject({
folderPathname: '/testok/subfolder'
})
})
// take notes and move them
// take sub folders and move them
// take sub folders notes and move them
// erase ALL the previous folders
})
})