Skip to content

Commit

Permalink
fix: correctly reset special fields like username when cancelling forms
Browse files Browse the repository at this point in the history
closes #2189
  • Loading branch information
sleidig committed Jan 23, 2024
1 parent 240a34d commit f6f5c2a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,36 @@ describe("EntityFormService", () => {
expect(unsavedChanges.pending).toBeFalse();
});

it("should reset form on cancel, including special fields with getter", async () => {
class MockEntity extends Entity {
@DatabaseField() simpleField = "original";

@DatabaseField() get getterField(): string {
return this._getterValue;
}
set getterField(value) {
this._getterValue = value;
}
private _getterValue: string = "original value";

@DatabaseField() emptyField;
}

const formFields = ["simpleField", "getterField", "emptyField"];
const mockEntity = new MockEntity();
const formGroup = service.createFormGroup(formFields, mockEntity);

formGroup.get("simpleField").setValue("new");
formGroup.get("getterField").setValue("new value");
formGroup.get("emptyField").setValue("value");

service.resetForm(formGroup, mockEntity);

expect(formGroup.get("simpleField").value).toBe("original");
expect(formGroup.get("getterField").value).toBe("original value");
expect(formGroup.get("emptyField").value).toBeUndefined();
});

it("should reset state once navigation happens", async () => {
const router = TestBed.inject(Router);
router.resetConfig([{ path: "test", component: NotFoundComponent }]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { EntitySchemaService } from "../../entity/schema/entity-schema.service";
import { DynamicValidatorsService } from "./dynamic-form-validators/dynamic-validators.service";
import { EntityAbility } from "../../permissions/ability/entity-ability";
import { InvalidFormFieldError } from "./invalid-form-field.error";
import { omit } from "lodash-es";
import { UnsavedChangesService } from "../../entity-details/form/unsaved-changes.service";
import { ActivationStart, Router } from "@angular/router";
import { Subscription } from "rxjs";
Expand Down Expand Up @@ -245,11 +244,10 @@ export class EntityFormService {
}

resetForm<E extends Entity>(form: EntityForm<E>, entity: E) {
// Patch form with values from the entity
form.patchValue(entity as any);
// Clear values that are not yet present on the entity
const newKeys = Object.keys(omit(form.controls, Object.keys(entity)));
newKeys.forEach((key) => form.get(key).setValue(null));
for (const key of Object.keys(form.controls)) {
form.get(key).setValue(entity[key]);
}

form.markAsPristine();
this.unsavedChanges.pending = false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/entity-details/form/form.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@ describe("FormComponent", () => {
component.form.get("name").setValue("my name");
component.cancelClicked();

expect(component.form.get("name")).toHaveValue(null);
expect(component.form.get("name").value).toBeUndefined();
});
});

0 comments on commit f6f5c2a

Please sign in to comment.