Skip to content

Commit

Permalink
feat: only check for own detectChangesOnRender flag
Browse files Browse the repository at this point in the history
ref #365
  • Loading branch information
shaman-apprentice committed Mar 10, 2023
1 parent 4c5a255 commit d218907
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 37 deletions.
5 changes: 1 addition & 4 deletions projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,7 @@ export async function render<SutType, WrapperType = SutType>(
}
renderedPropKeys = Object.keys(newComponentProps);

if (
properties?.detectChangesOnRender === true ||
(properties?.detectChangesOnRender === undefined && detectChangesOnRender === true)
) {
if (properties?.detectChangesOnRender !== false) {
fixture.componentRef.injector.get(ChangeDetectorRef).detectChanges();
}
};
Expand Down
40 changes: 7 additions & 33 deletions projects/testing-library/tests/rerender.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,39 +82,13 @@ test('rerenders the component with updated props and resets other props', async
});
});

describe('detectChangesOnRender', () => {
test('change detection gets not called if disabled within render', async () => {
const { rerender, detectChanges } = await render(FixtureComponent, { detectChangesOnRender: false });
detectChanges();
expect(screen.getByText('Sarah')).toBeInTheDocument();

const firstName = 'Mark';
await rerender({ componentInputs: { firstName } });

expect(screen.getByText('Sarah')).toBeInTheDocument();
expect(screen.queryByText(firstName)).not.toBeInTheDocument();
});

test('change detection gets called if disabled within render but enabled within rerender', async () => {
const { rerender, detectChanges } = await render(FixtureComponent, { detectChangesOnRender: false });
detectChanges();
expect(screen.getByText('Sarah')).toBeInTheDocument();

const firstName = 'Mark';
await rerender({ componentInputs: { firstName }, detectChangesOnRender: true });

expect(screen.getByText(firstName)).toBeInTheDocument();
expect(screen.queryByText('Sarah')).not.toBeInTheDocument();
});

test('change detection gets not called if disabled within rerender', async () => {
const { rerender } = await render(FixtureComponent);
expect(screen.getByText('Sarah')).toBeInTheDocument();
test('change detection gets not called if `detectChangesOnRender` is set to false', async () => {
const { rerender } = await render(FixtureComponent);
expect(screen.getByText('Sarah')).toBeInTheDocument();

const firstName = 'Mark';
await rerender({ componentInputs: { firstName }, detectChangesOnRender: false });
const firstName = 'Mark';
await rerender({ componentInputs: { firstName }, detectChangesOnRender: false });

expect(screen.getByText('Sarah')).toBeInTheDocument();
expect(screen.queryByText(firstName)).not.toBeInTheDocument();
});
expect(screen.getByText('Sarah')).toBeInTheDocument();
expect(screen.queryByText(firstName)).not.toBeInTheDocument();
});

0 comments on commit d218907

Please sign in to comment.