1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-16 16:23:44 +00:00
Files
browser/libs/components/src/copy-click/copy-click.directive.spec.ts
Shane Melton e8f53fe9b7 [PM-22756] Send minimizeOnCopy message during copy on Desktop platform (#15232)
* [PM-22756] Send minimizeOnCopy message during copy on Desktop platform

* [PM-22756] Introduce optional CopyClickListener pattern

* [PM-22756] Introduce CopyService that wraps PlatformUtilsService.copyToClipboard to allow scoped implementations

* [PM-22756] Introduce DesktopVaultCopyService that sends the minimizeOnCopy message

* [PM-22756] Remove leftover onCopy method

* [PM-22756] Fix failing tests

* [PM-22756] Revert CopyService solution

* [PM-22756] Cleanup

* [PM-22756] Update test

* [PM-22756] Cleanup leftover test changes
2025-06-18 14:44:21 -07:00

132 lines
4.4 KiB
TypeScript

import { Component, ElementRef, ViewChild } from "@angular/core";
import { ComponentFixture, TestBed } from "@angular/core/testing";
import { mock } from "jest-mock-extended";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { ToastService, CopyClickListener, COPY_CLICK_LISTENER } from "../";
import { CopyClickDirective } from "./copy-click.directive";
@Component({
template: `
<button type="button" appCopyClick="no toast shown" #noToast></button>
<button type="button" appCopyClick="info toast shown" showToast="info" #infoToast></button>
<button type="button" appCopyClick="success toast shown" showToast #successToast></button>
<button
type="button"
appCopyClick="toast with label"
showToast
valueLabel="Content"
#toastWithLabel
></button>
`,
imports: [CopyClickDirective],
})
class TestCopyClickComponent {
@ViewChild("noToast") noToastButton!: ElementRef<HTMLButtonElement>;
@ViewChild("infoToast") infoToastButton!: ElementRef<HTMLButtonElement>;
@ViewChild("successToast") successToastButton!: ElementRef<HTMLButtonElement>;
@ViewChild("toastWithLabel") toastWithLabelButton!: ElementRef<HTMLButtonElement>;
}
describe("CopyClickDirective", () => {
let fixture: ComponentFixture<TestCopyClickComponent>;
const copyToClipboard = jest.fn();
const showToast = jest.fn();
const copyClickListener = mock<CopyClickListener>();
beforeEach(async () => {
copyToClipboard.mockClear();
showToast.mockClear();
copyClickListener.onCopy.mockClear();
await TestBed.configureTestingModule({
imports: [TestCopyClickComponent],
providers: [
{
provide: I18nService,
useValue: {
t: (key: string, ...rest: string[]) => {
if (rest?.length) {
return `${key} ${rest.join("")}`;
}
return key;
},
},
},
{ provide: PlatformUtilsService, useValue: { copyToClipboard } },
{ provide: ToastService, useValue: { showToast } },
{ provide: COPY_CLICK_LISTENER, useValue: copyClickListener },
],
}).compileComponents();
fixture = TestBed.createComponent(TestCopyClickComponent);
fixture.detectChanges();
});
it("copies the the value for all variants of toasts ", () => {
const noToastButton = fixture.componentInstance.noToastButton.nativeElement;
noToastButton.click();
expect(copyToClipboard).toHaveBeenCalledWith("no toast shown");
const infoToastButton = fixture.componentInstance.infoToastButton.nativeElement;
infoToastButton.click();
expect(copyToClipboard).toHaveBeenCalledWith("info toast shown");
const successToastButton = fixture.componentInstance.successToastButton.nativeElement;
successToastButton.click();
expect(copyToClipboard).toHaveBeenCalledWith("success toast shown");
});
it("does not show a toast when showToast is not present", () => {
const noToastButton = fixture.componentInstance.noToastButton.nativeElement;
noToastButton.click();
expect(showToast).not.toHaveBeenCalled();
});
it("shows a success toast when showToast is present", () => {
const successToastButton = fixture.componentInstance.successToastButton.nativeElement;
successToastButton.click();
expect(showToast).toHaveBeenCalledWith({
message: "copySuccessful",
variant: "success",
});
});
it("shows the toast variant when set with showToast", () => {
const infoToastButton = fixture.componentInstance.infoToastButton.nativeElement;
infoToastButton.click();
expect(showToast).toHaveBeenCalledWith({
message: "copySuccessful",
variant: "info",
});
});
it('includes label in toast message when "copyLabel" is set', () => {
const toastWithLabelButton = fixture.componentInstance.toastWithLabelButton.nativeElement;
toastWithLabelButton.click();
expect(showToast).toHaveBeenCalledWith({
message: "valueCopied Content",
variant: "success",
});
});
it("should call copyClickListener.onCopy when value is copied", () => {
const successToastButton = fixture.componentInstance.successToastButton.nativeElement;
successToastButton.click();
expect(copyClickListener.onCopy).toHaveBeenCalledWith("success toast shown");
});
});