Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Feb 22, 2024
1 parent 6d78d80 commit 319aa5d
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions starsky/starsky/clientapp/src/shared/keyboard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ describe("keyboard", () => {
describe("isInForm", () => {
it("null input", () => {
// new EventTarget() = not supported in Safari

const result = keyboard.isInForm(undefined);

expect(result).toBeNull();
});

it("null input 2", () => {
// new EventTarget() = not supported in Safari
const eventTarget: EventTarget = new EventTarget();
const event = new KeyboardEvent("keydown", {
keyCode: 37,
Expand Down Expand Up @@ -52,6 +60,47 @@ describe("keyboard", () => {
});

describe("SetFocusOnEndField", () => {
it("no child items", () => {
const focusSpy = jest.fn();
new Keyboard().SetFocusOnEndField({
focus: focusSpy,
childNodes: []
} as unknown as HTMLDivElement);

expect(focusSpy).toHaveBeenCalledTimes(1);
});

it("text content", () => {
const focusSpy = jest.fn();
new Keyboard().SetFocusOnEndField({
focus: focusSpy,
childNodes: [{}] // text content is missing
} as unknown as HTMLDivElement);

expect(focusSpy).toHaveBeenCalledTimes(0);
});

it("missing range", () => {
const focusSpy = jest.fn();
const selectionSpy = jest.spyOn(window, "getSelection").mockImplementationOnce(() => null);
jest.spyOn(document, "createRange").mockImplementationOnce(() => {
return {
setStart: jest.fn(),
collapse: jest.fn()
} as unknown as Range;
});
new Keyboard().SetFocusOnEndField({
focus: focusSpy,
childNodes: [
{
textContent: "hi"
}
]
} as unknown as HTMLDivElement);

expect(selectionSpy).toHaveBeenCalledTimes(1);
});

it("input", () => {
const target = document.createElement("div");
target.className = "test";
Expand Down

0 comments on commit 319aa5d

Please sign in to comment.