|
| 1 | +import React from "react"; |
| 2 | +import { act, render } from "@testing-library/react"; |
| 3 | + |
| 4 | +import { useTextValidation } from "../useTextValidation"; |
| 5 | + |
| 6 | +const HookWrapper: React.FC<{ value: string; callback: jest.Mock; callbackDelay?: number }> = ({ |
| 7 | + value, |
| 8 | + callback, |
| 9 | + callbackDelay = 0, |
| 10 | +}) => { |
| 11 | + useTextValidation({ |
| 12 | + value, |
| 13 | + onChange: jest.fn(), |
| 14 | + invisibleCharacterWarning: { callback, callbackDelay }, |
| 15 | + }); |
| 16 | + return null; |
| 17 | +}; |
| 18 | + |
| 19 | +describe("useTextValidation", () => { |
| 20 | + beforeEach(() => { |
| 21 | + jest.useFakeTimers(); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(() => { |
| 25 | + jest.useRealTimers(); |
| 26 | + }); |
| 27 | + |
| 28 | + /** Render the hook with a controlled value and flush the debounce timer. */ |
| 29 | + const runWithValue = (value: string, callbackDelay = 0) => { |
| 30 | + const callback = jest.fn(); |
| 31 | + render(<HookWrapper value={value} callback={callback} callbackDelay={callbackDelay} />); |
| 32 | + act(() => { |
| 33 | + jest.runAllTimers(); |
| 34 | + }); |
| 35 | + return callback; |
| 36 | + }; |
| 37 | + |
| 38 | + describe("invisible character detection", () => { |
| 39 | + it("reports empty set for plain text", () => { |
| 40 | + const callback = runWithValue("hello world"); |
| 41 | + expect(callback).toHaveBeenCalledWith(new Set()); |
| 42 | + }); |
| 43 | + |
| 44 | + it("detects zero-width space (U+200B)", () => { |
| 45 | + const callback = runWithValue("hello\u200Bworld"); |
| 46 | + expect(callback).toHaveBeenCalledWith(new Set([0x200b])); |
| 47 | + }); |
| 48 | + |
| 49 | + it("detects zero-width non-joiner (U+200C)", () => { |
| 50 | + const callback = runWithValue("hello\u200Cworld"); |
| 51 | + expect(callback).toHaveBeenCalledWith(new Set([0x200c])); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe("emoji false-positive prevention", () => { |
| 56 | + it("does not flag ✔️ (base char + variation selector U+FE0F)", () => { |
| 57 | + const callback = runWithValue("✔️"); |
| 58 | + expect(callback).toHaveBeenCalledWith(new Set()); |
| 59 | + }); |
| 60 | + |
| 61 | + it("does not flag ZWJ sequence emoji 👨👩👧👦", () => { |
| 62 | + const callback = runWithValue("👨👩👧👦"); |
| 63 | + expect(callback).toHaveBeenCalledWith(new Set()); |
| 64 | + }); |
| 65 | + |
| 66 | + it("does not flag keycap emoji #️⃣", () => { |
| 67 | + const callback = runWithValue("#️⃣"); |
| 68 | + expect(callback).toHaveBeenCalledWith(new Set()); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe("mixed content", () => { |
| 73 | + it("detects ZWS while ignoring surrounding emoji", () => { |
| 74 | + const callback = runWithValue("Check\u200B ✔️👨👩👧#️⃣"); |
| 75 | + expect(callback).toHaveBeenCalledWith(new Set([0x200b])); |
| 76 | + }); |
| 77 | + |
| 78 | + it("reports empty set for text with only emoji", () => { |
| 79 | + const callback = runWithValue("✔️ 👨👩👧👦#️⃣"); |
| 80 | + expect(callback).toHaveBeenCalledWith(new Set()); |
| 81 | + }); |
| 82 | + }); |
| 83 | +}); |
0 commit comments