From 319aa5d798291ac5320bd7cf714b2edc6b4630d3 Mon Sep 17 00:00:00 2001 From: Dion Date: Thu, 22 Feb 2024 19:27:04 +0100 Subject: [PATCH] add tests --- .../clientapp/src/shared/keyboard.spec.ts | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/starsky/starsky/clientapp/src/shared/keyboard.spec.ts b/starsky/starsky/clientapp/src/shared/keyboard.spec.ts index 6716bf48fb..b253639b02 100644 --- a/starsky/starsky/clientapp/src/shared/keyboard.spec.ts +++ b/starsky/starsky/clientapp/src/shared/keyboard.spec.ts @@ -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, @@ -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";