From 059c6d60e7c6852123ac569a09a065a029c03914 Mon Sep 17 00:00:00 2001 From: Hunter Johnston Date: Sat, 21 Dec 2024 20:59:10 -0500 Subject: [PATCH] update tests --- packages/runed/src/lib/utilities/index.ts | 2 +- .../src/lib/utilities/onKeyStroke/index.ts | 2 +- .../onKeyStroke/onKeyStroke.test.svelte.ts | 118 ++++++++++-------- .../lib/components/demos/on-key-stroke.svelte | 5 +- 4 files changed, 71 insertions(+), 56 deletions(-) diff --git a/packages/runed/src/lib/utilities/index.ts b/packages/runed/src/lib/utilities/index.ts index cab1af93..124b5f3d 100644 --- a/packages/runed/src/lib/utilities/index.ts +++ b/packages/runed/src/lib/utilities/index.ts @@ -24,4 +24,4 @@ export * from "./useGeolocation/index.js"; export * from "./Context/index.js"; export * from "./IsInViewport/index.js"; export * from "./useActiveElement/index.js"; -export * from "./onKeyStroke/index.js"; \ No newline at end of file +export * from "./onKeyStroke/index.js"; diff --git a/packages/runed/src/lib/utilities/onKeyStroke/index.ts b/packages/runed/src/lib/utilities/onKeyStroke/index.ts index a3ea4744..48131e3b 100644 --- a/packages/runed/src/lib/utilities/onKeyStroke/index.ts +++ b/packages/runed/src/lib/utilities/onKeyStroke/index.ts @@ -1 +1 @@ -export * from "./onKeyStroke.svelte.js"; \ No newline at end of file +export * from "./onKeyStroke.svelte.js"; diff --git a/packages/runed/src/lib/utilities/onKeyStroke/onKeyStroke.test.svelte.ts b/packages/runed/src/lib/utilities/onKeyStroke/onKeyStroke.test.svelte.ts index 02077ef2..28afd9b1 100644 --- a/packages/runed/src/lib/utilities/onKeyStroke/onKeyStroke.test.svelte.ts +++ b/packages/runed/src/lib/utilities/onKeyStroke/onKeyStroke.test.svelte.ts @@ -3,6 +3,8 @@ import { onKeyStroke, onKeyDown, onKeyUp, onKeyPress } from "./onKeyStroke.svelt import { testWithEffect } from "$lib/test/util.svelte.js"; import { tick } from "svelte"; +const IS_MAC_REGEX = /Mac/; + describe("key stroke utilities", () => { let target: HTMLElement; @@ -16,6 +18,17 @@ describe("key stroke utilities", () => { vi.clearAllMocks(); }); + // Helper function to set platform + const mockPlatform = (platform: string) => { + Object.defineProperty(global, "navigator", { + value: { + ...navigator, + platform, + }, + writable: true, + }); + }; + // Helper function to create and dispatch keyboard events const dispatchKeyEvent = ( element: HTMLElement, @@ -51,57 +64,6 @@ describe("key stroke utilities", () => { stop(); }); - testWithEffect("should handle command/control + K combination", async () => { - const handler = vi.fn(); - const predicate = (event: KeyboardEvent) => { - const isMac = /Mac|iPod|iPhone|iPad/.test(navigator.platform); - const modifier = isMac ? event.metaKey : event.ctrlKey; - return event.key.toLowerCase() === "k" && modifier && !event.shiftKey && !event.altKey; - }; - - const { stop } = onKeyStroke(predicate, handler, { target }); - await tick(); - - // Test without any modifier (should not trigger) - dispatchKeyEvent(target, "keydown", "k"); - await tick(); - await tick(); - expect(handler).not.toHaveBeenCalled(); - - // Test with correct modifier for platform - const isMac = /Mac|iPod|iPhone|iPad/.test(navigator.platform); - dispatchKeyEvent(target, "keydown", "k", { - metaKey: isMac, - ctrlKey: !isMac, - shiftKey: false, - altKey: false, - }); - await tick(); - await tick(); - expect(handler).toHaveBeenCalledTimes(1); - - // Test with wrong modifier (should not trigger) - dispatchKeyEvent(target, "keydown", "k", { - metaKey: !isMac, - ctrlKey: isMac, - }); - await tick(); - await tick(); - expect(handler).toHaveBeenCalledTimes(1); - - // Test with additional modifiers (should not trigger) - dispatchKeyEvent(target, "keydown", "k", { - metaKey: isMac, - ctrlKey: !isMac, - shiftKey: true, - }); - await tick(); - await tick(); - expect(handler).toHaveBeenCalledTimes(1); - - stop(); - }); - testWithEffect("should handle array of keys", async () => { const handler = vi.fn(); const { stop } = onKeyStroke(["a", "b"], handler, { target }); @@ -261,4 +223,58 @@ describe("key stroke utilities", () => { expect(handler).toHaveBeenCalledTimes(1); }); }); + + describe("common key combinations", () => { + testWithEffect("should handle command + K on Mac", async () => { + mockPlatform("MacIntel"); + const handler = vi.fn(); + const predicate = (event: KeyboardEvent) => { + const isMac = IS_MAC_REGEX.test(navigator.platform); + const modifier = isMac ? event.metaKey : event.ctrlKey; + return event.key.toLowerCase() === "k" && modifier && !event.shiftKey && !event.altKey; + }; + + const { stop } = onKeyStroke(predicate, handler, { target }); + await tick(); + + // Test with command key (should trigger on Mac) + dispatchKeyEvent(target, "keydown", "k", { + metaKey: true, + ctrlKey: false, + shiftKey: false, + altKey: false, + }); + await tick(); + await tick(); + expect(handler).toHaveBeenCalledTimes(1); + + stop(); + }); + + testWithEffect("should handle ctrl + K on Windows", async () => { + mockPlatform("Win32"); + const handler = vi.fn(); + const predicate = (event: KeyboardEvent) => { + const isMac = IS_MAC_REGEX.test(navigator.platform); + const modifier = isMac ? event.metaKey : event.ctrlKey; + return event.key.toLowerCase() === "k" && modifier && !event.shiftKey && !event.altKey; + }; + + const { stop } = onKeyStroke(predicate, handler, { target }); + await tick(); + + // Test with control key (should trigger on Windows) + dispatchKeyEvent(target, "keydown", "k", { + metaKey: false, + ctrlKey: true, + shiftKey: false, + altKey: false, + }); + await tick(); + await tick(); + expect(handler).toHaveBeenCalledTimes(1); + + stop(); + }); + }); }); diff --git a/sites/docs/src/lib/components/demos/on-key-stroke.svelte b/sites/docs/src/lib/components/demos/on-key-stroke.svelte index 4d975c99..1e1c3d6c 100644 --- a/sites/docs/src/lib/components/demos/on-key-stroke.svelte +++ b/sites/docs/src/lib/components/demos/on-key-stroke.svelte @@ -1,7 +1,6 @@ -