|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { test } from "node:test"; |
| 3 | +import React, { act } from "react"; |
| 4 | +import { createRoot } from "react-dom/client"; |
| 5 | + |
| 6 | +import { ChannelNavigationProvider } from "@/shared/context/ChannelNavigationContext"; |
| 7 | +import { useChannelLinks } from "./useChannelLinks.ts"; |
| 8 | + |
| 9 | +const channels = [ |
| 10 | + { |
| 11 | + id: "general", |
| 12 | + name: "general", |
| 13 | + channelType: "stream", |
| 14 | + archivedAt: null, |
| 15 | + }, |
| 16 | +]; |
| 17 | + |
| 18 | +function mountChannelLinks(t) { |
| 19 | + t.mock.timers.enable({ apis: ["setTimeout"] }); |
| 20 | + const root = createRoot(document.createElement("div")); |
| 21 | + let current; |
| 22 | + function Harness() { |
| 23 | + current = useChannelLinks(); |
| 24 | + return null; |
| 25 | + } |
| 26 | + act(() => { |
| 27 | + root.render( |
| 28 | + React.createElement( |
| 29 | + ChannelNavigationProvider, |
| 30 | + { channels }, |
| 31 | + React.createElement(Harness), |
| 32 | + ), |
| 33 | + ); |
| 34 | + }); |
| 35 | + t.after(() => act(() => root.unmount())); |
| 36 | + return () => current; |
| 37 | +} |
| 38 | + |
| 39 | +for (const replacement of ["", "Edited message", "Welcome to #general "]) { |
| 40 | + test(`channel suggestions close immediately after replacing the query with ${JSON.stringify(replacement)}`, (t) => { |
| 41 | + const links = mountChannelLinks(t); |
| 42 | + act(() => links().updateChannelQuery("Welcome to #general", 19)); |
| 43 | + act(() => t.mock.timers.tick(120)); |
| 44 | + assert.equal(links().isChannelOpen, true); |
| 45 | + |
| 46 | + act(() => links().updateChannelQuery(replacement, replacement.length)); |
| 47 | + |
| 48 | + // Enter must submit the current edit before the debounce window ends. |
| 49 | + assert.equal(links().isChannelOpen, false); |
| 50 | + for (const key of ["Enter", "Tab"]) { |
| 51 | + assert.deepEqual( |
| 52 | + links().handleChannelKeyDown({ |
| 53 | + key, |
| 54 | + preventDefault: () => |
| 55 | + assert.fail("stale suggestion consumed the key"), |
| 56 | + }), |
| 57 | + { handled: false }, |
| 58 | + ); |
| 59 | + } |
| 60 | + act(() => t.mock.timers.tick(120)); |
| 61 | + assert.equal(links().isChannelOpen, false); |
| 62 | + }); |
| 63 | +} |
| 64 | + |
| 65 | +test("clearing a pending query cancels it; a later valid query still opens", (t) => { |
| 66 | + const links = mountChannelLinks(t); |
| 67 | + act(() => links().updateChannelQuery("#gen", 4)); |
| 68 | + assert.equal(links().isChannelOpen, false); |
| 69 | + act(() => links().updateChannelQuery("", 0)); |
| 70 | + act(() => t.mock.timers.tick(120)); |
| 71 | + assert.equal(links().isChannelOpen, false); |
| 72 | + |
| 73 | + act(() => links().updateChannelQuery("#gen", 4)); |
| 74 | + act(() => t.mock.timers.tick(120)); |
| 75 | + assert.equal(links().isChannelOpen, true); |
| 76 | + const event = { key: "Enter", preventDefault: t.mock.fn() }; |
| 77 | + assert.deepEqual(links().handleChannelKeyDown(event), { |
| 78 | + handled: true, |
| 79 | + suggestion: { id: "general", name: "general", channelType: "stream" }, |
| 80 | + }); |
| 81 | + assert.equal(event.preventDefault.mock.callCount(), 1); |
| 82 | +}); |
0 commit comments