Skip to content

Commit

Permalink
テストをvi.fn()に
Browse files Browse the repository at this point in the history
  • Loading branch information
tsym77yoshi committed May 28, 2024
1 parent c5c0e89 commit 0a82b96
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions tests/unit/lib/hotkeyManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,32 @@ it("registerできる", () => {

it("unregisterできる", () => {
const hotkeyManager = createHotkeyManager();
let testCount = 0;
const action = {
editor: "talk",
name: "音声書き出し",
callback: () => {
testCount++;
},
callback: vi.fn(),
} as const;
hotkeyManager.load([
{
action: "音声書き出し",
combination: HotkeyCombination("1"),
},
]);
hotkeyManager.onEditorChange("talk");
hotkeyManager.register(action);
hotkeyManager.keyInput(createDummyInput("1", "Digit1") as KeyboardEvent);
expect(testCount).toBe(1);
expect(action.callback).toHaveBeenCalledTimes(1);

hotkeyManager.unregister(action);
hotkeyManager.keyInput(createDummyInput("1", "Digit1") as KeyboardEvent);
expect(testCount).toBe(1);
expect(action.callback).toHaveBeenCalledTimes(1); // 呼び出し回数が増えない
});

let testCount = 0;
const callback = () => {
testCount++;
};
const callback = vi.fn();
const dummyAction: HotkeyAction = {
editor: "talk",
name: "音声書き出し",
callback,
callback: callback,
};
const createDummySetting = (combination: string): HotkeySettingType => ({
action: "音声書き出し",
Expand All @@ -92,14 +87,15 @@ describe("設定変更", () => {
beforeEach(() => {
const hotkeyManager_ = createHotkeyManager();
hotkeyManager = hotkeyManager_;
testCount = 0;
hotkeyManager.onEditorChange("talk");
callback.mockClear();
});

it("設定を登録するとhotkeysが更新される", () => {
hotkeyManager.register(dummyAction);
hotkeyManager.load([createDummySetting("1")]);
hotkeyManager.keyInput(createDummyInput("1", "Digit1") as KeyboardEvent);
expect(testCount).toBe(1);
expect(callback).toHaveBeenCalledTimes(1);
});

it("設定を更新するとhotkeysが更新される", () => {
Expand All @@ -108,41 +104,41 @@ describe("設定変更", () => {
hotkeyManager.replace(createDummySetting("A"));
hotkeyManager.keyInput(createDummyInput("a", "KeyA") as KeyboardEvent);

expect(testCount).toBe(1);
expect(callback).toHaveBeenCalledTimes(1);
});

it("未割り当てにするとhotkeysから削除される", () => {
hotkeyManager.register(dummyAction);
hotkeyManager.load([createDummySetting("1")]);
hotkeyManager.replace(createDummySetting(""));
hotkeyManager.keyInput(createDummyInput("1", "Digit1") as KeyboardEvent);
expect(testCount).toBe(0);
expect(callback).toHaveBeenCalledTimes(0);
});

it("未割り当てから割り当てるとhotkeysが更新される", () => {
hotkeyManager.register(dummyAction);
hotkeyManager.load([createDummySetting("")]);
hotkeyManager.keyInput(createDummyInput("1", "Digit1") as KeyboardEvent);
expect(testCount).toBe(0);
expect(callback).toHaveBeenCalledTimes(0);

hotkeyManager.replace(createDummySetting("1"));
hotkeyManager.keyInput(createDummyInput("1", "Digit1") as KeyboardEvent);
expect(testCount).toBe(1);
expect(callback).toHaveBeenCalledTimes(1);
});

it("割り当て -> 未割り当て -> 割り当てでhotkeysが更新される", () => {
hotkeyManager.register(dummyAction);
hotkeyManager.load([createDummySetting("1")]);

hotkeyManager.keyInput(createDummyInput("1", "Digit1") as KeyboardEvent);
expect(testCount).toBe(1);
expect(callback).toHaveBeenCalledTimes(1);

hotkeyManager.replace(createDummySetting(""));
hotkeyManager.keyInput(createDummyInput("1", "Digit1") as KeyboardEvent);
expect(testCount).toBe(1);
expect(callback).toHaveBeenCalledTimes(1);

hotkeyManager.replace(createDummySetting("A"));
hotkeyManager.keyInput(createDummyInput("a", "KeyA") as KeyboardEvent);
expect(testCount).toBe(2);
expect(callback).toHaveBeenCalledTimes(2);
});
});

0 comments on commit 0a82b96

Please sign in to comment.