Skip to content

Commit afd0de9

Browse files
fix: patch within queries to invoke a change detection cycle (#259)
1 parent 53a67d7 commit afd0de9

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

projects/testing-library/src/lib/testing-library.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ import {
1919
waitFor as dtlWaitFor,
2020
waitForElementToBeRemoved as dtlWaitForElementToBeRemoved,
2121
screen as dtlScreen,
22+
within as dtlWithin,
2223
waitForOptions as dtlWaitForOptions,
2324
configure as dtlConfigure,
25+
Queries,
26+
getQueriesForElement,
27+
queries as dtlQueries,
2428
} from '@testing-library/dom';
2529
import { RenderComponentOptions, RenderDirectiveOptions, RenderTemplateOptions, RenderResult } from './models';
2630
import { getConfig } from './config';
@@ -432,6 +436,18 @@ function detectChangesForMountedFixtures() {
432436
*/
433437
const screen = replaceFindWithFindAndDetectChanges(dtlScreen);
434438

439+
/**
440+
* Re-export within with patched queries
441+
*/
442+
443+
const within: typeof getQueriesForElement = <T extends Queries = typeof dtlQueries>(
444+
element: HTMLElement,
445+
queriesToBind?: T,
446+
) => {
447+
const container = dtlWithin(element, queriesToBind);
448+
return replaceFindWithFindAndDetectChanges(container);
449+
};
450+
435451
/**
436452
* Re-export waitFor with patched waitFor
437453
*/
@@ -516,8 +532,7 @@ export {
516532
queryAllByAttribute,
517533
queryByAttribute,
518534
queryHelpers,
519-
within,
520535
} from '@testing-library/dom';
521536

522537
// export patched dtl
523-
export { screen, waitFor, waitForElementToBeRemoved };
538+
export { screen, waitFor, waitForElementToBeRemoved, within };

projects/testing-library/tests/integration.spec.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const entities = [
8484
},
8585
];
8686

87-
it('renders the table', async () => {
87+
test('renders the table', async () => {
8888
jest.useFakeTimers();
8989

9090
await render(EntitiesComponent, {
@@ -125,10 +125,11 @@ it('renders the table', async () => {
125125
const row = await screen.findByRole('row', {
126126
name: /Entity 2/i,
127127
});
128+
128129
userEvent.click(
129130
await within(row).findByRole('button', {
130131
name: /edit/i,
131132
}),
132133
);
133-
waitFor(() => expect(modalMock.open).toHaveBeenCalledWith('edit entity', 'Entity 2'));
134+
await waitFor(() => expect(modalMock.open).toHaveBeenCalledWith('edit entity', 'Entity 2'));
134135
});

0 commit comments

Comments
 (0)