1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-04 05:08:06 +02:00

[PM-6426] Updating jest tests for GeneratePasswordToClipboardCommand

This commit is contained in:
Cesar Gonzalez 2024-04-01 15:17:29 -05:00
parent 52f914bead
commit ef1388e531
No known key found for this signature in database
GPG Key ID: 3381A5457F8CCECF
2 changed files with 28 additions and 4 deletions

View File

@ -1,4 +1,5 @@
import { mock, MockProxy } from "jest-mock-extended";
import { firstValueFrom } from "rxjs";
import { AutofillSettingsService } from "@bitwarden/common/autofill/services/autofill-settings.service";
import { ScheduledTaskNames } from "@bitwarden/common/platform/enums/scheduled-task-name.enum";
@ -7,8 +8,13 @@ import { PasswordGenerationServiceAbstraction } from "@bitwarden/common/tools/ge
import { BrowserApi } from "../../platform/browser/browser-api";
import { BrowserTaskSchedulerService } from "../../platform/services/browser-task-scheduler.service";
import { ClearClipboard } from "./clear-clipboard";
import { GeneratePasswordToClipboardCommand } from "./generate-password-to-clipboard-command";
jest.mock("rxjs", () => ({
firstValueFrom: jest.fn(),
}));
describe("GeneratePasswordToClipboardCommand", () => {
let passwordGenerationService: MockProxy<PasswordGenerationServiceAbstraction>;
let autofillSettingsService: MockProxy<AutofillSettingsService>;
@ -18,7 +24,10 @@ describe("GeneratePasswordToClipboardCommand", () => {
beforeEach(() => {
passwordGenerationService = mock<PasswordGenerationServiceAbstraction>();
browserTaskSchedulerService = mock<BrowserTaskSchedulerService>();
autofillSettingsService = mock<AutofillSettingsService>();
browserTaskSchedulerService = mock<BrowserTaskSchedulerService>({
setTimeout: jest.fn(async (callback, timeoutInMs) => setTimeout(callback, timeoutInMs)),
});
passwordGenerationService.getOptions.mockResolvedValue([{ length: 8 }, {} as any]);
@ -39,23 +48,25 @@ describe("GeneratePasswordToClipboardCommand", () => {
describe("generatePasswordToClipboard", () => {
it("has clear clipboard value", async () => {
jest.spyOn(sut as any, "getClearClipboard").mockImplementation(() => 5 * 60); // 5 minutes
jest.useFakeTimers();
jest.spyOn(ClearClipboard, "run");
(firstValueFrom as jest.Mock).mockResolvedValue(2 * 60); // 2 minutes
await sut.generatePasswordToClipboard({ id: 1 } as any);
jest.advanceTimersByTime(2 * 60 * 1000);
expect(jest.spyOn(BrowserApi, "sendTabsMessage")).toHaveBeenCalledTimes(1);
expect(jest.spyOn(BrowserApi, "sendTabsMessage")).toHaveBeenCalledWith(1, {
command: "copyText",
text: "PASSWORD",
});
expect(browserTaskSchedulerService.setTimeout).toHaveBeenCalledTimes(1);
expect(browserTaskSchedulerService.setTimeout).toHaveBeenCalledWith(
expect.any(Function),
expect.any(Number),
ScheduledTaskNames.clearClipboardTimeout,
);
expect(ClearClipboard.run).toHaveBeenCalledTimes(1);
});
it("does not have clear clipboard value", async () => {

View File

@ -124,6 +124,18 @@ const offscreen = {
},
};
const alarms = {
clear: jest.fn(),
clearAll: jest.fn(),
create: jest.fn(),
get: jest.fn(),
getAll: jest.fn(),
onAlarm: {
addListener: jest.fn(),
removeListener: jest.fn(),
},
};
// set chrome
global.chrome = {
i18n,
@ -137,4 +149,5 @@ global.chrome = {
privacy,
extension,
offscreen,
alarms,
} as any;