// src/plugin/__tests__/plugin-register-view.test.ts import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' import { Plugin } from '../plugin' import { App } from '../app' import { Vault } from '../vault' import { FileIndex } from '../../services/file-index' import { Workspace } from '../workspace' import { ItemView } from '../views/item-view' class TestPlugin extends Plugin { onload() { this.registerView('test-view', (leaf) => new TestView(leaf)) } } class TestView extends ItemView { getViewType() { return 'test-view' } getDisplayText() { return 'Test' } } function createApp() { const dm = { createFile: vi.fn(), createFolder: vi.fn(), readText: vi.fn(async (url: string) => ({ status: 'unavailable' as const, documentId: url })), updateContent: vi.fn() } as any const vault = new Vault('Test', new FileIndex(dm, 'automerge:root' as any), dm) const workspace = new Workspace(document.createElement('div')) return new App(vault, workspace) } describe('Plugin.registerView delegation', () => { let app: App beforeEach(() => { app = createApp() }) afterEach(() => { app.destroy() }) it('delegates to workspace.registerViewType on load', () => { const plugin = new TestPlugin(app, { id: 'test', name: 'Test', author: 'Test', version: '1.0.0', minAppVersion: '0.0.0', description: 'Test plugin', }) plugin.load() expect(app.workspace.getViewCreator('test-view')).toBeDefined() }) it('unregisters views from workspace on unload', () => { const plugin = new TestPlugin(app, { id: 'test', name: 'Test', author: 'Test', version: '1.0.0', minAppVersion: '0.0.0', description: 'Test plugin', }) plugin.load() expect(app.workspace.getViewCreator('test-view')).toBeDefined() plugin.unload() expect(app.workspace.getViewCreator('test-view')).toBeUndefined() }) it('no-ops registerView when plugin is not loaded', () => { const plugin = new TestPlugin(app, { id: 'test', name: 'Test', author: 'Test', version: '1.0.0', minAppVersion: '0.0.0', description: 'Test plugin', }) // Don't call load plugin.registerView('manual-view', (leaf) => new TestView(leaf)) expect(app.workspace.getViewCreator('manual-view')).toBeUndefined() }) it('handles load/unload/reload cycle', () => { const plugin = new TestPlugin(app, { id: 'test', name: 'Test', author: 'Test', version: '1.0.0', minAppVersion: '0.0.0', description: 'Test plugin', }) plugin.load() expect(app.workspace.getViewCreator('test-view')).toBeDefined() plugin.unload() expect(app.workspace.getViewCreator('test-view')).toBeUndefined() plugin.load() expect(app.workspace.getViewCreator('test-view')).toBeDefined() }) it('cleans up views alongside other plugin resources', () => { const plugin = new TestPlugin(app, { id: 'test', name: 'Test', author: 'Test', version: '1.0.0', minAppVersion: '0.0.0', description: 'Test plugin', }) plugin.load() plugin.addCommand({ id: 'cmd', name: 'Test Command' }) plugin.unload() // Both command and view should be cleaned up expect(app.commands.getCommand('test:cmd')).toBeUndefined() expect(app.workspace.getViewCreator('test-view')).toBeUndefined() }) })