Skip to content

Commit c9dbed6

Browse files
fix: calculate changes for input properties like component properties
ref testing-library#365
1 parent 9666dce commit c9dbed6

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

projects/testing-library/src/lib/testing-library.ts

+20-16
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,7 @@ export async function render<SutType, WrapperType = SutType>(
133133
>,
134134
) => {
135135
const newComponentInputs = properties?.componentInputs ?? {};
136-
for (const inputKey of renderedInputKeys) {
137-
if (!Object.prototype.hasOwnProperty.call(newComponentInputs, inputKey)) {
138-
delete (fixture.componentInstance as any)[inputKey];
139-
}
140-
}
141-
setComponentInputs(fixture, newComponentInputs);
136+
const changesInComponentInput = update(fixture, renderedInputKeys, newComponentInputs, setComponentInputs);
142137
renderedInputKeys = Object.keys(newComponentInputs);
143138

144139
const newComponentOutputs = properties?.componentOutputs ?? {};
@@ -151,11 +146,15 @@ export async function render<SutType, WrapperType = SutType>(
151146
renderedOutputKeys = Object.keys(newComponentOutputs);
152147

153148
const newComponentProps = properties?.componentProperties ?? {};
154-
const changes = updateProps(fixture, renderedPropKeys, newComponentProps);
149+
const changesInComponentProps = update(fixture, renderedPropKeys, newComponentProps, setComponentProperties);
150+
renderedPropKeys = Object.keys(newComponentProps);
151+
155152
if (hasOnChangesHook(fixture.componentInstance)) {
156-
fixture.componentInstance.ngOnChanges(changes);
153+
fixture.componentInstance.ngOnChanges({
154+
...changesInComponentInput,
155+
...changesInComponentProps,
156+
});
157157
}
158-
renderedPropKeys = Object.keys(newComponentProps);
159158

160159
if (properties?.detectChangesOnRender !== false) {
161160
fixture.componentRef.injector.get(ChangeDetectorRef).detectChanges();
@@ -361,27 +360,32 @@ function getChangesObj(oldProps: Record<string, any> | null, newProps: Record<st
361360
);
362361
}
363362

364-
function updateProps<SutType>(
363+
function update<SutType>(
365364
fixture: ComponentFixture<SutType>,
366-
prevRenderedPropsKeys: string[],
367-
newProps: Record<string, any>,
365+
prevRenderedKeys: string[],
366+
newValues: Record<string, any>,
367+
updateFunction: (
368+
fixture: ComponentFixture<SutType>,
369+
values: RenderTemplateOptions<SutType>['componentInputs' | 'componentProperties'],
370+
) => void,
368371
) {
369372
const componentInstance = fixture.componentInstance as Record<string, any>;
370373
const simpleChanges: SimpleChanges = {};
371374

372-
for (const key of prevRenderedPropsKeys) {
373-
if (!Object.prototype.hasOwnProperty.call(newProps, key)) {
375+
for (const key of prevRenderedKeys) {
376+
if (!Object.prototype.hasOwnProperty.call(newValues, key)) {
374377
simpleChanges[key] = new SimpleChange(componentInstance[key], undefined, false);
375378
delete componentInstance[key];
376379
}
377380
}
378381

379-
for (const [key, value] of Object.entries(newProps)) {
382+
for (const [key, value] of Object.entries(newValues)) {
380383
if (value !== componentInstance[key]) {
381384
simpleChanges[key] = new SimpleChange(componentInstance[key], value, false);
382385
}
383386
}
384-
setComponentProperties(fixture, newProps);
387+
388+
updateFunction(fixture, newValues);
385389

386390
return simpleChanges;
387391
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ test('rerenders the component with updated inputs', async () => {
4848
expect(screen.getByText(firstName)).toBeInTheDocument();
4949
});
5050

51-
test('rerenders the component with updated props and resets other props', async () => {
51+
test('rerenders the component with updated inputs and resets other props', async () => {
5252
const firstName = 'Mark';
5353
const lastName = 'Peeters';
5454
const { rerender } = await render(FixtureComponent, {

0 commit comments

Comments
 (0)