11import { expect } from '@esm-bundle/chai' ;
2- import { aTimeout , esc , fixtureSync , oneEvent } from '@vaadin/testing-helpers' ;
2+ import { aTimeout , click , esc , fixtureSync , nextRender , nextUpdate } from '@vaadin/testing-helpers' ;
33import sinon from 'sinon' ;
44import '../vaadin-dialog.js' ;
55import { getDeepActiveElement } from '@vaadin/a11y-base/src/focus-utils.js' ;
@@ -25,8 +25,9 @@ describe('vaadin-dialog', () => {
2525 describe ( 'host element' , ( ) => {
2626 let dialog ;
2727
28- beforeEach ( ( ) => {
28+ beforeEach ( async ( ) => {
2929 dialog = fixtureSync ( '<vaadin-dialog></vaadin-dialog>' ) ;
30+ await nextRender ( ) ;
3031 } ) ;
3132
3233 it ( 'should enforce display: none to hide the host element' , ( ) => {
@@ -38,48 +39,62 @@ describe('vaadin-dialog', () => {
3839 describe ( 'opened' , ( ) => {
3940 let dialog , backdrop , overlay ;
4041
41- beforeEach ( ( ) => {
42+ beforeEach ( async ( ) => {
4243 dialog = fixtureSync ( `
4344 <vaadin-dialog opened theme="foo"></vaadin-dialog>
4445 ` ) ;
46+ await nextRender ( ) ;
4547
4648 dialog . renderer = ( root ) => {
4749 root . innerHTML = '<div>Simple dialog</div>' ;
4850 } ;
51+ await nextUpdate ( dialog ) ;
4952
5053 overlay = dialog . $ . overlay ;
5154 backdrop = overlay . $ . backdrop ;
5255 } ) ;
5356
57+ afterEach ( async ( ) => {
58+ dialog . opened = false ;
59+ await nextRender ( ) ;
60+ } ) ;
61+
5462 describe ( 'attributes' , ( ) => {
5563 it ( 'overlay should have the `dialog` role' , ( ) => {
5664 expect ( overlay . getAttribute ( 'role' ) ) . to . be . eql ( 'dialog' ) ;
5765 } ) ;
5866
59- it ( 'overlay should have the `aria-label` attribute (if set)' , ( ) => {
67+ it ( 'overlay should have the `aria-label` attribute (if set)' , async ( ) => {
6068 dialog . ariaLabel = 'accessible' ;
69+ await nextUpdate ( dialog ) ;
6170 expect ( overlay . getAttribute ( 'aria-label' ) ) . to . be . eql ( 'accessible' ) ;
6271 } ) ;
6372
6473 it ( 'overlay should not have the `aria-label` attribute (if not set)' , ( ) => {
6574 expect ( overlay . getAttribute ( 'aria-label' ) ) . to . be . null ;
6675 } ) ;
6776
68- it ( 'overlay should not have `aria-label` attribute if set to undefined' , ( ) => {
77+ it ( 'overlay should not have `aria-label` attribute if set to undefined' , async ( ) => {
6978 dialog . ariaLabel = 'accessible' ;
79+ await nextUpdate ( dialog ) ;
7080 dialog . ariaLabel = undefined ;
81+ await nextUpdate ( dialog ) ;
7182 expect ( overlay . getAttribute ( 'aria-label' ) ) . to . be . null ;
7283 } ) ;
7384
74- it ( 'overlay should not have `aria-label` attribute if set to null' , ( ) => {
85+ it ( 'overlay should not have `aria-label` attribute if set to null' , async ( ) => {
7586 dialog . ariaLabel = 'accessible' ;
87+ await nextUpdate ( dialog ) ;
7688 dialog . ariaLabel = null ;
89+ await nextUpdate ( dialog ) ;
7790 expect ( overlay . getAttribute ( 'aria-label' ) ) . to . be . null ;
7891 } ) ;
7992
80- it ( 'overlay should not have `aria-label` attribute if set to empty string' , ( ) => {
93+ it ( 'overlay should not have `aria-label` attribute if set to empty string' , async ( ) => {
8194 dialog . ariaLabel = 'accessible' ;
95+ await nextUpdate ( dialog ) ;
8296 dialog . ariaLabel = '' ;
97+ await nextUpdate ( dialog ) ;
8398 expect ( overlay . getAttribute ( 'aria-label' ) ) . to . be . null ;
8499 } ) ;
85100
@@ -89,28 +104,33 @@ describe('vaadin-dialog', () => {
89104 } ) ;
90105
91106 describe ( 'no-close-on-esc' , ( ) => {
92- it ( 'should close itself on ESC press by default' , ( ) => {
107+ it ( 'should close itself on ESC press by default' , async ( ) => {
93108 esc ( document . body ) ;
94- expect ( dialog . opened ) . to . eql ( false ) ;
109+ await nextUpdate ( dialog ) ;
110+ expect ( dialog . opened ) . to . be . false ;
95111 } ) ;
96112
97- it ( 'should not close itself on ESC press when no-close-on-esc is true' , ( ) => {
113+ it ( 'should not close itself on ESC press when no-close-on-esc is true' , async ( ) => {
98114 dialog . noCloseOnEsc = true ;
115+ await nextUpdate ( dialog ) ;
99116 esc ( document . body ) ;
100- expect ( dialog . opened ) . to . eql ( true ) ;
117+ expect ( dialog . opened ) . to . be . true ;
101118 } ) ;
102119 } ) ;
103120
104121 describe ( 'no-close-on-outside-click' , ( ) => {
105- it ( 'should close itself on outside click by default' , ( ) => {
106- backdrop . dispatchEvent ( new CustomEvent ( 'click' , { bubbles : true , composed : true } ) ) ;
107- expect ( dialog . opened ) . to . eql ( false ) ;
122+ it ( 'should close itself on outside click by default' , async ( ) => {
123+ click ( backdrop ) ;
124+ await nextUpdate ( dialog ) ;
125+ expect ( dialog . opened ) . to . be . false ;
108126 } ) ;
109127
110- it ( 'should not close itself on outside click when no-close-on-outside-click is true' , ( ) => {
128+ it ( 'should not close itself on outside click when no-close-on-outside-click is true' , async ( ) => {
111129 dialog . noCloseOnOutsideClick = true ;
112- backdrop . dispatchEvent ( new CustomEvent ( 'click' , { bubbles : true , composed : true } ) ) ;
113- expect ( dialog . opened ) . to . eql ( true ) ;
130+ await nextUpdate ( dialog ) ;
131+ click ( backdrop ) ;
132+ await nextUpdate ( dialog ) ;
133+ expect ( dialog . opened ) . to . be . true ;
114134 } ) ;
115135 } ) ;
116136
@@ -160,8 +180,9 @@ describe('vaadin-dialog', () => {
160180 expect ( backdrop . hidden ) . to . be . false ;
161181 } ) ;
162182
163- it ( 'should not be modal when modeless is true' , ( ) => {
183+ it ( 'should not be modal when modeless is true' , async ( ) => {
164184 dialog . modeless = true ;
185+ await nextUpdate ( dialog ) ;
165186 expect ( overlay . modeless ) . to . be . true ;
166187 expect ( backdrop . hidden ) . to . be . true ;
167188 } ) ;
@@ -171,8 +192,14 @@ describe('vaadin-dialog', () => {
171192 describe ( 'without renderer' , ( ) => {
172193 let dialog ;
173194
174- beforeEach ( ( ) => {
195+ beforeEach ( async ( ) => {
175196 dialog = fixtureSync ( '<vaadin-dialog></vaadin-dialog>' ) ;
197+ await nextRender ( ) ;
198+ } ) ;
199+
200+ afterEach ( async ( ) => {
201+ dialog . opened = false ;
202+ await nextRender ( ) ;
176203 } ) ;
177204
178205 it ( 'should not throw an exception if renderer is not present' , ( ) => {
@@ -182,8 +209,9 @@ describe('vaadin-dialog', () => {
182209 expect ( openDialog ) . to . not . throw ( ) ;
183210 } ) ;
184211
185- it ( 'should have min-width for content' , ( ) => {
212+ it ( 'should have min-width for content' , async ( ) => {
186213 dialog . opened = true ;
214+ await nextRender ( ) ;
187215 const contentMinWidth = parseFloat ( getComputedStyle ( dialog . $ . overlay . $ . content ) . minWidth ) ;
188216 expect ( contentMinWidth ) . to . be . gt ( 0 ) ;
189217 } ) ;
@@ -192,29 +220,35 @@ describe('vaadin-dialog', () => {
192220 describe ( 'focus restoration' , ( ) => {
193221 let dialog , button , overlay ;
194222
195- beforeEach ( ( ) => {
223+ beforeEach ( async ( ) => {
196224 const wrapper = fixtureSync ( `
197225 <div>
198226 <vaadin-dialog></vaadin-dialog>
199227 <button></button>
200228 </div>
201229 ` ) ;
202230 [ dialog , button ] = wrapper . children ;
231+ await nextRender ( ) ;
203232 overlay = dialog . $ . overlay ;
204233 button . focus ( ) ;
205234 } ) ;
206235
236+ afterEach ( async ( ) => {
237+ dialog . opened = false ;
238+ await nextRender ( ) ;
239+ } ) ;
240+
207241 it ( 'should move focus to the dialog on open' , async ( ) => {
208242 dialog . opened = true ;
209- await oneEvent ( overlay , 'vaadin-overlay-open' ) ;
243+ await nextRender ( ) ;
210244 expect ( getDeepActiveElement ( ) ) . to . equal ( overlay . $ . overlay ) ;
211245 } ) ;
212246
213247 it ( 'should restore focus on dialog close' , async ( ) => {
214248 dialog . opened = true ;
215- await oneEvent ( overlay , 'vaadin-overlay-open' ) ;
249+ await nextRender ( ) ;
216250 dialog . opened = false ;
217- await aTimeout ( 0 ) ;
251+ await nextRender ( ) ;
218252 expect ( getDeepActiveElement ( ) ) . to . equal ( button ) ;
219253 } ) ;
220254 } ) ;
0 commit comments