Skip to content

fix: infinite loop in waitFor when DOM is mutating #231

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 2 commits into from
Jun 29, 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test('should scroll to load more items', async () => {
expect(item0).toBeVisible();

screen.getByTestId('scroll-viewport').scrollTop = 500;
await waitForElementToBeRemoved(() => screen.getByText(/Item #0/i));
await waitForElementToBeRemoved(() => screen.queryByText(/Item #0/i));

const item12 = await screen.findByText(/Item #12/i);
expect(item12).toBeVisible();
Expand Down
15 changes: 11 additions & 4 deletions projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,16 +303,21 @@ function addAutoImports({ imports, routes }: Pick<RenderComponentOptions<any>, '
}

/**
* Wrap waitFor to poke the Angular change detection cycle before invoking the callback
* Wrap waitFor to invoke the Angular change detection cycle before invoking the callback
*/
async function waitForWrapper<T>(
detectChanges: () => void,
callback: () => T extends Promise<any> ? never : T,
options?: dtlWaitForOptions,
): Promise<T> {
detectChanges();
return await dtlWaitFor(() => {
detectChanges();
return callback();
try {
return callback();
} catch (error) {
setImmediate(() => detectChanges());
throw error;
}
}, options);
}

Expand Down Expand Up @@ -340,8 +345,10 @@ async function waitForElementToBeRemovedWrapper<T>(
}

return await dtlWaitForElementToBeRemoved(() => {
const result = cb();
detectChanges();
return cb();
setImmediate(() => detectChanges());
return result;
}, options);
}

Expand Down
29 changes: 29 additions & 0 deletions projects/testing-library/tests/issues/issue-230.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Component } from '@angular/core';
import { render, waitFor, screen } from '@testing-library/angular';

@Component({
template: ` <button [ngClass]="classes">Load</button> `,
})
class LoopComponent {
get classes() {
return {
someClass: true,
};
}
}

test('wait does not end up in a loop', async () => {
await render(LoopComponent);

await expect(
waitFor(() => {
expect(true).toEqual(false);
}),
).rejects.toThrow();
});

test('find does not end up in a loop', async () => {
await render(LoopComponent);

await expect(screen.findByText('foo')).rejects.toThrow();
});
6 changes: 3 additions & 3 deletions projects/testing-library/tests/wait-for.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component } from '@angular/core';
import { timer } from 'rxjs';
import { render, screen, fireEvent, waitFor as waitForATL } from '../src/public_api';
import { render, screen, fireEvent, waitFor } from '../src/public_api';

@Component({
selector: 'atl-fixture',
Expand All @@ -24,7 +24,7 @@ test('waits for assertion to become true', async () => {

fireEvent.click(screen.getByTestId('button'));

await waitForATL(() => screen.getByText('Success'));
await waitFor(() => screen.getByText('Success'));
screen.getByText('Success');
});

Expand All @@ -33,7 +33,7 @@ test('allows to override options', async () => {

fireEvent.click(screen.getByTestId('button'));

await expect(waitForATL(() => screen.getByText('Success'), { timeout: 200 })).rejects.toThrow(
await expect(waitFor(() => screen.getByText('Success'), { timeout: 200 })).rejects.toThrow(
/Unable to find an element with the text: Success/i,
);
});