|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' |
| 2 | +import { shallowMount } from '@vue/test-utils' |
| 3 | +import MindMap from '../views/MindMap.vue' |
| 4 | + |
| 5 | +vi.mock('@nextcloud/l10n', () => ({ getLanguage: () => 'en' })) |
| 6 | +vi.mock('@nextcloud/router', () => ({ |
| 7 | + generateUrl: (path, params = {}) => |
| 8 | + Object.entries(params).reduce( |
| 9 | + (acc, [k, v]) => acc.replace(`{${k}}`, v ?? ''), |
| 10 | + path, |
| 11 | + ), |
| 12 | +})) |
| 13 | +vi.mock('@nextcloud/sharing/public', () => ({ |
| 14 | + isPublicShare: vi.fn(() => false), |
| 15 | + getSharingToken: () => '', |
| 16 | +})) |
| 17 | + |
| 18 | +// Viewer mixin that supplies the props/methods the component expects |
| 19 | +const viewerMixin = { |
| 20 | + data() { |
| 21 | + return { |
| 22 | + fileList: [], |
| 23 | + fileid: null, |
| 24 | + source: null, |
| 25 | + davPath: null, |
| 26 | + } |
| 27 | + }, |
| 28 | + methods: { |
| 29 | + doneLoading() {}, |
| 30 | + handleWebviewerloaded() {}, |
| 31 | + }, |
| 32 | +} |
| 33 | + |
| 34 | +function mountMindMap(dataOverrides = {}) { |
| 35 | + return shallowMount(MindMap, { |
| 36 | + mixins: [ |
| 37 | + { |
| 38 | + ...viewerMixin, |
| 39 | + data() { |
| 40 | + return { ...viewerMixin.data(), ...dataOverrides } |
| 41 | + }, |
| 42 | + }, |
| 43 | + ], |
| 44 | + }) |
| 45 | +} |
| 46 | + |
| 47 | +describe('MindMap.vue', () => { |
| 48 | + beforeEach(() => { |
| 49 | + window.OCA = { FilesMindMap: { setFile: vi.fn() } } |
| 50 | + }) |
| 51 | + |
| 52 | + afterEach(() => { |
| 53 | + delete window.OCA |
| 54 | + }) |
| 55 | + |
| 56 | + it('renders an iframe element', () => { |
| 57 | + const wrapper = mountMindMap() |
| 58 | + expect(wrapper.find('iframe').exists()).toBe(true) |
| 59 | + }) |
| 60 | + |
| 61 | + describe('iframeSrc computed property', () => { |
| 62 | + it('uses source when available', () => { |
| 63 | + const wrapper = mountMindMap({ |
| 64 | + source: '/remote.php/dav/files/user/test.km', |
| 65 | + }) |
| 66 | + expect(wrapper.vm.iframeSrc).toContain('/remote.php/dav/files/user/test.km') |
| 67 | + }) |
| 68 | + |
| 69 | + it('falls back to davPath when source is null', () => { |
| 70 | + const wrapper = mountMindMap({ |
| 71 | + source: null, |
| 72 | + davPath: '/dav/path/test.km', |
| 73 | + }) |
| 74 | + expect(wrapper.vm.iframeSrc).toContain('/dav/path/test.km') |
| 75 | + }) |
| 76 | + }) |
| 77 | + |
| 78 | + describe('file computed property', () => { |
| 79 | + it('returns the file whose fileid matches the current fileid', () => { |
| 80 | + const file = { fileid: 7, name: 'test.km' } |
| 81 | + const wrapper = mountMindMap({ fileList: [file], fileid: 7 }) |
| 82 | + expect(wrapper.vm.file).toBe(file) |
| 83 | + }) |
| 84 | + |
| 85 | + it('returns undefined when no file matches', () => { |
| 86 | + const wrapper = mountMindMap({ |
| 87 | + fileList: [{ fileid: 1, name: 'other.km' }], |
| 88 | + fileid: 99, |
| 89 | + }) |
| 90 | + expect(wrapper.vm.file).toBeUndefined() |
| 91 | + }) |
| 92 | + }) |
| 93 | + |
| 94 | + describe('lifecycle hooks', () => { |
| 95 | + it('calls OCA.FilesMindMap.setFile on mount', () => { |
| 96 | + mountMindMap({ |
| 97 | + fileList: [{ fileid: 3, name: 'test.km' }], |
| 98 | + fileid: 3, |
| 99 | + }) |
| 100 | + expect(window.OCA.FilesMindMap.setFile).toHaveBeenCalled() |
| 101 | + }) |
| 102 | + |
| 103 | + it('removes the webviewerloaded event listener on destroy', () => { |
| 104 | + const spy = vi.spyOn(document, 'removeEventListener') |
| 105 | + const wrapper = mountMindMap() |
| 106 | + wrapper.destroy() |
| 107 | + expect(spy).toHaveBeenCalledWith('webviewerloaded', expect.anything()) |
| 108 | + spy.mockRestore() |
| 109 | + }) |
| 110 | + }) |
| 111 | +}) |
0 commit comments