Skip to content

feat: add partialUpdate to rerender #427

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
Dec 2, 2023
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
2 changes: 1 addition & 1 deletion projects/testing-library/src/lib/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export interface RenderResult<ComponentType, WrapperType = ComponentType> extend
properties?: Pick<
RenderTemplateOptions<ComponentType>,
'componentProperties' | 'componentInputs' | 'componentOutputs' | 'detectChangesOnRender'
>,
> & { partialUpdate?: boolean },
) => Promise<void>;
/**
* @description
Expand Down
21 changes: 17 additions & 4 deletions projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,16 @@ export async function render<SutType, WrapperType = SutType>(
properties?: Pick<
RenderTemplateOptions<SutType>,
'componentProperties' | 'componentInputs' | 'componentOutputs' | 'detectChangesOnRender'
>,
> & { partialUpdate?: boolean },
) => {
const newComponentInputs = properties?.componentInputs ?? {};
const changesInComponentInput = update(fixture, renderedInputKeys, newComponentInputs, setComponentInputs);
const changesInComponentInput = update(
fixture,
renderedInputKeys,
newComponentInputs,
setComponentInputs,
properties?.partialUpdate ?? false,
);
renderedInputKeys = Object.keys(newComponentInputs);

const newComponentOutputs = properties?.componentOutputs ?? {};
Expand All @@ -198,7 +204,13 @@ export async function render<SutType, WrapperType = SutType>(
renderedOutputKeys = Object.keys(newComponentOutputs);

const newComponentProps = properties?.componentProperties ?? {};
const changesInComponentProps = update(fixture, renderedPropKeys, newComponentProps, setComponentProperties);
const changesInComponentProps = update(
fixture,
renderedPropKeys,
newComponentProps,
setComponentProperties,
properties?.partialUpdate ?? false,
);
renderedPropKeys = Object.keys(newComponentProps);

if (hasOnChangesHook(fixture.componentInstance)) {
Expand Down Expand Up @@ -387,12 +399,13 @@ function update<SutType>(
fixture: ComponentFixture<SutType>,
values: RenderTemplateOptions<SutType>['componentInputs' | 'componentProperties'],
) => void,
partialUpdate: boolean,
) {
const componentInstance = fixture.componentInstance as Record<string, any>;
const simpleChanges: SimpleChanges = {};

for (const key of prevRenderedKeys) {
if (!Object.prototype.hasOwnProperty.call(newValues, key)) {
if (!partialUpdate && !Object.prototype.hasOwnProperty.call(newValues, key)) {
simpleChanges[key] = new SimpleChange(componentInstance[key], undefined, false);
delete componentInstance[key];
}
Expand Down
58 changes: 58 additions & 0 deletions projects/testing-library/tests/rerender.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,35 @@ test('rerenders the component with updated inputs and resets other props', async
});
});

test('rerenders the component with updated inputs and keeps other props when partial is true', async () => {
const firstName = 'Mark';
const lastName = 'Peeters';
const { rerender } = await render(FixtureComponent, {
componentInputs: {
firstName,
lastName,
},
});

expect(screen.getByText(`${firstName} ${lastName}`)).toBeInTheDocument();

const firstName2 = 'Chris';
await rerender({ componentInputs: { firstName: firstName2 }, partialUpdate: true });

expect(screen.queryByText(firstName)).not.toBeInTheDocument();
expect(screen.getByText(`${firstName2} ${lastName}`)).toBeInTheDocument();

expect(ngOnChangesSpy).toHaveBeenCalledTimes(2); // one time initially and one time for rerender
const rerenderedChanges = ngOnChangesSpy.mock.calls[1][0] as SimpleChanges;
expect(rerenderedChanges).toEqual({
firstName: {
previousValue: 'Mark',
currentValue: 'Chris',
firstChange: false,
},
});
});

test('rerenders the component with updated props and resets other props with componentProperties', async () => {
const firstName = 'Mark';
const lastName = 'Peeters';
Expand Down Expand Up @@ -118,6 +147,35 @@ test('rerenders the component with updated props and resets other props with com
});
});

test('rerenders the component with updated props keeps other props when partial is true', async () => {
const firstName = 'Mark';
const lastName = 'Peeters';
const { rerender } = await render(FixtureComponent, {
componentProperties: {
firstName,
lastName,
},
});

expect(screen.getByText(`${firstName} ${lastName}`)).toBeInTheDocument();

const firstName2 = 'Chris';
await rerender({ componentProperties: { firstName: firstName2 }, partialUpdate: true });

expect(screen.queryByText(firstName)).not.toBeInTheDocument();
expect(screen.getByText(`${firstName2} ${lastName}`)).toBeInTheDocument();

expect(ngOnChangesSpy).toHaveBeenCalledTimes(2); // one time initially and one time for rerender
const rerenderedChanges = ngOnChangesSpy.mock.calls[1][0] as SimpleChanges;
expect(rerenderedChanges).toEqual({
firstName: {
previousValue: 'Mark',
currentValue: 'Chris',
firstChange: false,
},
});
});

test('change detection gets not called if `detectChangesOnRender` is set to false', async () => {
const { rerender } = await render(FixtureComponent);
expect(screen.getByText('Sarah')).toBeInTheDocument();
Expand Down