@@ -9,8 +9,10 @@ import userEvent from '@testing-library/user-event';
99import { TOPIC_CUSTOM_PARAMS } from 'lib/constants' ;
1010
1111const selectOption = async ( listbox : HTMLElement , option : string ) => {
12- await waitFor ( ( ) => userEvent . click ( listbox ) ) ;
13- await waitFor ( ( ) => userEvent . click ( screen . getByText ( option ) ) ) ;
12+ await waitFor ( ( ) => {
13+ userEvent . click ( listbox ) ;
14+ userEvent . click ( screen . getByText ( option ) ) ;
15+ } ) ;
1416} ;
1517
1618const expectOptionIsSelected = ( listbox : HTMLElement , option : string ) => {
@@ -19,17 +21,27 @@ const expectOptionIsSelected = (listbox: HTMLElement, option: string) => {
1921 expect ( selectedOption [ 0 ] ) . toHaveTextContent ( option ) ;
2022} ;
2123
22- const expectOptionIsDisabled = async (
24+ const expectOptionAvailability = async (
2325 listbox : HTMLElement ,
2426 option : string ,
2527 disabled : boolean
2628) => {
2729 await waitFor ( ( ) => userEvent . click ( listbox ) ) ;
28- const selectedOption = within ( listbox ) . getAllByText ( option ) ;
29- expect ( selectedOption [ 1 ] ) . toHaveStyleRule (
30+ const selectedOptions = within ( listbox ) . getAllByText ( option ) . reverse ( ) ;
31+ // its either two or one nodes, we only need last one
32+ const selectedOption = selectedOptions [ 0 ] ;
33+
34+ if ( disabled ) {
35+ expect ( selectedOption ) . toHaveAttribute ( 'disabled' ) ;
36+ } else {
37+ expect ( selectedOption ) . toBeEnabled ( ) ;
38+ }
39+
40+ expect ( selectedOption ) . toHaveStyleRule (
3041 'cursor' ,
3142 disabled ? 'not-allowed' : 'pointer'
3243 ) ;
44+ await waitFor ( ( ) => userEvent . click ( listbox ) ) ;
3345} ;
3446
3547describe ( 'CustomParams' , ( ) => {
@@ -51,15 +63,15 @@ describe('CustomParams', () => {
5163 } ) ;
5264
5365 it ( 'renders with props' , ( ) => {
54- const addParamButton = screen . getByRole ( 'button' ) ;
55- expect ( addParamButton ) . toBeInTheDocument ( ) ;
56- expect ( addParamButton ) . toHaveTextContent ( 'Add Custom Parameter' ) ;
66+ const button = screen . getByRole ( 'button' ) ;
67+ expect ( button ) . toBeInTheDocument ( ) ;
68+ expect ( button ) . toHaveTextContent ( 'Add Custom Parameter' ) ;
5769 } ) ;
5870
5971 describe ( 'works with user inputs correctly' , ( ) => {
6072 it ( 'button click creates custom param fieldset' , async ( ) => {
61- const addParamButton = screen . getByRole ( 'button' ) ;
62- await waitFor ( ( ) => userEvent . click ( addParamButton ) ) ;
73+ const button = screen . getByRole ( 'button' ) ;
74+ await waitFor ( ( ) => userEvent . click ( button ) ) ;
6375
6476 const listbox = screen . getByRole ( 'listbox' ) ;
6577 expect ( listbox ) . toBeInTheDocument ( ) ;
@@ -69,38 +81,38 @@ describe('CustomParams', () => {
6981 } ) ;
7082
7183 it ( 'can select option' , async ( ) => {
72- const addParamButton = screen . getByRole ( 'button' ) ;
73- await waitFor ( ( ) => userEvent . click ( addParamButton ) ) ;
74-
84+ const button = screen . getByRole ( 'button' ) ;
85+ await waitFor ( ( ) => userEvent . click ( button ) ) ;
7586 const listbox = screen . getByRole ( 'listbox' ) ;
7687
7788 await selectOption ( listbox , 'compression.type' ) ;
7889 expectOptionIsSelected ( listbox , 'compression.type' ) ;
79- expectOptionIsDisabled ( listbox , 'compression.type' , true ) ;
90+ await expectOptionAvailability ( listbox , 'compression.type' , true ) ;
8091
8192 const textbox = screen . getByRole ( 'textbox' ) ;
8293 expect ( textbox ) . toHaveValue ( TOPIC_CUSTOM_PARAMS [ 'compression.type' ] ) ;
8394 } ) ;
8495
8596 it ( 'when selected option changes disabled options update correctly' , async ( ) => {
86- const addParamButton = screen . getByRole ( 'button' ) ;
87- await waitFor ( ( ) => userEvent . click ( addParamButton ) ) ;
97+ const button = screen . getByRole ( 'button' ) ;
98+ await waitFor ( ( ) => userEvent . click ( button ) ) ;
8899
89100 const listbox = screen . getByRole ( 'listbox' ) ;
90101
91102 await selectOption ( listbox , 'compression.type' ) ;
92- expectOptionIsDisabled ( listbox , 'compression.type' , true ) ;
103+ expectOptionIsSelected ( listbox , 'compression.type' ) ;
104+ await expectOptionAvailability ( listbox , 'compression.type' , true ) ;
93105
94106 await selectOption ( listbox , 'delete.retention.ms' ) ;
95- expectOptionIsDisabled ( listbox , 'delete.retention.ms' , true ) ;
96- expectOptionIsDisabled ( listbox , 'compression.type' , false ) ;
107+ await expectOptionAvailability ( listbox , 'delete.retention.ms' , true ) ;
108+ await expectOptionAvailability ( listbox , 'compression.type' , false ) ;
97109 } ) ;
98110
99111 it ( 'multiple button clicks create multiple fieldsets' , async ( ) => {
100- const addParamButton = screen . getByRole ( 'button' ) ;
101- await waitFor ( ( ) => userEvent . click ( addParamButton ) ) ;
102- await waitFor ( ( ) => userEvent . click ( addParamButton ) ) ;
103- await waitFor ( ( ) => userEvent . click ( addParamButton ) ) ;
112+ const button = screen . getByRole ( 'button' ) ;
113+ await waitFor ( ( ) => userEvent . click ( button ) ) ;
114+ await waitFor ( ( ) => userEvent . click ( button ) ) ;
115+ await waitFor ( ( ) => userEvent . click ( button ) ) ;
104116
105117 const listboxes = screen . getAllByRole ( 'listbox' ) ;
106118 expect ( listboxes . length ) . toBe ( 3 ) ;
@@ -110,48 +122,64 @@ describe('CustomParams', () => {
110122 } ) ;
111123
112124 it ( "can't select already selected option" , async ( ) => {
113- const addParamButton = screen . getByRole ( 'button' ) ;
114- userEvent . click ( addParamButton ) ;
115- userEvent . click ( addParamButton ) ;
125+ const button = screen . getByRole ( 'button' ) ;
126+ await waitFor ( ( ) => userEvent . click ( button ) ) ;
127+ await waitFor ( ( ) => userEvent . click ( button ) ) ;
116128
117129 const listboxes = screen . getAllByRole ( 'listbox' ) ;
118130
119131 const firstListbox = listboxes [ 0 ] ;
120132 await selectOption ( firstListbox , 'compression.type' ) ;
121- expectOptionIsDisabled ( firstListbox , 'compression.type' , true ) ;
133+ await expectOptionAvailability ( firstListbox , 'compression.type' , true ) ;
122134
123135 const secondListbox = listboxes [ 1 ] ;
124- expectOptionIsDisabled ( secondListbox , 'compression.type' , true ) ;
136+ await expectOptionAvailability ( secondListbox , 'compression.type' , true ) ;
125137 } ) ;
126138
127139 it ( 'when fieldset with selected custom property type is deleted disabled options update correctly' , async ( ) => {
128- const addParamButton = screen . getByRole ( 'button' ) ;
129- userEvent . click ( addParamButton ) ;
130- userEvent . click ( addParamButton ) ;
131- userEvent . click ( addParamButton ) ;
140+ const button = screen . getByRole ( 'button' ) ;
141+ await waitFor ( ( ) => userEvent . click ( button ) ) ;
142+ await waitFor ( ( ) => userEvent . click ( button ) ) ;
143+ await waitFor ( ( ) => userEvent . click ( button ) ) ;
132144
133145 const listboxes = screen . getAllByRole ( 'listbox' ) ;
134146
135147 const firstListbox = listboxes [ 0 ] ;
136148 await selectOption ( firstListbox , 'compression.type' ) ;
137- expectOptionIsDisabled ( firstListbox , 'compression.type' , true ) ;
149+ await expectOptionAvailability ( firstListbox , 'compression.type' , true ) ;
138150
139151 const secondListbox = listboxes [ 1 ] ;
140152 await selectOption ( secondListbox , 'delete.retention.ms' ) ;
141- expectOptionIsDisabled ( secondListbox , 'delete.retention.ms' , true ) ;
153+ await expectOptionAvailability (
154+ secondListbox ,
155+ 'delete.retention.ms' ,
156+ true
157+ ) ;
142158
143159 const thirdListbox = listboxes [ 2 ] ;
144160 await selectOption ( thirdListbox , 'file.delete.delay.ms' ) ;
145- expectOptionIsDisabled ( thirdListbox , 'file.delete.delay.ms' , true ) ;
161+ await expectOptionAvailability (
162+ thirdListbox ,
163+ 'file.delete.delay.ms' ,
164+ true
165+ ) ;
146166
147167 const deleteSecondFieldsetButton = screen . getByTitle (
148168 'Delete customParam field 1'
149169 ) ;
150- userEvent . click ( deleteSecondFieldsetButton ) ;
170+ await waitFor ( ( ) => userEvent . click ( deleteSecondFieldsetButton ) ) ;
151171 expect ( secondListbox ) . not . toBeInTheDocument ( ) ;
152172
153- expectOptionIsDisabled ( firstListbox , 'delete.retention.ms' , false ) ;
154- expectOptionIsDisabled ( thirdListbox , 'delete.retention.ms' , false ) ;
173+ await expectOptionAvailability (
174+ firstListbox ,
175+ 'delete.retention.ms' ,
176+ false
177+ ) ;
178+ await expectOptionAvailability (
179+ thirdListbox ,
180+ 'delete.retention.ms' ,
181+ false
182+ ) ;
155183 } ) ;
156184 } ) ;
157185} ) ;
0 commit comments