import type { ComponentType } from 'react'; import { afterEach, describe, expect, test, vi } from 'vitest'; // eslint-disable-next-line testing-library/no-manual-cleanup import { act, render, cleanup } from '@testing-library/react'; import { getPlugins, unregisterPlugin, registerPlugin } from '../../api'; import PluginArea from '../plugin-area'; describe( 'PluginArea', () => { afterEach( () => { // Unmount components before unregistering the plugins. // RTL uses top-level `afterEach` for cleanup, executed after this teardown. cleanup(); getPlugins().forEach( ( plugin ) => { unregisterPlugin( plugin.name ); } ); getPlugins( 'my-app' ).forEach( ( plugin ) => { unregisterPlugin( plugin.name ); } ); } ); const TestComponent = ( { content }: { content: string } ) => { return `plugin: ${ content }.`; }; test( 'renders unscoped plugin', () => { registerPlugin( 'unscoped', { render: () => , icon: 'smiley', } ); const { container } = render( ); expect( container ).toHaveTextContent( 'plugin: unscoped.' ); } ); test( 'renders scoped plugin', () => { registerPlugin( 'scoped', { render: () => , icon: 'smiley', scope: 'my-app', } ); const { container } = render( ); expect( container ).toHaveTextContent( 'plugin: scoped.' ); } ); test( 'rerenders when a new plugin is registered', () => { registerPlugin( 'foo', { render: () => , icon: 'smiley', scope: 'my-app', } ); const { container } = render( ); act( () => { registerPlugin( 'bar', { render: () => , icon: 'smiley', scope: 'my-app', } ); } ); expect( container ).toHaveTextContent( 'plugin: bar.' ); } ); test( 'rerenders when a plugin is unregistered', () => { registerPlugin( 'one', { render: () => , icon: 'smiley', scope: 'my-app', } ); registerPlugin( 'two', { render: () => , icon: 'smiley', scope: 'my-app', } ); const { container } = render( ); expect( container ).toHaveTextContent( 'plugin: one.plugin: two.' ); act( () => { unregisterPlugin( 'one' ); } ); expect( container ).toHaveTextContent( 'plugin: two.' ); } ); test( 'does not rerender when a plugin is added to a different scope', () => { const ComponentSpy: ComponentType< { content: string } > = vi.fn( ( { content }: { content: string } ) => { return `plugin: ${ content }.`; } ); registerPlugin( 'scoped', { render: () => , icon: 'smiley', scope: 'my-app', } ); render( ); act( () => { registerPlugin( 'unscoped', { render: () => , icon: 'smiley', } ); } ); expect( ComponentSpy ).toHaveBeenCalledTimes( 1 ); } ); } );