|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { View, getNavigation } from '@nextcloud/files' |
| 7 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 8 | +import { renderFilesView } from './renderFilesView.ts' |
| 9 | + |
| 10 | +const VIEW_ID = 'test-view' |
| 11 | + |
| 12 | +vi.mock('../views/FilesList.vue', () => ({ |
| 13 | + default: { |
| 14 | + name: 'FilesList', |
| 15 | + props: { embedded: { type: Boolean, default: false } }, |
| 16 | + render(h) { |
| 17 | + return h('div', { attrs: { id: 'files-list-stub', 'data-embedded': String(this.embedded) } }) |
| 18 | + }, |
| 19 | + }, |
| 20 | +})) |
| 21 | + |
| 22 | +describe('renderFilesView', () => { |
| 23 | + beforeEach(() => { |
| 24 | + document.body.innerHTML = '' |
| 25 | + getNavigation().register(new View({ |
| 26 | + id: VIEW_ID, |
| 27 | + name: 'Test view', |
| 28 | + icon: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M0 0h24v24H0z" /></svg>', |
| 29 | + getContents: async () => ({ folder: {} as never, contents: [] }), |
| 30 | + })) |
| 31 | + }) |
| 32 | + |
| 33 | + afterEach(() => { |
| 34 | + getNavigation().remove(VIEW_ID) |
| 35 | + }) |
| 36 | + |
| 37 | + it('mounts only the file list, embedded, into the given element', () => { |
| 38 | + const el = document.createElement('div') |
| 39 | + document.body.appendChild(el) |
| 40 | + |
| 41 | + renderFilesView(el, VIEW_ID) |
| 42 | + |
| 43 | + const stub = document.body.querySelector('#files-list-stub') |
| 44 | + expect(stub).not.toBeNull() |
| 45 | + expect(stub?.getAttribute('data-embedded')).toBe('true') |
| 46 | + }) |
| 47 | + |
| 48 | + it('activates the requested view without touching the browser URL', () => { |
| 49 | + const el = document.createElement('div') |
| 50 | + document.body.appendChild(el) |
| 51 | + const before = window.location.href |
| 52 | + |
| 53 | + renderFilesView(el, VIEW_ID) |
| 54 | + |
| 55 | + expect(window.location.href).toBe(before) |
| 56 | + expect(getNavigation().active?.id).toBe(VIEW_ID) |
| 57 | + }) |
| 58 | + |
| 59 | + it('returns a handle that unmounts the file list', () => { |
| 60 | + const el = document.createElement('div') |
| 61 | + document.body.appendChild(el) |
| 62 | + |
| 63 | + const rendered = renderFilesView(el, VIEW_ID) |
| 64 | + rendered.destroy() |
| 65 | + |
| 66 | + expect(document.body.querySelector('#files-list-stub')).toBeNull() |
| 67 | + }) |
| 68 | +}) |
0 commit comments