Skip to content
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

[PM-12252] Autofill name of new ciphers #11127

Merged
merged 6 commits into from
Sep 23, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.servic
import { CipherType } from "@bitwarden/common/vault/enums";
import { ButtonModule, DialogService, MenuModule } from "@bitwarden/components";

import { BrowserApi } from "../../../../../platform/browser/browser-api";
import { AddEditQueryParams } from "../add-edit/add-edit-v2.component";
import { AddEditFolderDialogComponent } from "../add-edit-folder-dialog/add-edit-folder-dialog.component";

Expand All @@ -18,6 +19,10 @@ describe("NewItemDropdownV2Component", () => {
const open = jest.fn();
const navigate = jest.fn();

jest
.spyOn(BrowserApi, "getTabFromCurrentWindow")
.mockResolvedValue({ url: "https://example.com" } as chrome.tabs.Tab);

beforeEach(async () => {
open.mockClear();
navigate.mockClear();
Expand All @@ -44,63 +49,67 @@ describe("NewItemDropdownV2Component", () => {
});

describe("new item", () => {
const emptyParams: AddEditQueryParams = {
const defaultParams: AddEditQueryParams = {
collectionId: undefined,
organizationId: undefined,
folderId: undefined,
name: "example.com",
};

beforeEach(() => {
jest.spyOn(component, "newItemNavigate");
});

it("navigates to new login", () => {
component.newItemNavigate(CipherType.Login);
it("navigates to new login", async () => {
await component.newItemNavigate(CipherType.Login);

expect(navigate).toHaveBeenCalledWith(["/add-cipher"], {
queryParams: { type: CipherType.Login.toString(), ...emptyParams },
queryParams: { type: CipherType.Login.toString(), ...defaultParams },
});
});

it("navigates to new card", () => {
component.newItemNavigate(CipherType.Card);
it("navigates to new card", async () => {
await component.newItemNavigate(CipherType.Card);

expect(navigate).toHaveBeenCalledWith(["/add-cipher"], {
queryParams: { type: CipherType.Card.toString(), ...emptyParams },
queryParams: { type: CipherType.Card.toString(), ...defaultParams },
});
});

it("navigates to new identity", () => {
component.newItemNavigate(CipherType.Identity);
it("navigates to new identity", async () => {
await component.newItemNavigate(CipherType.Identity);

expect(navigate).toHaveBeenCalledWith(["/add-cipher"], {
queryParams: { type: CipherType.Identity.toString(), ...emptyParams },
queryParams: { type: CipherType.Identity.toString(), ...defaultParams },
});
});

it("navigates to new note", () => {
component.newItemNavigate(CipherType.SecureNote);
it("navigates to new note", async () => {
await component.newItemNavigate(CipherType.SecureNote);

expect(navigate).toHaveBeenCalledWith(["/add-cipher"], {
queryParams: { type: CipherType.SecureNote.toString(), ...emptyParams },
queryParams: { type: CipherType.SecureNote.toString(), ...defaultParams },
});
});

it("includes initial values", () => {
it("includes initial values", async () => {
component.initialValues = {
folderId: "222-333-444",
organizationId: "444-555-666",
collectionId: "777-888-999",
uri: "https://www.example.com/login",
} as NewItemInitialValues;

component.newItemNavigate(CipherType.Login);
await component.newItemNavigate(CipherType.Login);

expect(navigate).toHaveBeenCalledWith(["/add-cipher"], {
queryParams: {
type: CipherType.Login.toString(),
folderId: "222-333-444",
organizationId: "444-555-666",
collectionId: "777-888-999",
uri: "https://www.example.com/login",
name: "example.com",
},
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { Component, Input } from "@angular/core";
import { Router, RouterLink } from "@angular/router";

import { JslibModule } from "@bitwarden/angular/jslib.module";
import { Utils } from "@bitwarden/common/platform/misc/utils";
import { CollectionId, OrganizationId } from "@bitwarden/common/types/guid";
import { CipherType } from "@bitwarden/common/vault/enums";
import { ButtonModule, DialogService, MenuModule, NoItemsModule } from "@bitwarden/components";

import { BrowserApi } from "../../../../../platform/browser/browser-api";
import { AddEditQueryParams } from "../add-edit/add-edit-v2.component";
import { AddEditFolderDialogComponent } from "../add-edit-folder-dialog/add-edit-folder-dialog.component";

Expand Down Expand Up @@ -37,18 +39,21 @@ export class NewItemDropdownV2Component {
private dialogService: DialogService,
) {}

private buildQueryParams(type: CipherType): AddEditQueryParams {
private async buildQueryParams(type: CipherType): Promise<AddEditQueryParams> {
const tab = await BrowserApi.getTabFromCurrentWindow();
shane-melton marked this conversation as resolved.
Show resolved Hide resolved

return {
type: type.toString(),
collectionId: this.initialValues?.collectionId,
organizationId: this.initialValues?.organizationId,
folderId: this.initialValues?.folderId,
uri: this.initialValues?.uri,
name: Utils.getHostname(tab.url),
};
}

newItemNavigate(type: CipherType) {
void this.router.navigate(["/add-cipher"], { queryParams: this.buildQueryParams(type) });
async newItemNavigate(type: CipherType) {
await this.router.navigate(["/add-cipher"], { queryParams: await this.buildQueryParams(type) });
}

openFolderDialog() {
Expand Down
Loading