Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugins: Add unit tests for the 'PluginArea' component #49138

Merged
merged 5 commits into from
Mar 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions packages/plugins/src/components/test/plugin-area.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/**
* External dependencies
*/
import { act, render, cleanup } from '@testing-library/react';

/**
* Internal dependencies
*/
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();
Mamaduka marked this conversation as resolved.
Show resolved Hide resolved
getPlugins().forEach( ( plugin ) => {
unregisterPlugin( plugin.name );
} );
getPlugins( 'my-app' ).forEach( ( plugin ) => {
unregisterPlugin( plugin.name );
} );
} );

const TestComponent = ( { content } ) => {
return `plugin: ${ content }.`;
};

test( 'renders unscoped plugin', () => {
registerPlugin( 'unscoped', {
render: () => <TestComponent content="unscoped" />,
icon: 'smiley',
} );

const { container } = render( <PluginArea /> );

expect( container ).toHaveTextContent( 'plugin: unscoped.' );
} );

test( 'renders scoped plugin', () => {
registerPlugin( 'scoped', {
render: () => <TestComponent content="scoped" />,
icon: 'smiley',
scope: 'my-app',
} );

const { container } = render( <PluginArea scope="my-app" /> );

expect( container ).toHaveTextContent( 'plugin: scoped.' );
} );

test( 'rerenders when a new plugin is registered', () => {
registerPlugin( 'foo', {
render: () => <TestComponent content="foo" />,
icon: 'smiley',
scope: 'my-app',
} );

const { container } = render( <PluginArea scope="my-app" /> );

act( () => {
registerPlugin( 'bar', {
render: () => <TestComponent content="bar" />,
icon: 'smiley',
scope: 'my-app',
} );
} );

expect( container ).toHaveTextContent( 'plugin: bar.' );
} );

test( 'rerenders when a plugin is unregistered', () => {
registerPlugin( 'one', {
render: () => <TestComponent content="one" />,
icon: 'smiley',
scope: 'my-app',
} );
registerPlugin( 'two', {
render: () => <TestComponent content="two" />,
icon: 'smiley',
scope: 'my-app',
} );

const { container } = render( <PluginArea scope="my-app" /> );

expect( container ).toHaveTextContent( 'plugin: one.plugin: two.' );

act( () => {
unregisterPlugin( 'one' );
} );

expect( container ).toHaveTextContent( 'plugin: two.' );
} );

test.failing(
'does not rerender when a plugin is added to a different scope',
() => {
const ComponentSpy = jest.fn( ( { content } ) => {
return `plugin: ${ content }.`;
} );

registerPlugin( 'scoped', {
render: () => <ComponentSpy content="scoped" />,
icon: 'smiley',
scope: 'my-app',
} );

render( <PluginArea scope="my-app" /> );

act( () => {
registerPlugin( 'unscoped', {
render: () => <TestComponent content="unscoped" />,
icon: 'smiley',
} );
} );

// Any store update triggers setState and causes PluginArea to rerender.
expect( ComponentSpy ).toHaveBeenCalledTimes( 1 );
Comment on lines +117 to +118
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope to fix this during the refactoring.

}
);
} );