From 1b442f691c2aea986e371f17b71862f332f13346 Mon Sep 17 00:00:00 2001 From: Nick Krantz Date: Fri, 20 Dec 2024 09:56:52 -0600 Subject: [PATCH] add static open method for folder dialog --- .../new-item-dropdown-v2.component.ts | 2 +- .../popup/settings/folders-v2.component.spec.ts | 11 +++++------ .../src/vault/popup/settings/folders-v2.component.ts | 10 ++-------- .../app/vault/individual-vault/vault.component.ts | 12 ++++-------- .../add-edit-folder-dialog.component.ts | 7 +++++++ 5 files changed, 19 insertions(+), 23 deletions(-) diff --git a/apps/browser/src/vault/popup/components/vault-v2/new-item-dropdown/new-item-dropdown-v2.component.ts b/apps/browser/src/vault/popup/components/vault-v2/new-item-dropdown/new-item-dropdown-v2.component.ts index 0fb5c0e6a21..d6c341082ee 100644 --- a/apps/browser/src/vault/popup/components/vault-v2/new-item-dropdown/new-item-dropdown-v2.component.ts +++ b/apps/browser/src/vault/popup/components/vault-v2/new-item-dropdown/new-item-dropdown-v2.component.ts @@ -64,6 +64,6 @@ export class NewItemDropdownV2Component implements OnInit { } openFolderDialog() { - this.dialogService.open(AddEditFolderDialogComponent); + AddEditFolderDialogComponent.open(this.dialogService); } } diff --git a/apps/browser/src/vault/popup/settings/folders-v2.component.spec.ts b/apps/browser/src/vault/popup/settings/folders-v2.component.spec.ts index 0d1a1740236..41bbfe3265c 100644 --- a/apps/browser/src/vault/popup/settings/folders-v2.component.spec.ts +++ b/apps/browser/src/vault/popup/settings/folders-v2.component.spec.ts @@ -41,7 +41,8 @@ describe("FoldersV2Component", () => { let component: FoldersV2Component; let fixture: ComponentFixture; const folderViews$ = new BehaviorSubject([]); - const open = jest.fn(); + const open = jest.spyOn(AddEditFolderDialogComponent, "open"); + const mockDialogService = { open: jest.fn() }; beforeEach(async () => { open.mockClear(); @@ -64,7 +65,7 @@ describe("FoldersV2Component", () => { imports: [MockPopupHeaderComponent, MockPopupFooterComponent], }, }) - .overrideProvider(DialogService, { useValue: { open } }) + .overrideProvider(DialogService, { useValue: mockDialogService }) .compileComponents(); fixture = TestBed.createComponent(FoldersV2Component); @@ -97,9 +98,7 @@ describe("FoldersV2Component", () => { editButton.triggerEventHandler("click"); - expect(open).toHaveBeenCalledWith(AddEditFolderDialogComponent, { - data: { editFolderConfig: { folder } }, - }); + expect(open).toHaveBeenCalledWith(mockDialogService, { editFolderConfig: { folder } }); }); it("opens add dialog for new folder when there are no folders", () => { @@ -110,6 +109,6 @@ describe("FoldersV2Component", () => { addButton.triggerEventHandler("click"); - expect(open).toHaveBeenCalledWith(AddEditFolderDialogComponent, { data: {} }); + expect(open).toHaveBeenCalledWith(mockDialogService, {}); }); }); diff --git a/apps/browser/src/vault/popup/settings/folders-v2.component.ts b/apps/browser/src/vault/popup/settings/folders-v2.component.ts index a2d5de8fbc8..5e899fa171b 100644 --- a/apps/browser/src/vault/popup/settings/folders-v2.component.ts +++ b/apps/browser/src/vault/popup/settings/folders-v2.component.ts @@ -11,11 +11,7 @@ import { DialogService, IconButtonModule, } from "@bitwarden/components"; -import { - AddEditFolderDialogComponent, - AddEditFolderDialogData, - VaultIcons, -} from "@bitwarden/vault"; +import { AddEditFolderDialogComponent, VaultIcons } from "@bitwarden/vault"; import { ItemGroupComponent } from "../../../../../../libs/components/src/item/item-group.component"; import { ItemModule } from "../../../../../../libs/components/src/item/item.module"; @@ -67,8 +63,6 @@ export class FoldersV2Component { // If a folder is provided, the edit variant should be shown const editFolderConfig = folder ? { folder } : undefined; - this.dialogService.open(AddEditFolderDialogComponent, { - data: { editFolderConfig }, - }); + AddEditFolderDialogComponent.open(this.dialogService, { editFolderConfig }); } } diff --git a/apps/web/src/app/vault/individual-vault/vault.component.ts b/apps/web/src/app/vault/individual-vault/vault.component.ts index fc56d02c732..d600c204247 100644 --- a/apps/web/src/app/vault/individual-vault/vault.component.ts +++ b/apps/web/src/app/vault/individual-vault/vault.component.ts @@ -74,7 +74,6 @@ import { ServiceUtils } from "@bitwarden/common/vault/service-utils"; import { DialogService, Icons, ToastService } from "@bitwarden/components"; import { AddEditFolderDialogComponent, - AddEditFolderDialogData, AddEditFolderDialogResult, CipherFormConfig, CollectionAssignmentResult, @@ -572,16 +571,13 @@ export class VaultComponent implements OnInit, OnDestroy { } addFolder = (): void => { - this.dialogService.open(AddEditFolderDialogComponent); + AddEditFolderDialogComponent.open(this.dialogService); }; editFolder = async (folder: FolderFilter): Promise => { - const dialogRef = this.dialogService.open( - AddEditFolderDialogComponent, - { - data: { editFolderConfig: { folder } }, - }, - ); + const dialogRef = AddEditFolderDialogComponent.open(this.dialogService, { + editFolderConfig: { folder }, + }); const result = await lastValueFrom(dialogRef.closed); diff --git a/libs/vault/src/components/add-edit-folder-dialog/add-edit-folder-dialog.component.ts b/libs/vault/src/components/add-edit-folder-dialog/add-edit-folder-dialog.component.ts index 79e6a575e9b..dc06bc02484 100644 --- a/libs/vault/src/components/add-edit-folder-dialog/add-edit-folder-dialog.component.ts +++ b/libs/vault/src/components/add-edit-folder-dialog/add-edit-folder-dialog.component.ts @@ -163,4 +163,11 @@ export class AddEditFolderDialogComponent implements AfterViewInit, OnInit { private close(result: AddEditFolderDialogResult) { this.dialogRef.close(result); } + + static open(dialogService: DialogService, data?: AddEditFolderDialogData) { + return dialogService.open( + AddEditFolderDialogComponent, + { data }, + ); + } }