|
1 |
| -import { ChangeDetectionStrategy, Component, Input, OnChanges, SimpleChanges } from '@angular/core'; |
| 1 | +import { ChangeDetectionStrategy, Component, Input, OnChanges } from '@angular/core'; |
2 | 2 | import { render, screen } from '../src/public_api';
|
3 | 3 |
|
4 | 4 | @Component({
|
@@ -40,32 +40,43 @@ test('changes the component with updated props while keeping other props untouch
|
40 | 40 |
|
41 | 41 | @Component({
|
42 | 42 | selector: 'atl-fixture',
|
43 |
| - template: ` {{ name }} `, |
| 43 | + template: ` {{ propOne }} {{ propTwo }}`, |
44 | 44 | })
|
45 | 45 | 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() {} |
54 | 51 | }
|
55 | 52 |
|
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'); |
61 | 57 |
|
62 |
| - const name = 'Mark'; |
63 |
| - change({ name }); |
| 58 | + expect(screen.getByText(`${componentInputs.propOne} ${componentInputs.propTwo}`)).toBeInTheDocument(); |
64 | 59 |
|
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(); |
67 | 66 | });
|
68 | 67 |
|
| 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 | +}); |
69 | 80 | @Component({
|
70 | 81 | changeDetection: ChangeDetectionStrategy.OnPush,
|
71 | 82 | selector: 'atl-fixture',
|
|
0 commit comments