Skip to content

Commit

Permalink
fix: patch within queries to invoke a change detection cycle (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver authored Nov 17, 2021
1 parent 53a67d7 commit afd0de9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
19 changes: 17 additions & 2 deletions projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ import {
waitFor as dtlWaitFor,
waitForElementToBeRemoved as dtlWaitForElementToBeRemoved,
screen as dtlScreen,
within as dtlWithin,
waitForOptions as dtlWaitForOptions,
configure as dtlConfigure,
Queries,
getQueriesForElement,
queries as dtlQueries,
} from '@testing-library/dom';
import { RenderComponentOptions, RenderDirectiveOptions, RenderTemplateOptions, RenderResult } from './models';
import { getConfig } from './config';
Expand Down Expand Up @@ -432,6 +436,18 @@ function detectChangesForMountedFixtures() {
*/
const screen = replaceFindWithFindAndDetectChanges(dtlScreen);

/**
* Re-export within with patched queries
*/

const within: typeof getQueriesForElement = <T extends Queries = typeof dtlQueries>(
element: HTMLElement,
queriesToBind?: T,
) => {
const container = dtlWithin(element, queriesToBind);
return replaceFindWithFindAndDetectChanges(container);
};

/**
* Re-export waitFor with patched waitFor
*/
Expand Down Expand Up @@ -516,8 +532,7 @@ export {
queryAllByAttribute,
queryByAttribute,
queryHelpers,
within,
} from '@testing-library/dom';

// export patched dtl
export { screen, waitFor, waitForElementToBeRemoved };
export { screen, waitFor, waitForElementToBeRemoved, within };
5 changes: 3 additions & 2 deletions projects/testing-library/tests/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const entities = [
},
];

it('renders the table', async () => {
test('renders the table', async () => {
jest.useFakeTimers();

await render(EntitiesComponent, {
Expand Down Expand Up @@ -125,10 +125,11 @@ it('renders the table', async () => {
const row = await screen.findByRole('row', {
name: /Entity 2/i,
});

userEvent.click(
await within(row).findByRole('button', {
name: /edit/i,
}),
);
waitFor(() => expect(modalMock.open).toHaveBeenCalledWith('edit entity', 'Entity 2'));
await waitFor(() => expect(modalMock.open).toHaveBeenCalledWith('edit entity', 'Entity 2'));
});

0 comments on commit afd0de9

Please sign in to comment.