1- import React from 'react' ;
2- import { render , waitFor , screen } from '@testing-library/react' ;
1+ import React , { useState } from 'react' ;
2+ import {
3+ render , waitFor , screen , fireEvent ,
4+ } from '@testing-library/react' ;
35import PluggableComponent from '.' ;
46
7+ const ToggleContentComponent = ( ) => {
8+ const [ showContent , setShowContent ] = useState ( false ) ;
9+
10+ return (
11+ < div >
12+ < button type = "button" onClick = { ( ) => setShowContent ( ( prev ) => ! prev ) } >
13+ Toggle Content
14+ </ button >
15+ { showContent && < div data-testid = "toggle-content" > Toggle On</ div > }
16+ </ div >
17+ ) ;
18+ } ;
19+
520describe ( 'PluggableComponent' , ( ) => {
21+ beforeEach ( ( ) => {
22+ jest . resetModules ( ) ;
23+ } ) ;
624 it ( 'renders correctly' , async ( ) => {
25+ const handleClickMock = jest . fn ( ) ;
726 const props = {
8- isValid : true ,
9- controlId : 'randomID' ,
10- label : 'Hello' ,
11- feedbackText : 'You are correct' ,
27+ title : 'button title' ,
28+ handleClick : handleClickMock ,
1229 } ;
30+
1331 const { container } = render (
1432 < PluggableComponent
1533 id = "pluggableComponent"
16- as = "communications-app-input-form "
34+ as = "communications-app-test-component "
1735 { ...props }
1836 >
1937 < h1 > Hi this is the original component</ h1 >
2038 </ PluggableComponent > ,
2139 ) ;
2240
2341 await waitFor ( ( ) => {
24- const inputForm = screen . getByTestId ( 'plugin-input' ) ;
25- expect ( inputForm ) . toBeInTheDocument ( ) ;
26- expect ( screen . getByText ( props . label ) ) . toBeInTheDocument ( ) ;
27- expect ( screen . getByText ( props . feedbackText ) ) . toBeInTheDocument ( ) ;
42+ const buttonComponent = screen . getByTestId ( 'button-test' ) ;
43+ expect ( buttonComponent ) . toBeInTheDocument ( ) ;
44+ expect ( screen . getByText ( props . title ) ) . toBeInTheDocument ( ) ;
45+ fireEvent . click ( buttonComponent ) ;
46+ expect ( handleClickMock ) . toHaveBeenCalled ( ) ;
2847 expect ( container ) . toMatchSnapshot ( ) ;
2948 } ) ;
3049 } ) ;
@@ -43,7 +62,7 @@ describe('PluggableComponent', () => {
4362 } ) ;
4463 } ) ;
4564
46- it ( 'loads children component when import object ' , async ( ) => {
65+ it ( 'loads children component when import is empty ' , async ( ) => {
4766 render (
4867 < PluggableComponent
4968 id = "test-pluggable"
@@ -59,4 +78,69 @@ describe('PluggableComponent', () => {
5978 expect ( defaultComponent ) . toBeInTheDocument ( ) ;
6079 } ) ;
6180 } ) ;
81+
82+ it ( 'returns null when do not have children and import is invalid' , async ( ) => {
83+ render (
84+ < PluggableComponent
85+ id = "test-pluggable"
86+ as = "invalid-module"
87+ /> ,
88+ ) ;
89+
90+ await waitFor ( ( ) => {
91+ expect ( screen . queryByTestId ( 'plugin' ) ) . not . toBeInTheDocument ( ) ;
92+ } ) ;
93+ } ) ;
94+
95+ test ( 'updates component when props change' , async ( ) => {
96+ const { rerender } = render (
97+ < PluggableComponent
98+ id = "test-pluggable"
99+ as = "communications-app-test-component"
100+ title = "Testing title component"
101+ /> ,
102+ ) ;
103+
104+ await waitFor ( ( ) => {
105+ expect ( screen . getByText ( 'Testing title component' ) ) . toBeInTheDocument ( ) ;
106+ } ) ;
107+
108+ rerender (
109+ < PluggableComponent
110+ id = "test-pluggable"
111+ as = "communications-app-test-component"
112+ title = "Testing a new title component"
113+ /> ,
114+ ) ;
115+
116+ await waitFor ( ( ) => {
117+ expect ( screen . getByText ( 'Testing a new title component' ) ) . toBeInTheDocument ( ) ;
118+ } ) ;
119+ } ) ;
120+
121+ it ( 'updates component when children change' , async ( ) => {
122+ const { getByText, getByTestId } = render (
123+ < PluggableComponent
124+ id = "test-pluggable"
125+ as = "default-children"
126+ >
127+ < ToggleContentComponent />
128+ </ PluggableComponent > ,
129+ ) ;
130+
131+ await waitFor ( ( ) => {
132+ const toggleContent = screen . queryByTestId ( 'toggle-content' ) ;
133+ expect ( toggleContent ) . not . toBeInTheDocument ( ) ;
134+ } ) ;
135+
136+ const toggleButton = getByText ( 'Toggle Content' ) ;
137+ fireEvent . click ( toggleButton ) ;
138+
139+ // Now, after toggling the content, we expect it to be visible inside PluggableComponent
140+ await waitFor ( ( ) => {
141+ const toggleContent = getByTestId ( 'toggle-content' ) ;
142+ expect ( toggleContent ) . toBeInTheDocument ( ) ;
143+ expect ( toggleContent ) . toHaveTextContent ( 'Toggle On' ) ;
144+ } ) ;
145+ } ) ;
62146} ) ;
0 commit comments