aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/src/composables/useNavigation.spec.ts
blob: 364c9ab653d6c551503781508535f9dbb7e35434 (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
/**
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
import { beforeEach, describe, expect, it, jest } from '@jest/globals'
import nextcloudFiles, { Navigation, View } from '@nextcloud/files'
import { mount } from '@vue/test-utils'
import { defineComponent } from 'vue'
import { useNavigation } from './useNavigation'

// Just a wrapper so we can test the composable
const TestComponent = defineComponent({
	template: '<div></div>',
	setup() {
		const { currentView, views } = useNavigation()
		return {
			currentView,
			views,
		}
	},
})

describe('Composables: useNavigation', () => {
	const spy = jest.spyOn(nextcloudFiles, 'getNavigation')
	let navigation: Navigation

	describe('currentView', () => {
		beforeEach(() => {
			navigation = new Navigation()
			spy.mockImplementation(() => navigation)
		})

		it('should return null without active navigation', () => {
			const wrapper = mount(TestComponent)
			expect((wrapper.vm as unknown as { currentView: View | null}).currentView).toBe(null)
		})

		it('should return already active navigation', async () => {
			const view = new View({ getContents: () => Promise.reject(new Error()), icon: '<svg></svg>', id: 'view-1', name: 'My View 1', order: 0 })
			navigation.register(view)
			navigation.setActive(view)
			// Now the navigation is already set it should take the active navigation
			const wrapper = mount(TestComponent)
			expect((wrapper.vm as unknown as { currentView: View | null}).currentView).toBe(view)
		})

		it('should be reactive on updating active navigation', async () => {
			const view = new View({ getContents: () => Promise.reject(new Error()), icon: '<svg></svg>', id: 'view-1', name: 'My View 1', order: 0 })
			navigation.register(view)
			const wrapper = mount(TestComponent)

			// no active navigation
			expect((wrapper.vm as unknown as { currentView: View | null}).currentView).toBe(null)

			navigation.setActive(view)
			// Now the navigation is set it should take the active navigation
			expect((wrapper.vm as unknown as { currentView: View | null}).currentView).toBe(view)
		})
	})

	describe('views', () => {
		beforeEach(() => {
			navigation = new Navigation()
			spy.mockImplementation(() => navigation)
		})

		it('should return empty array without registered views', () => {
			const wrapper = mount(TestComponent)
			expect((wrapper.vm as unknown as { views: View[]}).views).toStrictEqual([])
		})

		it('should return already registered views', () => {
			const view = new View({ getContents: () => Promise.reject(new Error()), icon: '<svg></svg>', id: 'view-1', name: 'My View 1', order: 0 })
			// register before mount
			navigation.register(view)
			// now mount and check that the view is listed
			const wrapper = mount(TestComponent)
			expect((wrapper.vm as unknown as { views: View[]}).views).toStrictEqual([view])
		})

		it('should be reactive on registering new views', () => {
			const view = new View({ getContents: () => Promise.reject(new Error()), icon: '<svg></svg>', id: 'view-1', name: 'My View 1', order: 0 })
			const view2 = new View({ getContents: () => Promise.reject(new Error()), icon: '<svg></svg>', id: 'view-2', name: 'My View 2', order: 1 })

			// register before mount
			navigation.register(view)
			// now mount and check that the view is listed
			const wrapper = mount(TestComponent)
			expect((wrapper.vm as unknown as { views: View[]}).views).toStrictEqual([view])

			// now register view 2 and check it is reactivly added
			navigation.register(view2)
			expect((wrapper.vm as unknown as { views: View[]}).views).toStrictEqual([view, view2])
		})
	})
})