11import '@testing-library/jest-dom' ;
22import { render , screen } from '@testing-library/react' ;
33import userEvent from '@testing-library/user-event' ;
4+ import { createRef } from 'react' ;
45
56import { RadioControllerDecorator , RadioDecorator } from '../RadioDecorator' ;
67
@@ -13,6 +14,10 @@ describe('RadioDecorator', () => {
1314
1415 const Controller = RadioControllerDecorator ( 'main' ) ;
1516
17+ const expectToBeActive = ( controller , decorator ) => {
18+ expect ( controller . active ) . toBe ( decorator && decorator . handleDeactivate ) ;
19+ } ;
20+
1621 test ( 'should be activated when its prop is true on mount' , ( ) => {
1722 const Component = RadioDecorator ( { prop : 'active' } , Item ) ;
1823 render (
@@ -135,75 +140,81 @@ describe('RadioDecorator', () => {
135140 expect ( actual ) . toBe ( expected ) ;
136141 } ) ;
137142
138- // TODO This test is skipped because Component doesn't update its content text on click with React Testing Library
139- test . skip ( 'should be activated when the activated event fires' , ( ) => {
143+ test ( 'should be activated when the activated event fires' , ( ) => {
144+ const controllerRef = createRef ( ) ;
145+ const decoratorRef = createRef ( ) ;
140146 const Component = RadioDecorator ( { activate : 'onClick' , prop : 'active' } , Item ) ;
141147 render (
142- < Controller >
143- < Component />
148+ < Controller ref = { controllerRef } >
149+ < Component ref = { decoratorRef } />
144150 </ Controller >
145151 ) ;
146152 const component = screen . getByTestId ( 'span-element' ) ;
147153
148154 userEvent . click ( component ) ;
149155
150- expect ( component ) . toHaveTextContent ( 'Active' ) ;
156+ expectToBeActive ( controllerRef . current , decoratorRef . current ) ;
151157 } ) ;
152158
153- // TODO This test is skipped because Component doesn't update its content text on click with React Testing Library
154- test . skip ( 'should be deactivated when the deactivated event fires' , ( ) => {
159+ test ( 'should be deactivated when the deactivated event fires' , ( ) => {
160+ const controllerRef = createRef ( ) ;
161+ const decoratorRef = createRef ( ) ;
155162 const Component = RadioDecorator ( { deactivate : 'onClick' , prop : 'active' } , Item ) ;
156163 render (
157- < Controller >
158- < Component active />
164+ < Controller ref = { controllerRef } >
165+ < Component active ref = { decoratorRef } />
159166 </ Controller >
160167 ) ;
161168 const component = screen . getByTestId ( 'span-element' ) ;
162169
163170 userEvent . click ( component ) ;
164171
165- expect ( component ) . toHaveTextContent ( 'Inactive' ) ;
172+ expectToBeActive ( controllerRef . current , null ) ;
166173 } ) ;
167174
168- // TODO This test is skipped because Component doesn't update its content text on click with React Testing Library
169- test . skip ( 'should be deactivated when the activated event fires on another instance' , ( ) => {
175+ test ( 'should be deactivated when the activated event fires on another instance' , ( ) => {
176+ const controllerRef = createRef ( ) ;
177+ const decoratorRef = createRef ( ) ;
170178 const Component = RadioDecorator ( { activate : 'onClick' , prop : 'active' } , Item ) ;
171179 render (
172- < Controller >
180+ < Controller ref = { controllerRef } >
173181 < Component active />
174- < Component />
182+ < Component ref = { decoratorRef } />
175183 </ Controller >
176184 ) ;
177- const activeComponent = screen . getByText ( 'Active' ) ;
185+
178186 const inactiveComponent = screen . getByText ( 'Inactive' ) ;
179187
180188 userEvent . click ( inactiveComponent ) ;
181189
182- expect ( activeComponent ) . toHaveTextContent ( 'Inactive' ) ;
190+ expectToBeActive ( controllerRef . current , decoratorRef . current ) ;
183191 } ) ;
184192
185- // TODO This test is skipped because we can't have access to component instance with React Testing Library
186- test . skip ( 'should not deactivate items in a ancestor controller' , ( ) => {
193+ test ( 'should not deactivate items in a ancestor controller' , ( ) => {
194+ const parentControllerRef = createRef ( ) ;
195+ const parentDecoratorRef = createRef ( ) ;
196+ const childControllerRef = createRef ( ) ;
197+ const childDecoratorRef = createRef ( ) ;
187198 const Component = RadioDecorator ( { activate : 'onClick' , prop : 'active' } , Item ) ;
188199 render (
189- < Controller >
190- < Component active />
200+ < Controller ref = { parentControllerRef } >
201+ < Component active ref = { parentDecoratorRef } />
191202 < Component />
192- < Controller data-child-controller >
203+ < Controller data-child-controller ref = { childControllerRef } >
193204 < Component active />
194- < Component />
205+ < Component ref = { childDecoratorRef } />
195206 </ Controller >
196207 </ Controller >
197208 ) ;
198- const activeComponent = screen . getAllByText ( 'Active' ) ;
199209
200- userEvent . click ( activeComponent [ 0 ] ) ;
210+ const inactiveComponents = screen . getAllByText ( 'Inactive' ) ;
211+
212+ userEvent . click ( inactiveComponents [ 1 ] ) ;
201213
202214 // Breaking the pattern of 1 expect per test in order to verify the expect change happened
203215 // (activating second component in child controller) and no unexpected change happened in
204216 // the parent controller (active component should remain the first component)
205-
206- expect ( activeComponent [ 1 ] ) . toHaveTextContent ( 'Active' ) ;
207- expect ( activeComponent [ 0 ] ) . toHaveTextContent ( 'Active' ) ;
217+ expectToBeActive ( parentControllerRef , parentDecoratorRef ) ;
218+ expectToBeActive ( childControllerRef , childDecoratorRef ) ;
208219 } ) ;
209220} ) ;
0 commit comments