@@ -12,6 +12,8 @@ import {
1212 waitFor ,
1313 waitForElementToBeRemoved ,
1414 fireEvent as dtlFireEvent ,
15+ screen as dtlScreen ,
16+ queries as dtlQueries ,
1517} from '@testing-library/dom' ;
1618import { RenderComponentOptions , RenderDirectiveOptions , RenderResult } from './models' ;
1719import { createSelectOptions , createType , tab } from './user-events' ;
@@ -178,7 +180,7 @@ export async function render<SutType, WrapperType = SutType>(
178180 tab,
179181 waitFor : componentWaitFor ,
180182 waitForElementToBeRemoved : componentWaitForElementToBeRemoved ,
181- ...getQueriesForElement ( fixture . nativeElement , queries ) ,
183+ ...replaceFindWithFindAndDetectChanges ( fixture . nativeElement , getQueriesForElement ( fixture . nativeElement , queries ) ) ,
182184 ...eventsWithDetectChanges ,
183185 } ;
184186}
@@ -241,6 +243,30 @@ function addAutoImports({ imports, routes }: Pick<RenderComponentOptions<any>, '
241243 return [ ...imports , ...animations ( ) , ...routing ( ) ] ;
242244}
243245
246+ // for the findBy queries we first want to run a change detection cycle
247+ function replaceFindWithFindAndDetectChanges < T > ( container : HTMLElement , originalQueriesForContainer : T ) : T {
248+ return Object . keys ( originalQueriesForContainer ) . reduce (
249+ ( newQueries , key ) => {
250+ if ( key . startsWith ( 'find' ) ) {
251+ const getByQuery = dtlQueries [ key . replace ( 'find' , 'get' ) ] ;
252+ newQueries [ key ] = async ( text , options , waitForOptions ) => {
253+ // original implementation at https://github.com/testing-library/dom-testing-library/blob/master/src/query-helpers.js
254+ const result = await waitFor ( ( ) => {
255+ detectChangesForMountedFixtures ( ) ;
256+ return getByQuery ( container , text , options ) ;
257+ } , waitForOptions ) ;
258+ return result ;
259+ } ;
260+ } else {
261+ newQueries [ key ] = originalQueriesForContainer [ key ] ;
262+ }
263+
264+ return newQueries ;
265+ } ,
266+ { } as T ,
267+ ) ;
268+ }
269+
244270function cleanup ( ) {
245271 mountedFixtures . forEach ( cleanupAtFixture ) ;
246272}
@@ -258,20 +284,24 @@ if (typeof afterEach === 'function' && !process.env.ATL_SKIP_AUTO_CLEANUP) {
258284 } ) ;
259285}
260286
287+ function detectChangesForMountedFixtures ( ) {
288+ mountedFixtures . forEach ( fixture => fixture . detectChanges ( ) ) ;
289+ }
290+
261291export * from '@testing-library/dom' ;
262292
263293const fireEvent = Object . keys ( dtlFireEvent ) . reduce (
264294 ( events , key ) => {
265295 events [ key ] = ( element : HTMLElement , options ?: { } ) => {
266296 const result = dtlFireEvent [ key ] ( element , options ) ;
267- mountedFixtures . forEach ( fixture => {
268- fixture . detectChanges ( ) ;
269- } ) ;
297+ detectChangesForMountedFixtures ( ) ;
270298 return result ;
271299 } ;
272300 return events ;
273301 } ,
274- { } as FireFunction & FireObject ,
302+ { } as typeof dtlFireEvent ,
275303) ;
276304
277- export { fireEvent } ;
305+ const screen = replaceFindWithFindAndDetectChanges ( document . body , dtlScreen ) ;
306+
307+ export { fireEvent , screen } ;
0 commit comments