Skip to content

fix: invoke ngOnChanges when initial value is not set #189

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
Mar 18, 2021
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
13 changes: 5 additions & 8 deletions projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export async function render<SutType, WrapperType = SutType>(

// Call ngOnChanges on initial render
if (hasOnChangesHook(fixture.componentInstance)) {
const changes = getChangesObj(null, fixture.componentInstance);
const changes = getChangesObj(null, componentProperties);
fixture.componentInstance.ngOnChanges(changes);
}

Expand Down Expand Up @@ -205,13 +205,13 @@ function setComponentProperties<SutType>(
{ componentProperties = {} }: Pick<RenderDirectiveOptions<SutType, any>, 'componentProperties'>,
) {
for (const key of Object.keys(componentProperties)) {
let _value = componentProperties[key];
let _value = componentProperties[key];
Object.defineProperty(fixture.componentInstance, key, {
get: () => _value ,
get: () => _value,
set: (value) => {
_value = value;
fixture.detectChanges();
}
},
});
}
return fixture;
Expand Down Expand Up @@ -241,10 +241,7 @@ function addAutoDeclarations<SutType>(
excludeComponentDeclaration,
template,
wrapper,
}: Pick<
RenderDirectiveOptions<any>,
'declarations' | 'excludeComponentDeclaration' | 'template' | 'wrapper'
>,
}: Pick<RenderDirectiveOptions<any>, 'declarations' | 'excludeComponentDeclaration' | 'template' | 'wrapper'>,
) {
const wrappers = () => {
return template ? [wrapper] : [];
Expand Down
24 changes: 24 additions & 0 deletions projects/testing-library/tests/issues/188.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// https://github.com/testing-library/angular-testing-library/issues/188
import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';
import { render } from '../../src/public_api';

@Component({
template: `<h1>Hello {{ formattedName }}</h1>`,
})
export class BugOnChangeComponent implements OnChanges {
@Input() name: string;

formattedName: string;

ngOnChanges(changes: SimpleChanges) {
if (changes.name) {
this.formattedName = changes.name.currentValue.toUpperCase();
}
}
}

it('should output formatted name after rendering', async () => {
const { getByText } = await render(BugOnChangeComponent, { componentProperties: { name: 'name' } });

getByText('Hello NAME');
});
8 changes: 4 additions & 4 deletions projects/testing-library/tests/render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('Angular component life-cycle hooks', () => {
template: ` {{ name }} `,
})
class FixtureWithNgOnChangesComponent implements OnInit, OnChanges {
@Input() name = 'Sarah';
@Input() name = 'Initial';
@Input() nameInitialized?: (name: string) => void;
@Input() nameChanged?: (name: string, isFirstChange: boolean) => void;

Expand All @@ -109,14 +109,14 @@ describe('Angular component life-cycle hooks', () => {
const componentProperties = { nameInitialized };
const component = await render(FixtureWithNgOnChangesComponent, { componentProperties });

component.getByText('Sarah');
expect(nameInitialized).toBeCalledWith('Sarah');
component.getByText('Initial');
expect(nameInitialized).toBeCalledWith('Initial');
});

test('will call ngOnChanges on initial render before ngOnInit', async () => {
const nameInitialized = jest.fn();
const nameChanged = jest.fn();
const componentProperties = { nameInitialized, nameChanged };
const componentProperties = { nameInitialized, nameChanged, name: 'Sarah' };
const component = await render(FixtureWithNgOnChangesComponent, { componentProperties });

component.getByText('Sarah');
Expand Down