1- import { render , screen } from '@testing-library/vue' ;
1+ import { render , screen , waitFor } from '@testing-library/vue' ;
22import userEvent from '@testing-library/user-event' ;
33import { createLocalVue } from '@vue/test-utils' ;
44import Vuex from 'vuex' ;
@@ -65,6 +65,9 @@ const renderComponent = (props = {}) => {
6565 store,
6666 props : defaultProps ,
6767 routes : new VueRouter ( ) ,
68+ stubs : {
69+ transition : true ,
70+ } ,
6871 } ) ;
6972} ;
7073
@@ -84,9 +87,14 @@ describe('EmailUsersDialog', () => {
8487 expect ( screen . getByText ( '[email protected] ' ) ) . toBeInTheDocument ( ) ; 8588 } ) ;
8689
87- it ( 'displays individual user chips when initialRecipients are provided' , ( ) => {
90+ it ( 'displays individual user chips when initialRecipients are provided' , async ( ) => {
8891 renderComponent ( { initialRecipients : [ userId , userId2 ] } ) ;
89- expect ( screen . getByText ( 'Testy User' ) ) . toBeInTheDocument ( ) ;
92+
93+ // Use waitFor to handle async rendering
94+ await waitFor ( ( ) => {
95+ expect ( screen . getByText ( 'Testy User' ) ) . toBeInTheDocument ( ) ;
96+ } ) ;
97+
9098 expect ( screen . getByText ( 'Testier User' ) ) . toBeInTheDocument ( ) ;
9199 } ) ;
92100
@@ -122,15 +130,17 @@ describe('EmailUsersDialog', () => {
122130
123131 await user . click ( screen . getByText ( 'Send email' ) ) ;
124132
125- expect ( screen . getByText ( 'Field is required' ) ) . toBeInTheDocument ( ) ;
133+ const errorMessages = screen . getAllByText ( 'Field is required' ) ;
134+ expect ( errorMessages . length ) . toBeGreaterThan ( 0 ) ;
126135 expect ( mockActions . sendEmail ) . not . toHaveBeenCalled ( ) ;
127136 } ) ;
128137
129138 it ( 'does not submit when subject is empty' , async ( ) => {
130139 const user = userEvent . setup ( ) ;
131140 renderComponent ( { initialRecipients : [ userId ] } ) ;
132141
133- await user . type ( screen . getByLabelText ( 'Email body' ) , 'Test Message' ) ;
142+ const messageInput = screen . getByLabelText ( / e m a i l b o d y / i) ;
143+ await user . type ( messageInput , 'Test Message' ) ;
134144 await user . click ( screen . getByText ( 'Send email' ) ) ;
135145
136146 expect ( mockActions . sendEmail ) . not . toHaveBeenCalled ( ) ;
@@ -140,7 +150,8 @@ describe('EmailUsersDialog', () => {
140150 const user = userEvent . setup ( ) ;
141151 renderComponent ( { initialRecipients : [ userId ] } ) ;
142152
143- await user . type ( screen . getByLabelText ( 'Subject line' ) , 'Test Subject' ) ;
153+ const subjectInput = screen . getByLabelText ( / s u b j e c t l i n e / i) ;
154+ await user . type ( subjectInput , 'Test Subject' ) ;
144155 await user . click ( screen . getByText ( 'Send email' ) ) ;
145156
146157 expect ( mockActions . sendEmail ) . not . toHaveBeenCalled ( ) ;
@@ -152,7 +163,7 @@ describe('EmailUsersDialog', () => {
152163 const user = userEvent . setup ( ) ;
153164 renderComponent ( { initialRecipients : [ userId ] } ) ;
154165
155- const messageInput = screen . getByLabelText ( 'Email body' ) ;
166+ const messageInput = screen . getByLabelText ( / e m a i l b o d y / i ) ;
156167 await user . type ( messageInput , 'Hello ' ) ;
157168 await user . click ( screen . getByText ( 'First name' ) ) ;
158169
@@ -165,8 +176,13 @@ describe('EmailUsersDialog', () => {
165176 const user = userEvent . setup ( ) ;
166177 renderComponent ( { initialRecipients : [ userId , userId2 ] } ) ;
167178
168- await user . type ( screen . getByLabelText ( 'Subject line' ) , 'Test Subject' ) ;
169- await user . type ( screen . getByLabelText ( 'Email body' ) , 'Test Message' ) ;
179+ // Wait for recipients to be loaded
180+ await waitFor ( ( ) => {
181+ expect ( screen . getByText ( 'Testy User' ) ) . toBeInTheDocument ( ) ;
182+ } ) ;
183+
184+ await user . type ( screen . getByLabelText ( / s u b j e c t l i n e / i) , 'Test Subject' ) ;
185+ await user . type ( screen . getByLabelText ( / e m a i l b o d y / i) , 'Test Message' ) ;
170186 await user . click ( screen . getByText ( 'Send email' ) ) ;
171187
172188 expect ( mockActions . sendEmail ) . toHaveBeenCalledWith ( expect . any ( Object ) , {
@@ -182,10 +198,20 @@ describe('EmailUsersDialog', () => {
182198 const user = userEvent . setup ( ) ;
183199 renderComponent ( { initialRecipients : [ userId , userId2 ] } ) ;
184200
201+ // Wait for chips to render
202+ await waitFor ( ( ) => {
203+ expect ( screen . getByText ( 'Testy User' ) ) . toBeInTheDocument ( ) ;
204+ } ) ;
205+
206+ // Use getAllByTestId with the correct test ID
185207 const removeButtons = screen . getAllByTestId ( 'remove' ) ;
186208 await user . click ( removeButtons [ 0 ] ) ;
187209
188- expect ( screen . queryByText ( 'Testy User' ) ) . not . toBeInTheDocument ( ) ;
210+ // Wait for the removal to take effect
211+ await waitFor ( ( ) => {
212+ expect ( screen . queryByText ( 'Testy User' ) ) . not . toBeInTheDocument ( ) ;
213+ } ) ;
214+
189215 expect ( screen . getByText ( 'Testier User' ) ) . toBeInTheDocument ( ) ;
190216 } ) ;
191217 } ) ;
@@ -201,8 +227,8 @@ describe('EmailUsersDialog', () => {
201227 } ,
202228 } ) ;
203229
204- await user . type ( screen . getByLabelText ( 'Subject line' ) , 'Bulk Email Subject' ) ;
205- await user . type ( screen . getByLabelText ( 'Email body' ) , 'Bulk Email Message' ) ;
230+ await user . type ( screen . getByLabelText ( / s u b j e c t l i n e / i ) , 'Bulk Email Subject' ) ;
231+ await user . type ( screen . getByLabelText ( / e m a i l b o d y / i ) , 'Bulk Email Message' ) ;
206232 await user . click ( screen . getByText ( 'Send email' ) ) ;
207233
208234 expect ( mockActions . sendEmail ) . toHaveBeenCalledWith ( expect . any ( Object ) , {
@@ -222,7 +248,7 @@ describe('EmailUsersDialog', () => {
222248 const user = userEvent . setup ( ) ;
223249 renderComponent ( { initialRecipients : [ userId ] } ) ;
224250
225- await user . type ( screen . getByLabelText ( 'Subject line' ) , 'Draft Subject' ) ;
251+ await user . type ( screen . getByLabelText ( / s u b j e c t l i n e / i ) , 'Draft Subject' ) ;
226252 await user . click ( screen . getByText ( 'Cancel' ) ) ;
227253
228254 expect ( screen . getByText ( 'Draft in progress' ) ) . toBeInTheDocument ( ) ;
@@ -244,25 +270,26 @@ describe('EmailUsersDialog', () => {
244270
245271 it ( 'closes dialog when confirming discard in warning modal' , async ( ) => {
246272 const user = userEvent . setup ( ) ;
247- renderComponent ( { initialRecipients : [ userId ] } ) ;
273+ const { emitted } = renderComponent ( { initialRecipients : [ userId ] } ) ;
248274
249- await user . type ( screen . getByLabelText ( 'Subject line' ) , 'Draft Subject' ) ;
275+ await user . type ( screen . getByLabelText ( / s u b j e c t l i n e / i ) , 'Draft Subject' ) ;
250276 await user . click ( screen . getByText ( 'Cancel' ) ) ;
251277
252- const discardButton = screen . getByRole ( 'button' , { name : 'Discard draft' } ) ;
278+ const discardButton = screen . getByRole ( 'button' , { name : / d i s c a r d d r a f t / i } ) ;
253279 await user . click ( discardButton ) ;
254280
255- expect ( screen . queryByText ( 'Send Email' ) ) . not . toBeInTheDocument ( ) ;
281+ expect ( emitted ( ) . input ) . toBeTruthy ( ) ;
282+ expect ( emitted ( ) . input [ 0 ] ) . toEqual ( [ false ] ) ;
256283 } ) ;
257284
258285 it ( 'keeps dialog open when canceling warning modal' , async ( ) => {
259286 const user = userEvent . setup ( ) ;
260287 renderComponent ( { initialRecipients : [ userId ] } ) ;
261288
262- await user . type ( screen . getByLabelText ( 'Subject line' ) , 'Draft Subject' ) ;
289+ await user . type ( screen . getByLabelText ( / s u b j e c t l i n e / i ) , 'Draft Subject' ) ;
263290 await user . click ( screen . getByText ( 'Cancel' ) ) ;
264291
265- const keepOpenButton = screen . getByRole ( 'button' , { name : 'Keep open' } ) ;
292+ const keepOpenButton = screen . getByRole ( 'button' , { name : / k e e p o p e n / i } ) ;
266293 await user . click ( keepOpenButton ) ;
267294
268295 expect ( screen . getByText ( 'Send Email' ) ) . toBeInTheDocument ( ) ;
@@ -275,8 +302,8 @@ describe('EmailUsersDialog', () => {
275302 const user = userEvent . setup ( ) ;
276303 renderComponent ( { initialRecipients : [ userId ] } ) ;
277304
278- await user . type ( screen . getByLabelText ( 'Subject line' ) , 'Test Subject' ) ;
279- await user . type ( screen . getByLabelText ( 'Email body' ) , 'Test Message' ) ;
305+ await user . type ( screen . getByLabelText ( / s u b j e c t l i n e / i ) , 'Test Subject' ) ;
306+ await user . type ( screen . getByLabelText ( / e m a i l b o d y / i ) , 'Test Message' ) ;
280307 await user . click ( screen . getByText ( 'Send email' ) ) ;
281308
282309 expect ( mockActions . showSnackbarSimple ) . toHaveBeenCalledWith ( expect . any ( Object ) , 'Email sent' ) ;
@@ -287,8 +314,8 @@ describe('EmailUsersDialog', () => {
287314 const user = userEvent . setup ( ) ;
288315 renderComponent ( { initialRecipients : [ userId ] } ) ;
289316
290- await user . type ( screen . getByLabelText ( 'Subject line' ) , 'Test Subject' ) ;
291- await user . type ( screen . getByLabelText ( 'Email body' ) , 'Test Message' ) ;
317+ await user . type ( screen . getByLabelText ( / s u b j e c t l i n e / i ) , 'Test Subject' ) ;
318+ await user . type ( screen . getByLabelText ( / e m a i l b o d y / i ) , 'Test Message' ) ;
292319 await user . click ( screen . getByText ( 'Send email' ) ) ;
293320
294321 expect ( mockActions . showSnackbarSimple ) . toHaveBeenCalledWith (
0 commit comments