diff options
Diffstat (limited to 'apps/files/src/composables/useNavigation.spec.ts')
-rw-r--r-- | apps/files/src/composables/useNavigation.spec.ts | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/apps/files/src/composables/useNavigation.spec.ts b/apps/files/src/composables/useNavigation.spec.ts new file mode 100644 index 00000000000..b9eb671a181 --- /dev/null +++ b/apps/files/src/composables/useNavigation.spec.ts @@ -0,0 +1,106 @@ +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +import type { Navigation, View } from '@nextcloud/files' + +import { beforeEach, describe, expect, it, vi } from 'vitest' +import { mount } from '@vue/test-utils' +import { defineComponent } from 'vue' + +import { useNavigation } from './useNavigation' +import * as nextcloudFiles from '@nextcloud/files' + +// 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 = vi.spyOn(nextcloudFiles, 'getNavigation') + let navigation: Navigation + + describe('currentView', () => { + beforeEach(() => { + // eslint-disable-next-line import/namespace + navigation = new nextcloudFiles.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 () => { + // eslint-disable-next-line import/namespace + const view = new nextcloudFiles.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 () => { + // eslint-disable-next-line import/namespace + const view = new nextcloudFiles.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(() => { + // eslint-disable-next-line import/namespace + navigation = new nextcloudFiles.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', () => { + // eslint-disable-next-line import/namespace + const view = new nextcloudFiles.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', () => { + // eslint-disable-next-line import/namespace + const view = new nextcloudFiles.View({ getContents: () => Promise.reject(new Error()), icon: '<svg></svg>', id: 'view-1', name: 'My View 1', order: 0 }) + // eslint-disable-next-line import/namespace + const view2 = new nextcloudFiles.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]) + }) + }) +}) |