mirror of
https://github.com/bitwarden/browser
synced 2025-12-10 13:23:34 +00:00
force a null value for angular forms as undefined gets forced to null anyway (#16985)
This commit is contained in:
@@ -640,6 +640,46 @@ describe("ItemDetailsSectionComponent", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("initFromExistingCipher", () => {
|
||||||
|
it("should set organizationId to null when prefillCipher.organizationId is undefined", async () => {
|
||||||
|
component.config.organizationDataOwnershipDisabled = true;
|
||||||
|
component.config.organizations = [{ id: "org1" } as Organization];
|
||||||
|
|
||||||
|
const prefillCipher = {
|
||||||
|
name: "Test Cipher",
|
||||||
|
organizationId: undefined,
|
||||||
|
folderId: null,
|
||||||
|
collectionIds: [],
|
||||||
|
favorite: false,
|
||||||
|
} as unknown as CipherView;
|
||||||
|
|
||||||
|
getInitialCipherView.mockReturnValueOnce(prefillCipher);
|
||||||
|
|
||||||
|
await component.ngOnInit();
|
||||||
|
|
||||||
|
expect(component.itemDetailsForm.controls.organizationId.value).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should preserve organizationId when prefillCipher.organizationId has a value", async () => {
|
||||||
|
component.config.organizationDataOwnershipDisabled = true;
|
||||||
|
component.config.organizations = [{ id: "org1", name: "Organization 1" } as Organization];
|
||||||
|
|
||||||
|
const prefillCipher = {
|
||||||
|
name: "Test Cipher",
|
||||||
|
organizationId: "org1",
|
||||||
|
folderId: null,
|
||||||
|
collectionIds: [],
|
||||||
|
favorite: false,
|
||||||
|
} as unknown as CipherView;
|
||||||
|
|
||||||
|
getInitialCipherView.mockReturnValueOnce(prefillCipher);
|
||||||
|
|
||||||
|
await component.ngOnInit();
|
||||||
|
|
||||||
|
expect(component.itemDetailsForm.controls.organizationId.value).toBe("org1");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("form status when editing a cipher", () => {
|
describe("form status when editing a cipher", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
component.config.mode = "edit";
|
component.config.mode = "edit";
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { CommonModule } from "@angular/common";
|
|||||||
import { Component, DestroyRef, Input, OnInit } from "@angular/core";
|
import { Component, DestroyRef, Input, OnInit } from "@angular/core";
|
||||||
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
|
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
|
||||||
import { FormBuilder, FormControl, ReactiveFormsModule, Validators } from "@angular/forms";
|
import { FormBuilder, FormControl, ReactiveFormsModule, Validators } from "@angular/forms";
|
||||||
import { concatMap, firstValueFrom, map } from "rxjs";
|
import { concatMap, distinctUntilChanged, firstValueFrom, map } from "rxjs";
|
||||||
|
|
||||||
// This import has been flagged as unallowed for this class. It may be involved in a circular dependency loop.
|
// This import has been flagged as unallowed for this class. It may be involved in a circular dependency loop.
|
||||||
// eslint-disable-next-line no-restricted-imports
|
// eslint-disable-next-line no-restricted-imports
|
||||||
@@ -236,6 +236,7 @@ export class ItemDetailsSectionComponent implements OnInit {
|
|||||||
this.itemDetailsForm.controls.organizationId.valueChanges
|
this.itemDetailsForm.controls.organizationId.valueChanges
|
||||||
.pipe(
|
.pipe(
|
||||||
takeUntilDestroyed(this.destroyRef),
|
takeUntilDestroyed(this.destroyRef),
|
||||||
|
distinctUntilChanged(),
|
||||||
concatMap(async () => {
|
concatMap(async () => {
|
||||||
await this.updateCollectionOptions();
|
await this.updateCollectionOptions();
|
||||||
this.setFormState();
|
this.setFormState();
|
||||||
@@ -314,7 +315,10 @@ export class ItemDetailsSectionComponent implements OnInit {
|
|||||||
|
|
||||||
this.itemDetailsForm.patchValue({
|
this.itemDetailsForm.patchValue({
|
||||||
name: name ? name : (this.initialValues?.name ?? ""),
|
name: name ? name : (this.initialValues?.name ?? ""),
|
||||||
organizationId: prefillCipher.organizationId, // We do not allow changing ownership of an existing cipher.
|
// We do not allow changing ownership of an existing cipher.
|
||||||
|
// Angular forms do not support `undefined` as a value for a form control,
|
||||||
|
// force `null` if `organizationId` is undefined.
|
||||||
|
organizationId: prefillCipher.organizationId ?? null,
|
||||||
folderId: folderId ? folderId : (this.initialValues?.folderId ?? null),
|
folderId: folderId ? folderId : (this.initialValues?.folderId ?? null),
|
||||||
collectionIds: [],
|
collectionIds: [],
|
||||||
favorite: prefillCipher.favorite,
|
favorite: prefillCipher.favorite,
|
||||||
|
|||||||
Reference in New Issue
Block a user