-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuseTextValidation.test.tsx
More file actions
83 lines (70 loc) · 2.79 KB
/
Copy pathuseTextValidation.test.tsx
File metadata and controls
83 lines (70 loc) · 2.79 KB
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
import React from "react";
import { act, render } from "@testing-library/react";
import { useTextValidation } from "../useTextValidation";
const HookWrapper: React.FC<{ value: string; callback: jest.Mock; callbackDelay?: number }> = ({
value,
callback,
callbackDelay = 0,
}) => {
useTextValidation({
value,
onChange: jest.fn(),
invisibleCharacterWarning: { callback, callbackDelay },
});
return null;
};
describe("useTextValidation", () => {
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.useRealTimers();
});
/** Render the hook with a controlled value and flush the debounce timer. */
const runWithValue = (value: string, callbackDelay = 0) => {
const callback = jest.fn();
render(<HookWrapper value={value} callback={callback} callbackDelay={callbackDelay} />);
act(() => {
jest.runAllTimers();
});
return callback;
};
describe("invisible character detection", () => {
it("reports empty set for plain text", () => {
const callback = runWithValue("hello world");
expect(callback).toHaveBeenCalledWith(new Set());
});
it("detects zero-width space (U+200B)", () => {
const callback = runWithValue("hello\u200Bworld");
expect(callback).toHaveBeenCalledWith(new Set([0x200b]));
});
it("detects zero-width non-joiner (U+200C)", () => {
const callback = runWithValue("hello\u200Cworld");
expect(callback).toHaveBeenCalledWith(new Set([0x200c]));
});
});
describe("emoji false-positive prevention", () => {
it("does not flag ✔️ (base char + variation selector U+FE0F)", () => {
const callback = runWithValue("✔️");
expect(callback).toHaveBeenCalledWith(new Set());
});
it("does not flag ZWJ sequence emoji 👨👩👧👦", () => {
const callback = runWithValue("👨👩👧👦");
expect(callback).toHaveBeenCalledWith(new Set());
});
it("does not flag keycap emoji #️⃣", () => {
const callback = runWithValue("#️⃣");
expect(callback).toHaveBeenCalledWith(new Set());
});
});
describe("mixed content", () => {
it("detects ZWS while ignoring surrounding emoji", () => {
const callback = runWithValue("Check\u200B ✔️👨👩👧#️⃣");
expect(callback).toHaveBeenCalledWith(new Set([0x200b]));
});
it("reports empty set for text with only emoji", () => {
const callback = runWithValue("✔️ 👨👩👧👦#️⃣");
expect(callback).toHaveBeenCalledWith(new Set());
});
});
});