Skip to content

feat: remove change and changeInput in favor of rerender #378

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 2 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fix: calculate changes for input properties like component properties (
  • Loading branch information
shaman-apprentice authored Apr 3, 2023
commit 33d0069ed2829149402dbb1122224e3cdff18079
36 changes: 20 additions & 16 deletions projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,7 @@ export async function render<SutType, WrapperType = SutType>(
>,
) => {
const newComponentInputs = properties?.componentInputs ?? {};
for (const inputKey of renderedInputKeys) {
if (!Object.prototype.hasOwnProperty.call(newComponentInputs, inputKey)) {
delete (fixture.componentInstance as any)[inputKey];
}
}
setComponentInputs(fixture, newComponentInputs);
const changesInComponentInput = update(fixture, renderedInputKeys, newComponentInputs, setComponentInputs);
renderedInputKeys = Object.keys(newComponentInputs);

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

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

if (hasOnChangesHook(fixture.componentInstance)) {
fixture.componentInstance.ngOnChanges(changes);
fixture.componentInstance.ngOnChanges({
...changesInComponentInput,
...changesInComponentProps,
});
}
renderedPropKeys = Object.keys(newComponentProps);

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

function updateProps<SutType>(
function update<SutType>(
fixture: ComponentFixture<SutType>,
prevRenderedPropsKeys: string[],
newProps: Record<string, any>,
prevRenderedKeys: string[],
newValues: Record<string, any>,
updateFunction: (
fixture: ComponentFixture<SutType>,
values: RenderTemplateOptions<SutType>['componentInputs' | 'componentProperties'],
) => void,
) {
const componentInstance = fixture.componentInstance as Record<string, any>;
const simpleChanges: SimpleChanges = {};

for (const key of prevRenderedPropsKeys) {
if (!Object.prototype.hasOwnProperty.call(newProps, key)) {
for (const key of prevRenderedKeys) {
if (!Object.prototype.hasOwnProperty.call(newValues, key)) {
simpleChanges[key] = new SimpleChange(componentInstance[key], undefined, false);
delete componentInstance[key];
}
}

for (const [key, value] of Object.entries(newProps)) {
for (const [key, value] of Object.entries(newValues)) {
if (value !== componentInstance[key]) {
simpleChanges[key] = new SimpleChange(componentInstance[key], value, false);
}
}
setComponentProperties(fixture, newProps);

updateFunction(fixture, newValues);

return simpleChanges;
}
Expand Down
2 changes: 1 addition & 1 deletion projects/testing-library/tests/rerender.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ test('rerenders the component with updated inputs', async () => {
expect(screen.getByText(firstName)).toBeInTheDocument();
});

test('rerenders the component with updated props and resets other props', async () => {
test('rerenders the component with updated inputs and resets other props', async () => {
const firstName = 'Mark';
const lastName = 'Peeters';
const { rerender } = await render(FixtureComponent, {
Expand Down