1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-22 04:14:04 +00:00

[PM-9190] Use updateFn for patchCipher so that the current CipherView is available for context (#10258)

This commit is contained in:
Shane Melton
2024-07-25 07:50:39 -07:00
committed by GitHub
parent 14f51544c7
commit f4023762a8
13 changed files with 175 additions and 124 deletions

View File

@@ -270,7 +270,11 @@ describe("CustomFieldsComponent", () => {
fieldView.value = "new text value";
fieldView.type = FieldType.Text;
expect(patchCipher).toHaveBeenCalledWith({ fields: [fieldView] });
expect(patchCipher).toHaveBeenCalled();
const patchFn = patchCipher.mock.lastCall[0];
const updatedCipher = patchFn(new CipherView());
expect(updatedCipher.fields).toEqual([fieldView]);
});
it("updates the label", () => {
@@ -281,7 +285,11 @@ describe("CustomFieldsComponent", () => {
fieldView.value = "text value";
fieldView.type = FieldType.Text;
expect(patchCipher).toHaveBeenCalledWith({ fields: [fieldView] });
expect(patchCipher).toHaveBeenCalled();
const patchFn = patchCipher.mock.lastCall[0];
const updatedCipher = patchFn(new CipherView());
expect(updatedCipher.fields).toEqual([fieldView]);
});
});
@@ -295,7 +303,11 @@ describe("CustomFieldsComponent", () => {
it("removes the field", () => {
component.removeField(0);
expect(patchCipher).toHaveBeenCalledWith({ fields: [] });
expect(patchCipher).toHaveBeenCalled();
const patchFn = patchCipher.mock.lastCall[0];
const updatedCipher = patchFn(new CipherView());
expect(updatedCipher.fields).toEqual([]);
});
});
@@ -325,9 +337,12 @@ describe("CustomFieldsComponent", () => {
// Move second field to first
component.drop({ previousIndex: 0, currentIndex: 1 } as CdkDragDrop<HTMLDivElement>);
const latestCallParams = patchCipher.mock.lastCall[0];
expect(patchCipher).toHaveBeenCalled();
const patchFn = patchCipher.mock.lastCall[0];
expect(latestCallParams.fields.map((f: FieldView) => f.name)).toEqual([
const updatedCipher = patchFn(new CipherView());
expect(updatedCipher.fields.map((f: FieldView) => f.name)).toEqual([
"hidden label",
"text label",
"boolean label",
@@ -342,9 +357,12 @@ describe("CustomFieldsComponent", () => {
preventDefault: jest.fn(),
});
const latestCallParams = patchCipher.mock.lastCall[0];
expect(patchCipher).toHaveBeenCalled();
const patchFn = patchCipher.mock.lastCall[0];
expect(latestCallParams.fields.map((f: FieldView) => f.name)).toEqual([
const updatedCipher = patchFn(new CipherView());
expect(updatedCipher.fields.map((f: FieldView) => f.name)).toEqual([
"text label",
"hidden label",
"linked label",
@@ -356,9 +374,12 @@ describe("CustomFieldsComponent", () => {
// Move 2nd item (hidden label) up to 1st
toggleItems[1].triggerEventHandler("keydown", { key: "ArrowUp", preventDefault: jest.fn() });
const latestCallParams = patchCipher.mock.lastCall[0];
expect(patchCipher).toHaveBeenCalled();
const patchFn = patchCipher.mock.lastCall[0];
expect(latestCallParams.fields.map((f: FieldView) => f.name)).toEqual([
const updatedCipher = patchFn(new CipherView());
expect(updatedCipher.fields.map((f: FieldView) => f.name)).toEqual([
"hidden label",
"text label",
"boolean label",

View File

@@ -8,11 +8,11 @@ import {
DestroyRef,
ElementRef,
EventEmitter,
inject,
OnInit,
Output,
QueryList,
ViewChildren,
inject,
} from "@angular/core";
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
import { FormArray, FormBuilder, FormsModule, ReactiveFormsModule } from "@angular/forms";
@@ -26,16 +26,16 @@ import { FieldView } from "@bitwarden/common/vault/models/view/field.view";
import { IdentityView } from "@bitwarden/common/vault/models/view/identity.view";
import { LoginView } from "@bitwarden/common/vault/models/view/login.view";
import {
CardComponent,
CheckboxModule,
DialogService,
FormFieldModule,
IconButtonModule,
LinkModule,
SectionComponent,
SectionHeaderComponent,
FormFieldModule,
TypographyModule,
CardComponent,
IconButtonModule,
CheckboxModule,
SelectModule,
LinkModule,
TypographyModule,
} from "@bitwarden/components";
import { CipherFormContainer } from "../../cipher-form-container";
@@ -344,8 +344,9 @@ export class CustomFieldsComponent implements OnInit, AfterViewInit {
this.numberOfFieldsChange.emit(newFields.length);
this.cipherFormContainer.patchCipher({
fields: newFields,
this.cipherFormContainer.patchCipher((cipher) => {
cipher.fields = newFields;
return cipher;
});
}
}