Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Feb 22, 2024
1 parent 5d8a236 commit c0be912
Showing 1 changed file with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
import React from "react";
import * as useHotKeysParent from "../../hooks/use-keyboard/use-hotkeys";
import * as useLocation from "../../hooks/use-location/use-location";
import { UrlQuery } from "../url-query";
import { GlobalShortcuts } from "./global-shortcuts";

describe("GlobalShortcuts", () => {
it("should call useHotKeys with the correct arguments", () => {
it("command + shift + k", () => {
const locationObject = {
location: window.location,
navigate: jest.fn()
};

jest.spyOn(useLocation, "default").mockImplementationOnce(() => locationObject);
jest.spyOn(React, "useEffect").mockImplementationOnce((cb) => {
cb();
});

GlobalShortcuts();
const event = new KeyboardEvent("keydown", {
bubbles: true,
cancelable: true,
key: "k",
metaKey: true,
shiftKey: true
});

jest.spyOn(useHotKeysParent, "default").mockImplementationOnce((_, callback) => {
if (callback) {
callback(event);
}
});

expect(useHotKeysParent).toHaveBeenCalledWith(
{ key: "k", shiftKey: true, ctrlKeyOrMetaKey: true },
expect.any(Function),
[]
);
const useLocationSpy = jest
.spyOn(useLocation, "default")
.mockImplementationOnce(() => locationObject);

GlobalShortcuts();

expect(locationObject.navigate).toHaveBeenCalledWith(expect.any(String));
expect(useLocationSpy).toHaveBeenCalled();
expect(locationObject.navigate).toHaveBeenCalled();
expect(locationObject.navigate).toHaveBeenCalledWith(new UrlQuery().UrlPreferencesPage());
});
});

0 comments on commit c0be912

Please sign in to comment.