Skip to content

Commit

Permalink
Let feature handle state keys
Browse files Browse the repository at this point in the history
Moves to a feature folder and creates clipboard-module level state
handler functions.

StateService is being paired down to storage routing, so we are handling storage
specifics in-module.

Co-authored-by: Justin Baur <justindbaur@users.noreply.github.com>
Co-authored-by: Daniel Smith <djsmith85@users.noreply.github.com>
  • Loading branch information
3 people committed Oct 12, 2022
1 parent 2442920 commit 2718ca0
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ import { BrowserApi } from "../browser/browserApi";
import { StateService } from "../services/abstractions/state.service";

import { ClearClipboard } from "./clearClipboard";
import { getClearClipboardTime, setClearClipboardTime } from "./clipboard-state";

jest.mock("./clipboard-state", () => {
return {
getClearClipboardTime: jest.fn(),
setClearClipboardTime: jest.fn(),
};
});

const getClearClipboardTimeMock = getClearClipboardTime as jest.Mock;
const setClearClipboardTimeMock = setClearClipboardTime as jest.Mock;

describe("clearClipboard", () => {
describe("run", () => {
Expand Down Expand Up @@ -33,7 +44,7 @@ describe("clearClipboard", () => {

jest.spyOn(BrowserApi, "sendTabsMessage").mockReturnValue();

stateService.getClearClipboardTime.mockResolvedValue(clearTime.getTime());
getClearClipboardTimeMock.mockResolvedValue(clearTime.getTime());

await ClearClipboard.run(executionTime, serviceCache);

Expand All @@ -48,7 +59,7 @@ describe("clearClipboard", () => {
const executionTime = new Date(2022, 1, 1, 12);
const clearTime = new Date(2022, 1, 1, 11);

stateService.getClearClipboardTime.mockResolvedValue(clearTime.getTime());
setClearClipboardTimeMock.mockResolvedValue(clearTime.getTime());

await ClearClipboard.run(executionTime, serviceCache);

Expand All @@ -58,7 +69,7 @@ describe("clearClipboard", () => {
it("has an undefined clearTime", async () => {
const executionTime = new Date(2022, 1, 1);

stateService.getClearClipboardTime.mockResolvedValue(undefined);
getClearClipboardTimeMock.mockResolvedValue(undefined);

await ClearClipboard.run(executionTime, serviceCache);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { stateServiceFactory } from "../background/service_factories/state-servi
import { BrowserApi } from "../browser/browserApi";
import { Account } from "../models/account";

import { getClearClipboardTime } from "./clipboard-state";

export class ClearClipboard {
static async run(executionTime: Date, serviceCache: Record<string, unknown>) {
const stateFactory = new StateFactory(GlobalState, Account);
Expand All @@ -26,7 +28,7 @@ export class ClearClipboard {
},
});

const clearClipboardTime = await stateService.getClearClipboardTime();
const clearClipboardTime = await getClearClipboardTime(stateService);

if (!clearClipboardTime) {
return;
Expand Down
10 changes: 10 additions & 0 deletions apps/browser/src/clipboard/clipboard-state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { StateService } from "../services/abstractions/state.service";

const clearClipboardStorageKey = "clearClipboardTime";
export const getClearClipboardTime = async (stateService: StateService) => {
return await stateService.getFromSessionMemory<number>(clearClipboardStorageKey);
};

export const setClearClipboardTime = async (stateService: StateService, time: number) => {
await stateService.setInSessionMemory(clearClipboardStorageKey, time);
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { matches, mock, MockProxy } from "jest-mock-extended";
import { mock, MockProxy } from "jest-mock-extended";

import { PasswordGenerationService } from "@bitwarden/common/abstractions/passwordGeneration.service";

import { BrowserApi } from "../browser/browserApi";
import { StateService } from "../services/abstractions/state.service";

import { setClearClipboardTime } from "./clipboard-state";
import { GeneratePasswordToClipboardCommand } from "./generate-password-to-clipboard-command";

jest.mock("./clipboard-state", () => {
return {
getClearClipboardTime: jest.fn(),
setClearClipboardTime: jest.fn(),
};
});

const setClearClipboardTimeMock = setClearClipboardTime as jest.Mock;

describe("GeneratePasswordToClipboardCommand", () => {
let passwordGenerationService: MockProxy<PasswordGenerationService>;
let stateService: MockProxy<StateService>;
Expand Down Expand Up @@ -43,16 +53,9 @@ describe("GeneratePasswordToClipboardCommand", () => {
text: "PASSWORD",
});

expect(stateService.setClearClipboardTime).toHaveBeenCalledTimes(1);

expect(stateService.setClearClipboardTime).toHaveBeenCalledWith(
matches((time: number) => {
const now = new Date();
const date = new Date(time);
expect(setClearClipboardTimeMock).toHaveBeenCalledTimes(1);

return date.getMinutes() - now.getMinutes() === 5;
})
);
expect(setClearClipboardTimeMock).toHaveBeenCalledWith(stateService, expect.any(Number));
});

it("does not have clear clipboard value", async () => {
Expand All @@ -67,7 +70,7 @@ describe("GeneratePasswordToClipboardCommand", () => {
text: "PASSWORD",
});

expect(stateService.setClearClipboardTime).not.toHaveBeenCalled();
expect(setClearClipboardTimeMock).not.toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { PasswordGenerationService } from "@bitwarden/common/abstractions/passwo

import { StateService } from "../services/abstractions/state.service";

import { setClearClipboardTime } from "./clipboard-state";
import { copyToClipboard } from "./copy-to-clipboard-command";

export class GeneratePasswordToClipboardCommand {
Expand All @@ -19,7 +20,7 @@ export class GeneratePasswordToClipboardCommand {
const clearClipboard = await this.stateService.getClearClipboard();

if (clearClipboard != null) {
await this.stateService.setClearClipboardTime(Date.now() + clearClipboard * 1000);
await setClearClipboardTime(this.stateService, Date.now() + clearClipboard * 1000);
}
}
}
3 changes: 3 additions & 0 deletions apps/browser/src/clipboard/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./clearClipboard";
export * from "./copy-to-clipboard-command";
export * from "./generate-password-to-clipboard-command";
2 changes: 0 additions & 2 deletions apps/browser/src/services/abstractions/state.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,4 @@ export abstract class StateService extends BaseStateServiceAbstraction<Account>
value: BrowserComponentState,
options?: StorageOptions
) => Promise<void>;
getClearClipboardTime: () => Promise<number>;
setClearClipboardTime: (time: number) => Promise<void>;
}
8 changes: 0 additions & 8 deletions apps/browser/src/services/state.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,4 @@ export class StateService
this.reconcileOptions(options, await this.defaultInMemoryOptions())
);
}

async setClearClipboardTime(time: number): Promise<void> {
this.setInSessionMemory("clearClipboardTime", time);
}

async getClearClipboardTime(): Promise<number | undefined> {
return this.getFromSessionMemory("clearClipboardTime");
}
}

0 comments on commit 2718ca0

Please sign in to comment.