Skip to content

test: add tests for ngOnChanges #342

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
Dec 10, 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
49 changes: 30 additions & 19 deletions projects/testing-library/tests/change.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChangeDetectionStrategy, Component, Input, OnChanges, SimpleChanges } from '@angular/core';
import { ChangeDetectionStrategy, Component, Input, OnChanges } from '@angular/core';
import { render, screen } from '../src/public_api';

@Component({
Expand Down Expand Up @@ -40,32 +40,43 @@ test('changes the component with updated props while keeping other props untouch

@Component({
selector: 'atl-fixture',
template: ` {{ name }} `,
template: ` {{ propOne }} {{ propTwo }}`,
})
class FixtureWithNgOnChangesComponent implements OnChanges {
@Input() name = 'Sarah';
@Input() nameChanged?: (name: string, isFirstChange: boolean) => void;

ngOnChanges(changes: SimpleChanges) {
if (changes.name && this.nameChanged) {
this.nameChanged(changes.name.currentValue, changes.name.isFirstChange());
}
}
@Input() propOne = 'Init';
@Input() propTwo = '';

// eslint-disable-next-line @angular-eslint/no-empty-lifecycle-method, @typescript-eslint/no-empty-function
ngOnChanges() {}
}

test('will call ngOnChanges on change', async () => {
const nameChanged = jest.fn();
const componentProperties = { nameChanged };
const { change } = await render(FixtureWithNgOnChangesComponent, { componentProperties });
expect(screen.getByText('Sarah')).toBeInTheDocument();
test('calls ngOnChanges on change', async () => {
const componentInputs = { propOne: 'One', propTwo: 'Two' };
const { change, fixture } = await render(FixtureWithNgOnChangesComponent, { componentInputs });
const spy = jest.spyOn(fixture.componentInstance, 'ngOnChanges');

const name = 'Mark';
change({ name });
expect(screen.getByText(`${componentInputs.propOne} ${componentInputs.propTwo}`)).toBeInTheDocument();

expect(screen.getByText(name)).toBeInTheDocument();
expect(nameChanged).toHaveBeenCalledWith(name, false);
const propOne = 'UpdatedOne';
const propTwo = 'UpdatedTwo';
change({ propOne, propTwo });

expect(spy).toHaveBeenCalledTimes(1);
expect(screen.getByText(`${propOne} ${propTwo}`)).toBeInTheDocument();
});

test('does not invoke ngOnChanges on change without props', async () => {
const componentInputs = { propOne: 'One', propTwo: 'Two' };
const { change, fixture } = await render(FixtureWithNgOnChangesComponent, { componentInputs });
const spy = jest.spyOn(fixture.componentInstance, 'ngOnChanges');

expect(screen.getByText(`${componentInputs.propOne} ${componentInputs.propTwo}`)).toBeInTheDocument();

change({});
expect(spy).not.toHaveBeenCalled();

expect(screen.getByText(`${componentInputs.propOne} ${componentInputs.propTwo}`)).toBeInTheDocument();
});
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: 'atl-fixture',
Expand Down
13 changes: 13 additions & 0 deletions projects/testing-library/tests/changeInputs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ test('calls ngOnChanges on change', async () => {
expect(screen.getByText(`${propOne} ${propTwo}`)).toBeInTheDocument();
});

test('does not invoke ngOnChanges on change without props', async () => {
const componentInputs = { propOne: 'One', propTwo: 'Two' };
const { changeInput, fixture } = await render(FixtureWithNgOnChangesComponent, { componentInputs });
const spy = jest.spyOn(fixture.componentInstance, 'ngOnChanges');

expect(screen.getByText(`${componentInputs.propOne} ${componentInputs.propTwo}`)).toBeInTheDocument();

changeInput({});
expect(spy).not.toHaveBeenCalled();

expect(screen.getByText(`${componentInputs.propOne} ${componentInputs.propTwo}`)).toBeInTheDocument();
});

@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: 'atl-fixture',
Expand Down