Skip to content

fix: don't invoke ngOnChanges when no properties are provided #326

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 22, 2022
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
2 changes: 1 addition & 1 deletion projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export async function render<SutType, WrapperType = SutType>(
let isAlive = true;
fixture.componentRef.onDestroy(() => (isAlive = false));

if (hasOnChangesHook(fixture.componentInstance)) {
if (hasOnChangesHook(fixture.componentInstance) && Object.keys(properties).length > 0) {
const changes = getChangesObj(null, componentProperties);
fixture.componentInstance.ngOnChanges(changes);
}
Expand Down
39 changes: 26 additions & 13 deletions projects/testing-library/tests/render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ import { render, fireEvent, screen } from '../src/public_api';
})
class FixtureComponent {}

test('creates queries and events', async () => {
const view = await render(FixtureComponent);
describe('DTL functionality', () => {
it('creates queries and events', async () => {
const view = await render(FixtureComponent);

/// We wish to test the utility function from `render` here.
// eslint-disable-next-line testing-library/prefer-screen-queries
fireEvent.input(view.getByTestId('input'), { target: { value: 'a super awesome input' } });
// eslint-disable-next-line testing-library/prefer-screen-queries
expect(view.getByDisplayValue('a super awesome input')).toBeInTheDocument();
// eslint-disable-next-line testing-library/prefer-screen-queries
fireEvent.click(view.getByText('button'));
/// We wish to test the utility function from `render` here.
// eslint-disable-next-line testing-library/prefer-screen-queries
fireEvent.input(view.getByTestId('input'), { target: { value: 'a super awesome input' } });
// eslint-disable-next-line testing-library/prefer-screen-queries
expect(view.getByDisplayValue('a super awesome input')).toBeInTheDocument();
// eslint-disable-next-line testing-library/prefer-screen-queries
fireEvent.click(view.getByText('button'));
});
});

describe('standalone', () => {
Expand Down Expand Up @@ -162,13 +164,13 @@ describe('Angular component life-cycle hooks', () => {
}

ngOnChanges(changes: SimpleChanges) {
if (changes.name && this.nameChanged) {
this.nameChanged(changes.name.currentValue, changes.name.isFirstChange());
if (this.nameChanged) {
this.nameChanged(changes.name?.currentValue, changes.name?.isFirstChange());
}
}
}

it('will call ngOnInit on initial render', async () => {
it('invokes ngOnInit on initial render', async () => {
const nameInitialized = jest.fn();
const componentProperties = { nameInitialized };
const view = await render(FixtureWithNgOnChangesComponent, { componentProperties });
Expand All @@ -179,7 +181,7 @@ describe('Angular component life-cycle hooks', () => {
expect(nameInitialized).toHaveBeenCalledWith('Initial');
});

it('will call ngOnChanges on initial render before ngOnInit', async () => {
it('invokes ngOnChanges on initial render before ngOnInit', async () => {
const nameInitialized = jest.fn();
const nameChanged = jest.fn();
const componentProperties = { nameInitialized, nameChanged, name: 'Sarah' };
Expand All @@ -193,6 +195,17 @@ describe('Angular component life-cycle hooks', () => {
/// expect `nameChanged` to be called before `nameInitialized`
expect(nameChanged.mock.invocationCallOrder[0]).toBeLessThan(nameInitialized.mock.invocationCallOrder[0]);
});

it('does not invoke ngOnChanges when no properties are provided', async () => {
@Component({ template: `` })
class TestFixtureComponent implements OnChanges {
ngOnChanges() {
throw new Error('should not be called');
}
}

await render(TestFixtureComponent);
});
});

test('waits for angular app initialization before rendering components', async () => {
Expand Down