@bigfishtv/cockpit
Version:
57 lines (49 loc) • 2.23 kB
JavaScript
import { getContainer, clearAll } from './pluginRegistry'
const TestComponent1 = () => 1
const TestComponent2 = () => 2
beforeEach(clearAll)
describe('pluginRegistry', () => {
test('should register and return components', () => {
const container = getContainer('SomeId')
container.add('TestComponent1', TestComponent1)
container.add('TestComponent2', TestComponent2)
expect(container.all()).toEqual([
{ container: 'SomeId', name: 'TestComponent1', component: TestComponent1 },
{ container: 'SomeId', name: 'TestComponent2', component: TestComponent2 },
])
})
test('should throw if the same component name is used more than once', () => {
const container = getContainer('SomeId')
container.add('TestComponent1', TestComponent1)
expect(() => {
container.add('TestComponent1', TestComponent2)
}).toThrow()
})
test('should allow if the same component is used more than once with different names', () => {
const container = getContainer('SomeId')
container.add('dup1', TestComponent1)
container.add('dup2', TestComponent1)
expect(container.all()).toEqual([
{ container: 'SomeId', name: 'dup1', component: TestComponent1 },
{ container: 'SomeId', name: 'dup2', component: TestComponent1 },
])
})
test('should allow if the same component is used across different containers', () => {
const container1 = getContainer('Container1')
container1.add('TestComponent1', TestComponent1)
const container2 = getContainer('Container2')
container2.add('TestComponent1', TestComponent1)
expect(container1.all()).toEqual([{ container: 'Container1', name: 'TestComponent1', component: TestComponent1 }])
})
test('clear a single container', () => {
const container1 = getContainer('Container1')
container1.add('TestComponent1', TestComponent1)
const container2 = getContainer('Container2')
container2.add('TestComponent1', TestComponent1)
expect(container2.all()).toEqual([{ container: 'Container2', name: 'TestComponent1', component: TestComponent1 }])
container2.clear()
expect(container2.all()).toEqual([])
// container 1 is unaffected
expect(container1.all()).toEqual([{ container: 'Container1', name: 'TestComponent1', component: TestComponent1 }])
})
})