Skip to content

fix: patch within queries to invoke a change detection cycle #259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'));
});