Spaces:
Paused
Paused
File size: 2,839 Bytes
ff7a361 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | import type { KeyboardEvent } from "react";
import { describe, expect, it, vi } from "vitest";
import { handleEnterKeyDown } from "./keyboard";
function mockEvent(
shift: boolean,
): Partial<KeyboardEvent<HTMLTextAreaElement>> {
return {
code: "Enter",
shiftKey: shift,
keyCode: 13,
preventDefault: vi.fn(),
nativeEvent: { isComposing: false },
} as unknown as Partial<KeyboardEvent<HTMLTextAreaElement>>;
}
describe("handleEnterKeyDown", () => {
it("submits when Enter without Shift and enterToSubmit true", () => {
const onSubmit = vi.fn();
const event = mockEvent(false);
handleEnterKeyDown(
event as KeyboardEvent<HTMLTextAreaElement>,
{ enterToSubmit: true },
onSubmit,
);
expect(event.preventDefault).toHaveBeenCalled();
expect(onSubmit).toHaveBeenCalled();
});
it("does not submit when Enter without Shift and enterToSubmit false", () => {
const onSubmit = vi.fn();
const event = mockEvent(false);
handleEnterKeyDown(
event as KeyboardEvent<HTMLTextAreaElement>,
{ enterToSubmit: false },
onSubmit,
);
expect(event.preventDefault).not.toHaveBeenCalled();
expect(onSubmit).not.toHaveBeenCalled();
});
it("submits when Shift+Enter and enterToSubmit false", () => {
const onSubmit = vi.fn();
const event = mockEvent(true);
handleEnterKeyDown(
event as KeyboardEvent<HTMLTextAreaElement>,
{ enterToSubmit: false },
onSubmit,
);
expect(event.preventDefault).toHaveBeenCalled();
expect(onSubmit).toHaveBeenCalled();
});
it("does not submit when Shift+Enter and enterToSubmit true", () => {
const onSubmit = vi.fn();
const event = mockEvent(true);
handleEnterKeyDown(
event as KeyboardEvent<HTMLTextAreaElement>,
{ enterToSubmit: true },
onSubmit,
);
expect(event.preventDefault).not.toHaveBeenCalled();
expect(onSubmit).not.toHaveBeenCalled();
});
it("does not submit when Enter confirms an IME composition candidate", () => {
const onSubmit = vi.fn();
const event = {
...mockEvent(false),
nativeEvent: { isComposing: true },
};
handleEnterKeyDown(
event as KeyboardEvent<HTMLTextAreaElement>,
{ enterToSubmit: true },
onSubmit,
);
expect(event.preventDefault).not.toHaveBeenCalled();
expect(onSubmit).not.toHaveBeenCalled();
});
it("does not submit when the composition is only flagged by keyCode 229", () => {
const onSubmit = vi.fn();
const event = {
...mockEvent(false),
keyCode: 229,
};
handleEnterKeyDown(
event as KeyboardEvent<HTMLTextAreaElement>,
{ enterToSubmit: true },
onSubmit,
);
expect(event.preventDefault).not.toHaveBeenCalled();
expect(onSubmit).not.toHaveBeenCalled();
});
});
|