Skip to content

Commit 914de0e

Browse files
committed
test(files): Add tests for path handling
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent c7d22f9 commit 914de0e

2 files changed

Lines changed: 162 additions & 0 deletions

File tree

apps/files/src/store/paths.spec.ts

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import { setActivePinia, createPinia } from 'pinia'
7+
import { usePathsStore } from './paths.ts'
8+
import { emit } from '@nextcloud/event-bus'
9+
import { File, Folder } from '@nextcloud/files'
10+
import { useFilesStore } from './files.ts'
11+
12+
describe('Path store', () => {
13+
14+
let store: ReturnType<typeof usePathsStore>
15+
let files: ReturnType<typeof useFilesStore>
16+
let root: Folder & { _children?: string[] }
17+
18+
beforeEach(() => {
19+
setActivePinia(createPinia())
20+
21+
root = new Folder({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/', id: 1 })
22+
files = useFilesStore()
23+
files.setRoot({ service: 'files', root })
24+
25+
store = usePathsStore()
26+
})
27+
28+
test('Folder is created', () => {
29+
// no defined paths
30+
expect(store.paths).toEqual({})
31+
32+
// create the folder
33+
const node = new Folder({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/folder', id: 2 })
34+
emit('files:node:created', node)
35+
36+
// see that the path is added
37+
expect(store.paths).toEqual({ files: { [node.path]: node.source } })
38+
39+
// see that the node is added
40+
expect(root._children).toEqual([node.source])
41+
})
42+
43+
test('File is created', () => {
44+
// no defined paths
45+
expect(store.paths).toEqual({})
46+
47+
// create the file
48+
const node = new File({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/file.txt', id: 2, mime: 'text/plain' })
49+
emit('files:node:created', node)
50+
51+
// see that there are still no paths
52+
expect(store.paths).toEqual({})
53+
54+
// see that the node is added
55+
expect(root._children).toEqual([node.source])
56+
})
57+
58+
test('Existing file is created', () => {
59+
// no defined paths
60+
expect(store.paths).toEqual({})
61+
62+
// create the file
63+
const node1 = new File({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/file.txt', id: 2, mime: 'text/plain' })
64+
emit('files:node:created', node1)
65+
66+
// see that there are still no paths
67+
expect(store.paths).toEqual({})
68+
69+
// see that the node is added
70+
expect(root._children).toEqual([node1.source])
71+
72+
// create the same named file again
73+
const node2 = new File({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/file.txt', id: 2, mime: 'text/plain' })
74+
emit('files:node:created', node2)
75+
76+
// see that there are still no paths and the children are not duplicated
77+
expect(store.paths).toEqual({})
78+
expect(root._children).toEqual([node1.source])
79+
80+
})
81+
82+
test('Existing folder is created', () => {
83+
// no defined paths
84+
expect(store.paths).toEqual({})
85+
86+
// create the file
87+
const node1 = new Folder({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/folder', id: 2 })
88+
emit('files:node:created', node1)
89+
90+
// see the path is added
91+
expect(store.paths).toEqual({ files: { [node1.path]: node1.source } })
92+
93+
// see that the node is added
94+
expect(root._children).toEqual([node1.source])
95+
96+
// create the same named file again
97+
const node2 = new Folder({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/folder', id: 2 })
98+
emit('files:node:created', node2)
99+
100+
// see that there is still only one paths and the children are not duplicated
101+
expect(store.paths).toEqual({ files: { [node1.path]: node1.source } })
102+
expect(root._children).toEqual([node1.source])
103+
})
104+
105+
test('Folder is deleted', () => {
106+
const node = new Folder({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/folder', id: 2 })
107+
emit('files:node:created', node)
108+
// see that the path is added and the children are set-up
109+
expect(store.paths).toEqual({ files: { [node.path]: node.source } })
110+
expect(root._children).toEqual([node.source])
111+
112+
emit('files:node:deleted', node)
113+
// See the path is removed
114+
expect(store.paths).toEqual({ files: {} })
115+
// See the child is removed
116+
expect(root._children).toEqual([])
117+
})
118+
119+
test('File is deleted', () => {
120+
const node = new File({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/file.txt', id: 2, mime: 'text/plain' })
121+
emit('files:node:created', node)
122+
// see that the children are set-up
123+
expect(root._children).toEqual([node.source])
124+
125+
emit('files:node:deleted', node)
126+
// See the child is removed
127+
expect(root._children).toEqual([])
128+
})
129+
})
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import { createFolder, getRowForFile, triggerActionForFile } from './FilesUtils.ts'
7+
8+
before(() => {
9+
cy.createRandomUser()
10+
.then((user) => {
11+
cy.mkdir(user, '/only once')
12+
cy.login(user)
13+
cy.visit('/apps/files')
14+
})
15+
})
16+
17+
/**
18+
* Regression test for https://github.com/nextcloud/server/issues/47904
19+
*/
20+
it('Ensure nodes are not duplicated in the file list', () => {
21+
// See the folder
22+
getRowForFile('only once').should('be.visible')
23+
// Delete the folder
24+
cy.intercept('DELETE', '**/remote.php/dav/**').as('deleteFolder')
25+
triggerActionForFile('only once', 'delete')
26+
cy.wait('@deleteFolder')
27+
getRowForFile('only once').should('not.exist')
28+
// Create the folder again
29+
createFolder('only once')
30+
// See folder exists only once
31+
getRowForFile('only once')
32+
.should('have.length', 1)
33+
})

0 commit comments

Comments
 (0)