Skip to content

Commit 5e17e95

Browse files
test: extra tests for ngOnChanges (#342)
1 parent c229778 commit 5e17e95

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

projects/testing-library/tests/change.spec.ts

+30-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChangeDetectionStrategy, Component, Input, OnChanges, SimpleChanges } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component, Input, OnChanges } from '@angular/core';
22
import { render, screen } from '../src/public_api';
33

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

4141
@Component({
4242
selector: 'atl-fixture',
43-
template: ` {{ name }} `,
43+
template: ` {{ propOne }} {{ propTwo }}`,
4444
})
4545
class FixtureWithNgOnChangesComponent implements OnChanges {
46-
@Input() name = 'Sarah';
47-
@Input() nameChanged?: (name: string, isFirstChange: boolean) => void;
48-
49-
ngOnChanges(changes: SimpleChanges) {
50-
if (changes.name && this.nameChanged) {
51-
this.nameChanged(changes.name.currentValue, changes.name.isFirstChange());
52-
}
53-
}
46+
@Input() propOne = 'Init';
47+
@Input() propTwo = '';
48+
49+
// eslint-disable-next-line @angular-eslint/no-empty-lifecycle-method, @typescript-eslint/no-empty-function
50+
ngOnChanges() {}
5451
}
5552

56-
test('will call ngOnChanges on change', async () => {
57-
const nameChanged = jest.fn();
58-
const componentProperties = { nameChanged };
59-
const { change } = await render(FixtureWithNgOnChangesComponent, { componentProperties });
60-
expect(screen.getByText('Sarah')).toBeInTheDocument();
53+
test('calls ngOnChanges on change', async () => {
54+
const componentInputs = { propOne: 'One', propTwo: 'Two' };
55+
const { change, fixture } = await render(FixtureWithNgOnChangesComponent, { componentInputs });
56+
const spy = jest.spyOn(fixture.componentInstance, 'ngOnChanges');
6157

62-
const name = 'Mark';
63-
change({ name });
58+
expect(screen.getByText(`${componentInputs.propOne} ${componentInputs.propTwo}`)).toBeInTheDocument();
6459

65-
expect(screen.getByText(name)).toBeInTheDocument();
66-
expect(nameChanged).toHaveBeenCalledWith(name, false);
60+
const propOne = 'UpdatedOne';
61+
const propTwo = 'UpdatedTwo';
62+
change({ propOne, propTwo });
63+
64+
expect(spy).toHaveBeenCalledTimes(1);
65+
expect(screen.getByText(`${propOne} ${propTwo}`)).toBeInTheDocument();
6766
});
6867

68+
test('does not invoke ngOnChanges on change without props', async () => {
69+
const componentInputs = { propOne: 'One', propTwo: 'Two' };
70+
const { change, fixture } = await render(FixtureWithNgOnChangesComponent, { componentInputs });
71+
const spy = jest.spyOn(fixture.componentInstance, 'ngOnChanges');
72+
73+
expect(screen.getByText(`${componentInputs.propOne} ${componentInputs.propTwo}`)).toBeInTheDocument();
74+
75+
change({});
76+
expect(spy).not.toHaveBeenCalled();
77+
78+
expect(screen.getByText(`${componentInputs.propOne} ${componentInputs.propTwo}`)).toBeInTheDocument();
79+
});
6980
@Component({
7081
changeDetection: ChangeDetectionStrategy.OnPush,
7182
selector: 'atl-fixture',

projects/testing-library/tests/changeInputs.spec.ts

+13
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,19 @@ test('calls ngOnChanges on change', async () => {
6565
expect(screen.getByText(`${propOne} ${propTwo}`)).toBeInTheDocument();
6666
});
6767

68+
test('does not invoke ngOnChanges on change without props', async () => {
69+
const componentInputs = { propOne: 'One', propTwo: 'Two' };
70+
const { changeInput, fixture } = await render(FixtureWithNgOnChangesComponent, { componentInputs });
71+
const spy = jest.spyOn(fixture.componentInstance, 'ngOnChanges');
72+
73+
expect(screen.getByText(`${componentInputs.propOne} ${componentInputs.propTwo}`)).toBeInTheDocument();
74+
75+
changeInput({});
76+
expect(spy).not.toHaveBeenCalled();
77+
78+
expect(screen.getByText(`${componentInputs.propOne} ${componentInputs.propTwo}`)).toBeInTheDocument();
79+
});
80+
6881
@Component({
6982
changeDetection: ChangeDetectionStrategy.OnPush,
7083
selector: 'atl-fixture',

0 commit comments

Comments
 (0)