11import React from 'react' ;
22import { render , waitFor } from '@testing-library/react' ;
3- import { usePlugins } from './hooks' ;
4-
5- describe ( 'usePlugins' , ( ) => {
6- beforeEach ( ( ) => {
7- jest . resetModules ( ) ;
8- jest . clearAllMocks ( ) ;
9- } ) ;
3+ import MultiplePlugins from './MultiplePlugins' ;
104
5+ describe ( 'MultiplePlugins' , ( ) => {
116 const mockPlugins = [
127 { id : 'plugin1' , name : 'Plugin1' } ,
138 { id : 'plugin2' , name : 'Plugin2' } ,
149 ] ;
1510
16- /* eslint-disable react/prop-types */
17- const ComponentsContainer = ( {
18- plugins = [ ] ,
19- pluggableComponentProps = { } ,
20- prefix = '' ,
21- loadingComponent,
22- } ) => {
23- const pluginComponents = usePlugins (
24- plugins ,
25- pluggableComponentProps ,
26- prefix ,
27- loadingComponent ,
28- ) ;
11+ beforeEach ( ( ) => {
12+ jest . resetModules ( ) ;
13+ jest . clearAllMocks ( ) ;
14+ } ) ;
2915
30- return (
31- < >
32- { Object . entries ( pluginComponents ) . map ( ( [ pluginKey , Component ] ) => (
33- < div key = { pluginKey } data-testid = { pluginKey } >
34- { Component }
35- </ div >
36- ) ) }
37- </ >
16+ /* eslint-disable react/prop-types */
17+ test ( 'initializes with loading components for each plugin' , async ( ) => {
18+ const { getAllByText } = render (
19+ < MultiplePlugins
20+ plugins = { mockPlugins }
21+ pluggableComponentProps = { { } }
22+ loadingComponent = { < div > Loading... </ div > }
23+ /> ,
3824 ) ;
39- } ;
40-
41- const renderComponent = ( overrideProps ) => render ( < ComponentsContainer { ...overrideProps } /> ) ;
4225
43- test ( 'initializes with loading components for each plugin' , async ( ) => {
44- const { getByTestId } = renderComponent ( {
45- plugins : mockPlugins ,
46- pluggableComponentProps : { } ,
47- loadingComponent : < div > Loading...</ div > ,
48- } ) ;
26+ const [ pluginLoading1 , pluginLoading2 ] = getAllByText ( 'Loading...' ) ;
4927
50- expect ( getByTestId ( 'plugin1' ) ) . toHaveTextContent ( 'Loading...' ) ;
51- expect ( getByTestId ( 'plugin2' ) ) . toHaveTextContent ( 'Loading...' ) ;
28+ expect ( pluginLoading1 ) . toBeInTheDocument ( ) ;
29+ expect ( pluginLoading1 ) . toHaveTextContent ( 'Loading...' ) ;
30+ expect ( pluginLoading2 ) . toBeInTheDocument ( ) ;
31+ expect ( pluginLoading2 ) . toHaveTextContent ( 'Loading...' ) ;
5232 } ) ;
5333
5434 test ( 'loads a plugins list successfully' , async ( ) => {
5535 const mockValidPlugins = [
5636 { id : 'plugin1' , name : 'communications-app-test-component' } ,
5737 ] ;
5838
59- const MockPluginComponent = ( ) => < div > Mocked Plugin Component</ div > ;
39+ const MockPluginComponent = ( ) => < div data-testid = "plugin1" > Mocked Plugin Component</ div > ;
6040
6141 jest . mock (
6242 '@node_modules/@openedx-plugins/communications-app-test-component' ,
6343 ( ) => MockPluginComponent ,
6444 ) ;
6545
66- const { getByTestId } = renderComponent ( {
67- plugins : mockValidPlugins ,
68- pluggableComponentProps : { } ,
69- } ) ;
46+ const { getByTestId } = render (
47+ < MultiplePlugins plugins = { mockValidPlugins } pluggableComponentProps = { { } } /> ,
48+ ) ;
7049
7150 await waitFor ( ( ) => {
7251 const pluginComponent = getByTestId ( 'plugin1' ) ;
@@ -76,17 +55,19 @@ describe('usePlugins', () => {
7655 } ) ;
7756
7857 test ( 'loads a plugin successfully with prefix' , async ( ) => {
79- const MockPluginComponent = ( ) => < div > Mocked Plugin Component</ div > ;
58+ const MockPluginComponent = ( ) => < div data-testid = "communications-app-test-component" > Mocked Plugin Component</ div > ;
8059
8160 jest . mock (
8261 '@node_modules/@openedx-plugins/communications-app-test-component' ,
8362 ( ) => MockPluginComponent ,
8463 ) ;
8564
86- const { getByTestId } = renderComponent ( {
87- pluggableComponentProps : { } ,
88- prefix : 'communications-app-test' ,
89- } ) ;
65+ const { getByTestId } = render (
66+ < MultiplePlugins
67+ pluggableComponentProps = { { } }
68+ prefix = "communications-app-test"
69+ /> ,
70+ ) ;
9071
9172 await waitFor ( ( ) => {
9273 const pluginComponent = getByTestId ( 'communications-app-test-component' ) ;
@@ -96,23 +77,15 @@ describe('usePlugins', () => {
9677 } ) ;
9778
9879 test ( 'loads a plugin successfully with prefix changing component props' , async ( ) => {
99- // eslint-disable-next-line react/prop-types
100- const MockPluginComponent = ( props ) => {
101- console . log ( 'props' , props ) ;
102- return < div data-testid = "mock-plugin-props" > { props . title } </ div > ;
103- } ;
80+ const MockPluginComponent = ( props ) => < div data-testid = "mock-plugin-props" > { props . title } </ div > ;
10481
10582 jest . mock (
10683 '@node_modules/@openedx-plugins/communications-app-test-component' ,
10784 ( ) => MockPluginComponent ,
10885 ) ;
10986
110- const ComponentsContainerUpdater = ( overrideProps ) => (
111- < ComponentsContainer { ...overrideProps } />
112- ) ;
113-
11487 const { getByTestId, rerender } = render (
115- < ComponentsContainerUpdater
88+ < MultiplePlugins
11689 prefix = "communications-app-test"
11790 pluggableComponentProps = { { title : 'Initial Title' } }
11891 /> ,
@@ -126,7 +99,7 @@ describe('usePlugins', () => {
12699 } ) ;
127100
128101 rerender (
129- < ComponentsContainerUpdater
102+ < MultiplePlugins
130103 prefix = "communications-app-test"
131104 pluggableComponentProps = { { title : 'Title updated' } }
132105 /> ,
0 commit comments