Barazo default frontend barazo.forum
at main 97 lines 3.6 kB view raw
1/** 2 * Tests for plugin component registry. 3 */ 4 5import { describe, it, expect, beforeEach } from 'vitest' 6import type { ComponentType } from 'react' 7import { registerPluginComponent, getPluginComponents, clearPluginRegistry } from './registry' 8 9// Stub components for testing 10const StubComponentA = (() => null) as ComponentType<Record<string, unknown>> 11const StubComponentB = (() => null) as ComponentType<Record<string, unknown>> 12 13beforeEach(() => { 14 clearPluginRegistry() 15}) 16 17describe('registerPluginComponent', () => { 18 it('adds a component to a slot', () => { 19 registerPluginComponent('post-content', 'test-plugin', StubComponentA) 20 21 const components = getPluginComponents('post-content') 22 expect(components).toHaveLength(1) 23 expect(components[0]!.pluginName).toBe('test-plugin') 24 expect(components[0]!.component).toBe(StubComponentA) 25 }) 26 27 it('allows multiple plugins in the same slot', () => { 28 registerPluginComponent('post-content', 'plugin-a', StubComponentA) 29 registerPluginComponent('post-content', 'plugin-b', StubComponentB) 30 31 const components = getPluginComponents('post-content') 32 expect(components).toHaveLength(2) 33 expect(components[0]!.pluginName).toBe('plugin-a') 34 expect(components[1]!.pluginName).toBe('plugin-b') 35 }) 36 37 it('prevents duplicate registration for same plugin and slot', () => { 38 registerPluginComponent('post-content', 'test-plugin', StubComponentA) 39 registerPluginComponent('post-content', 'test-plugin', StubComponentB) 40 41 const components = getPluginComponents('post-content') 42 expect(components).toHaveLength(1) 43 expect(components[0]!.component).toBe(StubComponentA) 44 }) 45 46 it('allows same plugin in different slots', () => { 47 registerPluginComponent('post-content', 'test-plugin', StubComponentA) 48 registerPluginComponent('topic-sidebar', 'test-plugin', StubComponentA) 49 50 expect(getPluginComponents('post-content')).toHaveLength(1) 51 expect(getPluginComponents('topic-sidebar')).toHaveLength(1) 52 }) 53}) 54 55describe('getPluginComponents', () => { 56 it('returns registered components for a slot', () => { 57 registerPluginComponent('admin-dashboard', 'dashboard-plugin', StubComponentA) 58 59 const result = getPluginComponents('admin-dashboard') 60 expect(result).toHaveLength(1) 61 expect(result[0]!.pluginName).toBe('dashboard-plugin') 62 }) 63 64 it('returns empty array for unregistered slot', () => { 65 const result = getPluginComponents('user-profile') 66 expect(result).toEqual([]) 67 }) 68 69 it('returns empty array for slot with no registrations', () => { 70 // Register in a different slot, then query an unused one 71 registerPluginComponent('post-content', 'some-plugin', StubComponentA) 72 const result = getPluginComponents('settings-community') 73 expect(result).toEqual([]) 74 }) 75}) 76 77describe('clearPluginRegistry', () => { 78 it('removes all registrations', () => { 79 registerPluginComponent('post-content', 'plugin-a', StubComponentA) 80 registerPluginComponent('topic-sidebar', 'plugin-b', StubComponentB) 81 82 clearPluginRegistry() 83 84 expect(getPluginComponents('post-content')).toEqual([]) 85 expect(getPluginComponents('topic-sidebar')).toEqual([]) 86 }) 87 88 it('allows re-registration after clearing', () => { 89 registerPluginComponent('post-content', 'test-plugin', StubComponentA) 90 clearPluginRegistry() 91 registerPluginComponent('post-content', 'test-plugin', StubComponentB) 92 93 const components = getPluginComponents('post-content') 94 expect(components).toHaveLength(1) 95 expect(components[0]!.component).toBe(StubComponentB) 96 }) 97})