66 */
77
88import React from 'react' ;
9- import type { ComponentType , ReactWrapper } from 'enzyme' ;
10- import { mount } from 'enzyme' ;
119import { render , screen } from '@testing-library/react' ;
1210import userEvent from '@testing-library/user-event' ;
13- import { EuiText } from '@elastic/eui' ;
1411
1512import type { ConfigureCaseButtonProps , CaseDetailsLinkProps } from '.' ;
1613import { ConfigureCaseButton , CaseDetailsLink } from '.' ;
@@ -20,89 +17,53 @@ import { useCaseViewNavigation } from '../../common/navigation/hooks';
2017jest . mock ( '../../common/navigation/hooks' ) ;
2118
2219describe ( 'Configuration button' , ( ) => {
23- let wrapper : ReactWrapper ;
2420 const props : ConfigureCaseButtonProps = {
2521 label : 'My label' ,
2622 msgTooltip : < > </ > ,
2723 showToolTip : false ,
2824 titleTooltip : '' ,
2925 } ;
3026
31- beforeAll ( ( ) => {
32- wrapper = mount ( < ConfigureCaseButton { ...props } /> , {
33- wrappingComponent : TestProviders as ComponentType < React . PropsWithChildren < { } > > ,
34- } ) ;
35- } ) ;
36-
37- test ( 'it renders without the tooltip' , ( ) => {
38- expect ( wrapper . find ( '[data-test-subj="configure-case-button"]' ) . first ( ) . exists ( ) ) . toBe ( true ) ;
27+ it ( 'renders without the tooltip' , async ( ) => {
28+ render (
29+ < TestProviders >
30+ < ConfigureCaseButton { ...props } />
31+ </ TestProviders >
32+ ) ;
3933
40- expect ( wrapper . find ( '[data-test-subj="configure-case-tooltip"]' ) . first ( ) . exists ( ) ) . toBe ( false ) ;
41- } ) ;
34+ const configureButton = await screen . findByTestId ( 'configure-case-button' ) ;
4235
43- test ( 'it pass the correct props to the button' , ( ) => {
44- expect ( wrapper . find ( '[data-test-subj="configure-case-button"]' ) . first ( ) . props ( ) ) . toMatchObject ( {
45- href : `/app/security/cases/configure` ,
46- iconType : 'controlsHorizontal' ,
47- isDisabled : false ,
48- 'aria-label' : 'My label' ,
49- children : 'My label' ,
50- } ) ;
36+ expect ( configureButton ) . toBeEnabled ( ) ;
37+ expect ( configureButton ) . toHaveAttribute ( 'href' , '/app/security/cases/configure' ) ;
38+ expect ( configureButton ) . toHaveAttribute ( 'aria-label' , 'My label' ) ;
5139 } ) ;
5240
53- test ( 'it renders the tooltip' , ( ) => {
54- const msgTooltip = < EuiText > { 'My message tooltip' } </ EuiText > ;
55-
56- const newWrapper = mount (
57- < ConfigureCaseButton
58- { ...props }
59- showToolTip = { true }
60- titleTooltip = { 'My tooltip title' }
61- msgTooltip = { msgTooltip }
62- /> ,
63- {
64- wrappingComponent : TestProviders as ComponentType < React . PropsWithChildren < { } > > ,
65- }
66- ) ;
67-
68- expect ( newWrapper . find ( '[data-test-subj="configure-case-tooltip"]' ) . first ( ) . exists ( ) ) . toBe (
69- true
70- ) ;
41+ it ( 'renders the tooltip correctly when hovering the button' , async ( ) => {
42+ jest . useFakeTimers ( ) ;
7143
72- expect ( wrapper . find ( '[data-test-subj="configure-case-button"]' ) . first ( ) . exists ( ) ) . toBe ( true ) ;
73- } ) ;
44+ const user = userEvent . setup ( {
45+ advanceTimers : jest . advanceTimersByTime ,
46+ pointerEventsCheck : 0 ,
47+ } ) ;
7448
75- test ( 'it shows the tooltip when hovering the button' , ( ) => {
76- // Use fake timers so we don't have to wait for the EuiToolTip timeout
77- jest . useFakeTimers ( { legacyFakeTimers : true } ) ;
78-
79- const msgTooltip = 'My message tooltip' ;
80- const titleTooltip = 'My title' ;
81-
82- const newWrapper = mount (
83- < ConfigureCaseButton
84- { ...props }
85- showToolTip = { true }
86- titleTooltip = { titleTooltip }
87- msgTooltip = { < > { msgTooltip } </ > }
88- /> ,
89- {
90- wrappingComponent : TestProviders as ComponentType < React . PropsWithChildren < { } > > ,
91- }
49+ render (
50+ < TestProviders >
51+ < ConfigureCaseButton
52+ { ...props }
53+ showToolTip = { true }
54+ titleTooltip = { 'My title' }
55+ msgTooltip = { < > { 'My message tooltip' } </ > }
56+ />
57+ </ TestProviders >
9258 ) ;
9359
94- newWrapper . find ( 'a[data-test-subj=" configure-case-button"]' ) . first ( ) . simulate ( 'mouseOver' ) ;
60+ await user . hover ( await screen . findByTestId ( ' configure-case-button' ) ) ;
9561
96- // Run the timers so the EuiTooltip will be visible
97- jest . runAllTimers ( ) ;
62+ expect ( await screen . findByTestId ( 'configure-case-tooltip' ) ) . toBeInTheDocument ( ) ;
63+ expect ( await screen . findByText ( 'My title' ) ) . toBeInTheDocument ( ) ;
64+ expect ( await screen . findByText ( 'My message tooltip' ) ) . toBeInTheDocument ( ) ;
9865
99- newWrapper . update ( ) ;
100- expect ( newWrapper . find ( '.euiToolTipPopover' ) . last ( ) . text ( ) ) . toBe (
101- `${ titleTooltip } ${ msgTooltip } `
102- ) ;
103-
104- // Clearing all mocks will also reset fake timers.
105- jest . clearAllMocks ( ) ;
66+ jest . useRealTimers ( ) ;
10667 } ) ;
10768} ) ;
10869
@@ -120,31 +81,33 @@ describe('CaseDetailsLink', () => {
12081 useCaseViewNavigationMock . mockReturnValue ( { getCaseViewUrl, navigateToCaseView } ) ;
12182 } ) ;
12283
123- test ( 'it renders', ( ) => {
84+ it ( ' renders', async ( ) => {
12485 render ( < CaseDetailsLink { ...props } /> ) ;
125- expect ( screen . getByText ( 'test detail name' ) ) . toBeInTheDocument ( ) ;
86+ expect ( await screen . findByText ( 'test detail name' ) ) . toBeInTheDocument ( ) ;
12687 } ) ;
12788
128- test ( 'it renders the children instead of the detail name if provided', ( ) => {
89+ it ( ' renders the children instead of the detail name if provided', async ( ) => {
12990 render ( < CaseDetailsLink { ...props } > { 'children' } </ CaseDetailsLink > ) ;
13091 expect ( screen . queryByText ( 'test detail name' ) ) . toBeFalsy ( ) ;
131- expect ( screen . getByText ( 'children' ) ) . toBeInTheDocument ( ) ;
92+ expect ( await screen . findByText ( 'children' ) ) . toBeInTheDocument ( ) ;
13293 } ) ;
13394
134- test ( 'it uses the detailName in the aria-label if the title is not provided', ( ) => {
95+ it ( ' uses the detailName in the aria-label if the title is not provided', async ( ) => {
13596 render ( < CaseDetailsLink { ...props } /> ) ;
13697 expect (
137- screen . getByLabelText ( `click to visit case with title ${ props . detailName } ` )
98+ await screen . findByLabelText ( `click to visit case with title ${ props . detailName } ` )
13899 ) . toBeInTheDocument ( ) ;
139100 } ) ;
140101
141- test ( 'it uses the title in the aria-label if provided', ( ) => {
102+ it ( ' uses the title in the aria-label if provided', async ( ) => {
142103 render ( < CaseDetailsLink { ...props } title = { 'my title' } /> ) ;
143- expect ( screen . getByText ( 'test detail name' ) ) . toBeInTheDocument ( ) ;
144- expect ( screen . getByLabelText ( `click to visit case with title my title` ) ) . toBeInTheDocument ( ) ;
104+ expect ( await screen . findByText ( 'test detail name' ) ) . toBeInTheDocument ( ) ;
105+ expect (
106+ await screen . findByLabelText ( `click to visit case with title my title` )
107+ ) . toBeInTheDocument ( ) ;
145108 } ) ;
146109
147- test ( 'it calls navigateToCaseViewClick on click', async ( ) => {
110+ it ( ' calls navigateToCaseViewClick on click', async ( ) => {
148111 // Workaround for timeout via https://github.com/testing-library/user-event/issues/833#issuecomment-1171452841
149112 jest . useFakeTimers ( ) ;
150113 const user = userEvent . setup ( {
@@ -153,19 +116,21 @@ describe('CaseDetailsLink', () => {
153116 } ) ;
154117
155118 render ( < CaseDetailsLink { ...props } /> ) ;
156- await user . click ( screen . getByText ( 'test detail name' ) ) ;
119+
120+ await user . click ( await screen . findByText ( 'test detail name' ) ) ;
121+
157122 expect ( navigateToCaseView ) . toHaveBeenCalledWith ( {
158123 detailName : props . detailName ,
159124 } ) ;
160125
161126 jest . useRealTimers ( ) ;
162127 } ) ;
163128
164- test ( 'it set the href correctly', ( ) => {
129+ it ( 'sets the href correctly', async ( ) => {
165130 render ( < CaseDetailsLink { ...props } /> ) ;
166131 expect ( getCaseViewUrl ) . toHaveBeenCalledWith ( {
167132 detailName : props . detailName ,
168133 } ) ;
169- expect ( screen . getByRole ( 'link' ) ) . toHaveAttribute ( 'href' , '/cases/test' ) ;
134+ expect ( await screen . findByRole ( 'link' ) ) . toHaveAttribute ( 'href' , '/cases/test' ) ;
170135 } ) ;
171136} ) ;
0 commit comments