Skip to content

Commit

Permalink
test: update tests to use Bun
Browse files Browse the repository at this point in the history
  • Loading branch information
mikerourke committed Jan 7, 2025
1 parent e75132a commit 78b600b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 37 deletions.
14 changes: 5 additions & 9 deletions src/__tests__/getChordDisplay.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down
14 changes: 8 additions & 6 deletions src/__tests__/handleChords.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { describe, expect, it, mock } from "bun:test";

import { handleChords } from "../handleChords.ts";
import { isPrintableCharPressed } from "../printableChars.ts";
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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
52 changes: 32 additions & 20 deletions src/__tests__/isChordPressed.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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);

Expand All @@ -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,
Expand All @@ -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);

Expand All @@ -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 }),
Expand Down Expand Up @@ -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);

Expand All @@ -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 });
Expand All @@ -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 });
Expand All @@ -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 });
Expand All @@ -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 });
Expand All @@ -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 });
Expand All @@ -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,
Expand Down Expand Up @@ -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);

Expand Down
6 changes: 4 additions & 2 deletions src/__tests__/printableChars.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
Expand Down Expand Up @@ -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" });

Expand Down

0 comments on commit 78b600b

Please sign in to comment.