From 78b600b436844270b0468d0ade9ede25467cbd85 Mon Sep 17 00:00:00 2001 From: Mike Rourke Date: Mon, 6 Jan 2025 18:15:58 -0600 Subject: [PATCH] test: update tests to use Bun --- src/__tests__/getChordDisplay.test.ts | 14 +++----- src/__tests__/handleChords.test.ts | 14 ++++---- src/__tests__/isChordPressed.test.ts | 52 ++++++++++++++++----------- src/__tests__/printableChars.test.ts | 6 ++-- 4 files changed, 49 insertions(+), 37 deletions(-) diff --git a/src/__tests__/getChordDisplay.test.ts b/src/__tests__/getChordDisplay.test.ts index b997e1a..2f10e46 100644 --- a/src/__tests__/getChordDisplay.test.ts +++ b/src/__tests__/getChordDisplay.test.ts @@ -1,15 +1,11 @@ +import { describe, expect, it, mock } from "bun:test"; + import { getChordDisplay } from "../getChordDisplay.ts"; import { Key, Modifier, MouseButton, MouseEventButton } from "../types.ts"; -vi.mock("@laserware/arcade", async (importActual) => { - const mod = await importActual(); - - return { - // @ts-ignore - ...mod, - isPlatform: (platform: string) => platform === "mac", - }; -}); +mock.module("@laserware/arcade", () => ({ + isPlatform: () => true, +})); describe("the getChordDisplay function", () => { it("returns the display for a chord", () => { diff --git a/src/__tests__/handleChords.test.ts b/src/__tests__/handleChords.test.ts index 6190989..d6951bb 100644 --- a/src/__tests__/handleChords.test.ts +++ b/src/__tests__/handleChords.test.ts @@ -1,3 +1,5 @@ +import { describe, expect, it, mock } from "bun:test"; + import { handleChords } from "../handleChords.ts"; import { isPrintableCharPressed } from "../printableChars.ts"; import { Key, Modifier } from "../types.ts"; @@ -5,7 +7,7 @@ import { Key, Modifier } from "../types.ts"; describe("the handleChords function", () => { describe("when using the match and listener arguments", () => { it("only fires the handler when a single chord is pressed", () => { - const handlerFired = vi.fn(); + const handlerFired = mock(); const event = new KeyboardEvent("keydown", { key: "c", @@ -25,8 +27,8 @@ describe("the handleChords function", () => { describe("when using the builder function", () => { it("only fires the handler when the key chord is pressed", () => { - const handlerFired = vi.fn(); - const handlerNotFired = vi.fn(); + const handlerFired = mock(); + const handlerNotFired = mock(); const event = new KeyboardEvent("keydown", { key: "c", @@ -57,8 +59,8 @@ describe("the handleChords function", () => { }); it("handles arrow keys", () => { - const handlerFired = vi.fn(); - const handlerNotFired = vi.fn(); + const handlerFired = mock(); + const handlerNotFired = mock(); const event = new KeyboardEvent("keydown", { key: "ArrowDown", @@ -89,7 +91,7 @@ describe("the handleChords function", () => { }); it("handles when handlers", () => { - const handlerFired = vi.fn(); + const handlerFired = mock(); const event = new KeyboardEvent("keydown", { key: "a", diff --git a/src/__tests__/isChordPressed.test.ts b/src/__tests__/isChordPressed.test.ts index a1b9013..46ed861 100644 --- a/src/__tests__/isChordPressed.test.ts +++ b/src/__tests__/isChordPressed.test.ts @@ -1,19 +1,13 @@ -import { isPlatform } from "@laserware/arcade"; +import { describe, expect, it, mock } from "bun:test"; import { getChordDisplay } from "../getChordDisplay.ts"; import { isChordPressed } from "../isChordPressed.ts"; import { Key, Modifier, MouseButton, MouseEventButton } from "../types.ts"; -vi.mock("@laserware/arcade"); - describe("the isChordPressed function", () => { - beforeEach(() => { - vi.resetAllMocks(); - }); - describe("returns true if requirements are met for keyboard events", () => { // prettier-ignore - it.concurrent.each([ + it.each([ { event: new KeyboardEvent("keydown", { key: "B", altKey: false, ctrlKey: false, metaKey: false, shiftKey: false }), chord: Key.LetterB, @@ -45,7 +39,9 @@ describe("the isChordPressed function", () => { display: getChordDisplay(Modifier.Alt | Modifier.Shift | Key.LetterA), }, ])("when $display is pressed and event matches", async ({ event, chord }) => { - vi.mocked(isPlatform).mockImplementationOnce((platform: string) => platform === "mac"); + mock.module("@laserware/arcade", () => ({ + isPlatform: (platform: string) => platform === "mac", + })); const result = isChordPressed(event, chord); @@ -55,7 +51,7 @@ describe("the isChordPressed function", () => { describe("returns false if requirements are not met for keyboard events", () => { // prettier-ignore - it.concurrent.each([ + it.each([ { event: new KeyboardEvent("keydown", { key: "B", altKey: true, ctrlKey: false, metaKey: false, shiftKey: false }), chord: Key.LetterB, @@ -82,7 +78,9 @@ describe("the isChordPressed function", () => { display: getChordDisplay(Modifier.Alt | Modifier.Cmd | Key.LetterB), }, ])("when $display is pressed and event does not match", async ({ event, chord })=> { - vi.mocked(isPlatform).mockImplementationOnce((platform: string) => platform === "mac"); + mock.module("@laserware/arcade", () => ({ + isPlatform: (platform: string) => platform === "mac", + })); const result = isChordPressed(event, chord); @@ -92,7 +90,7 @@ describe("the isChordPressed function", () => { describe("when the Command key is specified", () => { // prettier-ignore - it.concurrent.each([ + it.each([ // Command modifier: { event: new KeyboardEvent("keydown", { key: "A", altKey: false, ctrlKey: true, metaKey: false, shiftKey: false }), @@ -175,7 +173,9 @@ describe("the isChordPressed function", () => { ])( "returns $expected when $display ($chord) is pressed", async ({ event, chord, expected }) => { - vi.mocked(isPlatform).mockImplementationOnce((platform: string) => platform === "mac"); + mock.module("@laserware/arcade", () => ({ + isPlatform: (platform: string) => platform === "mac", + })); const result = isChordPressed(event, chord); @@ -185,7 +185,9 @@ describe("the isChordPressed function", () => { }); it("returns true if only modifiers are specified with no key", () => { - vi.mocked(isPlatform).mockImplementationOnce((platform: string) => platform === "mac"); + mock.module("@laserware/arcade", () => ({ + isPlatform: (platform: string) => platform === "mac", + })); // prettier-ignore const event = new KeyboardEvent("keydown", { altKey: true, ctrlKey: true, metaKey: false, shiftKey: true }); @@ -196,7 +198,9 @@ describe("the isChordPressed function", () => { }); it("returns true if mouse buttons are specified and are clicked", () => { - vi.mocked(isPlatform).mockImplementationOnce((platform: string) => platform === "mac"); + mock.module("@laserware/arcade", () => ({ + isPlatform: (platform: string) => platform === "mac", + })); // prettier-ignore const event = new MouseEvent("mousedown", { altKey: true, ctrlKey: false, metaKey: false, shiftKey: false, buttons: MouseEventButton.Left }); @@ -208,7 +212,9 @@ describe("the isChordPressed function", () => { describe("when checking for CmdOrCtrl", () => { it("clears the Cmd key on macOS", () => { - vi.mocked(isPlatform).mockImplementationOnce((platform: string) => platform === "mac"); + mock.module("@laserware/arcade", () => ({ + isPlatform: (platform: string) => platform === "mac", + })); // prettier-ignore const event = new KeyboardEvent("keydown", { altKey: false, ctrlKey: true, metaKey: true, shiftKey: false }); @@ -219,7 +225,9 @@ describe("the isChordPressed function", () => { }); it("clears the Ctrl key on Windows/Linux", () => { - vi.mocked(isPlatform).mockImplementationOnce((platform: string) => platform !== "mac"); + mock.module("@laserware/arcade", () => ({ + isPlatform: (platform: string) => platform !== "mac", + })); // prettier-ignore const event = new KeyboardEvent("keydown", { altKey: false, ctrlKey: true, metaKey: false, shiftKey: false }); @@ -231,7 +239,9 @@ describe("the isChordPressed function", () => { }); it("handles Shift and Shift + Tab", () => { - vi.mocked(isPlatform).mockImplementationOnce((platform: string) => platform === "mac"); + mock.module("@laserware/arcade", () => ({ + isPlatform: (platform: string) => platform === "mac", + })); // prettier-ignore const event = new KeyboardEvent("keydown", { key: "Tab", altKey: false, ctrlKey: false, metaKey: false, shiftKey: true }); @@ -243,7 +253,7 @@ describe("the isChordPressed function", () => { describe("returns true if requirements are met for mouse events", () => { // prettier-ignore - it.concurrent.each([ + it.each([ { event: new MouseEvent("keydown", { buttons: MouseEventButton.Left, altKey: false, ctrlKey: false, metaKey: false, shiftKey: false }), chord: MouseButton.Left, @@ -275,7 +285,9 @@ describe("the isChordPressed function", () => { display: getChordDisplay(Modifier.Alt | Modifier.Shift | MouseButton.None), }, ])("when $display is pressed and event matches", async ({ event, chord }) => { - vi.mocked(isPlatform).mockImplementationOnce((platform: string) => platform === "mac"); + mock.module("@laserware/arcade", () => ({ + isPlatform: (platform: string) => platform === "mac", + })); const result = isChordPressed(event, chord); diff --git a/src/__tests__/printableChars.test.ts b/src/__tests__/printableChars.test.ts index 41d5bd7..e642a36 100644 --- a/src/__tests__/printableChars.test.ts +++ b/src/__tests__/printableChars.test.ts @@ -1,7 +1,9 @@ +import { describe, expect, it } from "bun:test"; + import { isPrintableChar, isPrintableCharPressed } from "../printableChars.ts"; describe("within printableChars", () => { - describe.concurrent("the isPrintableChar function", () => { + describe("the isPrintableChar function", () => { it("returns true for lowercase letters", async () => { expect(isPrintableChar("a")).toBeTruthy(); }); @@ -31,7 +33,7 @@ describe("within printableChars", () => { }); }); - describe.concurrent("the isPrintableCharPressed function", () => { + describe("the isPrintableCharPressed function", () => { it("returns true for lowercase letters", async () => { const event = new KeyboardEvent("keydown", { key: "a" });