Skip to content

Commit

Permalink
test: extra tests for ngOnChanges (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver authored Dec 10, 2022
1 parent c229778 commit 5e17e95
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
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

0 comments on commit 5e17e95

Please sign in to comment.