@@ -2,7 +2,12 @@ import { ReactWrapper } from 'enzyme';
2
2
import ReactDOM from 'react-dom' ;
3
3
import { findByTestId , getByTestId } from '../../test-utils/enzyme-selectors' ;
4
4
import { expectFocusToBeOn } from '../../test-utils/enzyme-utils' ;
5
- import { mountWithProviders , mountWithTheme , renderWithProviders } from '../../test-utils/renderer' ;
5
+ import {
6
+ actAndWaitForEffects ,
7
+ mountWithProviders ,
8
+ mountWithTheme ,
9
+ renderWithProviders ,
10
+ } from '../../test-utils/renderer' ;
6
11
import { Tab , Tabs } from './tabs' ;
7
12
8
13
function givenTabs ( amount : number ) : Tab [ ] {
@@ -82,44 +87,52 @@ describe('Tabs', () => {
82
87
expect ( getByTestId ( wrapper , 'tab-panel-1' ) . exists ( ) ) . toBe ( false ) ;
83
88
} ) ;
84
89
85
- test ( 'tab panel should not change if onBeforeUnload cancels tab selection' , ( ) => {
90
+ test ( 'tab panel should not change if onBeforeUnload cancels tab selection' , async ( ) => {
86
91
const tabs : Tab [ ] = givenTabs ( 2 ) ;
87
- const shouldConfirmTabUnload = false ;
92
+ const shouldConfirmTabUnload = Promise . resolve ( false ) ;
88
93
tabs [ 0 ] = {
89
94
...tabs [ 0 ] ,
90
95
onBeforeUnload : ( ) => shouldConfirmTabUnload ,
91
96
} ;
92
97
const wrapper = mountWithProviders ( < Tabs tabs = { tabs } /> ) ;
93
98
94
- getByTestId ( wrapper , 'tab-button-2' ) . simulate ( 'click' ) ;
99
+ await actAndWaitForEffects ( wrapper , ( ) => {
100
+ getByTestId ( wrapper , 'tab-button-2' ) . prop ( 'onClick' ) ( ) ;
101
+ } ) ;
95
102
96
- expectPanelToBeRendered ( wrapper , 'tab-panel-1' ) ;
103
+ expect ( getByTestId ( wrapper , 'tab-panel-1' ) . exists ( ) ) . toBe ( true ) ;
97
104
} ) ;
98
105
99
- test ( 'tab panel should change if no onBeforeUnload callback was provided' , ( ) => {
106
+ test ( 'tab panel should change if no onBeforeUnload callback was provided' , async ( ) => {
100
107
const tabs : Tab [ ] = givenTabs ( 2 ) ;
101
108
tabs [ 0 ] = {
102
109
...tabs [ 0 ] ,
103
110
} ;
104
111
const wrapper = mountWithProviders ( < Tabs tabs = { tabs } /> ) ;
105
112
106
- getByTestId ( wrapper , 'tab-button-2' ) . simulate ( 'click' ) ;
113
+ await actAndWaitForEffects ( wrapper , ( ) => {
114
+ const tabButton = getByTestId ( wrapper , 'tab-button-2' ) ;
115
+ tabButton . prop ( 'onClick' ) ( ) ;
116
+ } ) ;
107
117
108
- expectPanelToBeRendered ( wrapper , 'tab-panel-2' ) ;
118
+ expect ( getByTestId ( wrapper , 'tab-panel-2' ) . exists ( ) ) . toBeTruthy ( ) ;
109
119
} ) ;
110
120
111
- test ( 'tab panel should change if onBeforeUnload confirms tab selection' , ( ) => {
121
+ test ( 'tab panel should change if onBeforeUnload confirms tab selection' , async ( ) => {
112
122
const tabs : Tab [ ] = givenTabs ( 2 ) ;
113
- const shouldConfirmTabOnClick = true ;
123
+ const shouldConfirmTabOnClick = Promise . resolve ( true ) ;
114
124
tabs [ 0 ] = {
115
125
...tabs [ 0 ] ,
116
126
onBeforeUnload : ( ) => shouldConfirmTabOnClick ,
117
127
} ;
118
128
const wrapper = mountWithProviders ( < Tabs tabs = { tabs } /> ) ;
119
129
120
- getByTestId ( wrapper , 'tab-button-2' ) . simulate ( 'click' ) ;
130
+ await actAndWaitForEffects ( wrapper , ( ) => {
131
+ const tabButton2 = getByTestId ( wrapper , 'tab-button-2' ) ;
132
+ tabButton2 . prop ( 'onClick' ) ( ) ;
133
+ } ) ;
121
134
122
- expectPanelToBeRendered ( wrapper , 'tab-panel-2' ) ;
135
+ expect ( getByTestId ( wrapper , 'tab-panel-2' ) . exists ( ) ) . toBeTruthy ( ) ;
123
136
} ) ;
124
137
125
138
test ( 'tab-panels should all be initially mounted when forceRenderTabPanels is set to true' , ( ) => {
0 commit comments