Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: testing-library/angular-testing-library
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: a0dfa35
Choose a base ref
...
head repository: testing-library/angular-testing-library
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: d5486f1
Choose a head ref
  • 1 commit
  • 3 files changed
  • 1 contributor

Commits on Dec 2, 2023

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    d5486f1 View commit details
Showing with 76 additions and 5 deletions.
  1. +1 −1 projects/testing-library/src/lib/models.ts
  2. +17 −4 projects/testing-library/src/lib/testing-library.ts
  3. +58 −0 projects/testing-library/tests/rerender.spec.ts
2 changes: 1 addition & 1 deletion projects/testing-library/src/lib/models.ts
Original file line number Diff line number Diff line change
@@ -61,7 +61,7 @@ export interface RenderResult<ComponentType, WrapperType = ComponentType> extend
properties?: Pick<
RenderTemplateOptions<ComponentType>,
'componentProperties' | 'componentInputs' | 'componentOutputs' | 'detectChangesOnRender'
>,
> & { partialUpdate?: boolean },
) => Promise<void>;
/**
* @description
21 changes: 17 additions & 4 deletions projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
@@ -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 ?? {};
@@ -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)) {
@@ -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];
}
58 changes: 58 additions & 0 deletions projects/testing-library/tests/rerender.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
@@ -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();