Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
huntabyte committed Dec 22, 2024
1 parent 6b343a9 commit 059c6d6
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 56 deletions.
2 changes: 1 addition & 1 deletion packages/runed/src/lib/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
export * from "./onKeyStroke/index.js";
2 changes: 1 addition & 1 deletion packages/runed/src/lib/utilities/onKeyStroke/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./onKeyStroke.svelte.js";
export * from "./onKeyStroke.svelte.js";
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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();
});
});
});
5 changes: 2 additions & 3 deletions sites/docs/src/lib/components/demos/on-key-stroke.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

<script lang="ts">
import { onKeyStroke } from 'runed';
import { DemoContainer } from '@svecodocs/kit';
import { onKeyStroke } from "runed";

Check failure on line 2 in sites/docs/src/lib/components/demos/on-key-stroke.svelte

View workflow job for this annotation

GitHub Actions / Lint

'onKeyStroke' is defined but never used. Allowed unused vars must match /^_/u
import { DemoContainer } from "@svecodocs/kit";
</script>

<DemoContainer>
Expand Down

0 comments on commit 059c6d6

Please sign in to comment.