aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/src/store/paths.spec.ts
blob: b4c3f2fa686546e76dbc25313a29c87d3a7ca56d (plain)
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
/**
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

import { describe, beforeEach, test, expect } from 'vitest'
import { setActivePinia, createPinia } from 'pinia'
import { usePathsStore } from './paths.ts'
import { emit } from '@nextcloud/event-bus'
import { File, Folder } from '@nextcloud/files'
import { useFilesStore } from './files.ts'

describe('Path store', () => {

	let store: ReturnType<typeof usePathsStore>
	let files: ReturnType<typeof useFilesStore>
	let root: Folder & { _children?: string[] }

	beforeEach(() => {
		setActivePinia(createPinia())

		root = new Folder({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/', id: 1 })
		files = useFilesStore()
		files.setRoot({ service: 'files', root })

		store = usePathsStore()
	})

	test('Folder is created', () => {
		// no defined paths
		expect(store.paths).toEqual({})

		// create the folder
		const node = new Folder({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/folder', id: 2 })
		emit('files:node:created', node)

		// see that the path is added
		expect(store.paths).toEqual({ files: { [node.path]: node.source } })

		// see that the node is added
		expect(root._children).toEqual([node.source])
	})

	test('File is created', () => {
		// no defined paths
		expect(store.paths).toEqual({})

		// create the file
		const node = new File({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/file.txt', id: 2, mime: 'text/plain' })
		emit('files:node:created', node)

		// see that there are still no paths
		expect(store.paths).toEqual({})

		// see that the node is added
		expect(root._children).toEqual([node.source])
	})

	test('Existing file is created', () => {
		// no defined paths
		expect(store.paths).toEqual({})

		// create the file
		const node1 = new File({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/file.txt', id: 2, mime: 'text/plain' })
		emit('files:node:created', node1)

		// see that there are still no paths
		expect(store.paths).toEqual({})

		// see that the node is added
		expect(root._children).toEqual([node1.source])

		// create the same named file again
		const node2 = new File({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/file.txt', id: 2, mime: 'text/plain' })
		emit('files:node:created', node2)

		// see that there are still no paths and the children are not duplicated
		expect(store.paths).toEqual({})
		expect(root._children).toEqual([node1.source])

	})

	test('Existing folder is created', () => {
		// no defined paths
		expect(store.paths).toEqual({})

		// create the file
		const node1 = new Folder({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/folder', id: 2 })
		emit('files:node:created', node1)

		// see the path is added
		expect(store.paths).toEqual({ files: { [node1.path]: node1.source } })

		// see that the node is added
		expect(root._children).toEqual([node1.source])

		// create the same named file again
		const node2 = new Folder({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/folder', id: 2 })
		emit('files:node:created', node2)

		// see that there is still only one paths and the children are not duplicated
		expect(store.paths).toEqual({ files: { [node1.path]: node1.source } })
		expect(root._children).toEqual([node1.source])
	})

	test('Folder is deleted', () => {
		const node = new Folder({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/folder', id: 2 })
		emit('files:node:created', node)
		// see that the path is added and the children are set-up
		expect(store.paths).toEqual({ files: { [node.path]: node.source } })
		expect(root._children).toEqual([node.source])

		emit('files:node:deleted', node)
		// See the path is removed
		expect(store.paths).toEqual({ files: {} })
		// See the child is removed
		expect(root._children).toEqual([])
	})

	test('File is deleted', () => {
		const node = new File({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/file.txt', id: 2, mime: 'text/plain' })
		emit('files:node:created', node)
		// see that the children are set-up
		expect(root._children).toEqual([node.source])

		emit('files:node:deleted', node)
		// See the child is removed
		expect(root._children).toEqual([])
	})
})