-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Plugins: Add unit tests for the 'PluginArea' component (#49138)
* Fix typos in the test descriptions * Add an explanation for manual cleanup Co-authored-by: Greg Ziółkowski <grzegorz@gziolo.pl>
- Loading branch information
Showing
1 changed file
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
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 ); | ||
} | ||
); | ||
} ); |
2c3bf49
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flaky tests detected in 2c3bf49.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4466009153
📝 Reported issues:
/test/e2e/specs/site-editor/writing-flow.spec.js
/test/e2e/specs/widgets/customizing-widgets.spec.js
/test/e2e/specs/site-editor/template-part.spec.js