|
1 | 1 | import assert from "node:assert/strict"; |
2 | | -import test from "node:test"; |
| 2 | +import { after, before, test } from "node:test"; |
| 3 | +import { JSDOM } from "jsdom"; |
| 4 | + |
| 5 | +const dom = new JSDOM("<!doctype html><html><body></body></html>", { |
| 6 | + url: "http://localhost", |
| 7 | +}); |
| 8 | + |
| 9 | +before(() => { |
| 10 | + Object.assign(globalThis, { |
| 11 | + document: dom.window.document, |
| 12 | + HTMLElement: dom.window.HTMLElement, |
| 13 | + IS_REACT_ACT_ENVIRONMENT: true, |
| 14 | + window: dom.window, |
| 15 | + }); |
| 16 | +}); |
| 17 | + |
| 18 | +after(() => dom.window.close()); |
3 | 19 |
|
4 | 20 | import { |
5 | 21 | createWorkstreamPollingRegistry, |
@@ -155,3 +171,101 @@ test("polling deduplicates canonical identities and tears down", async () => { |
155 | 171 | assert.equal(aborted, true); |
156 | 172 | resolveRequest(); |
157 | 173 | }); |
| 174 | + |
| 175 | +test("workstream status hook exposes loading before a supported PR resolves", async () => { |
| 176 | + const { cleanup, renderHook } = await import("@testing-library/react"); |
| 177 | + const { useWorkstreamPullRequestStatuses } = await import( |
| 178 | + "./workstreamPullRequestStatus.ts" |
| 179 | + ); |
| 180 | + const reference = parseWorkstreamPullRequestReference( |
| 181 | + "https://github.com/block/buzz/pull/42", |
| 182 | + ); |
| 183 | + const registry = createWorkstreamPollingRegistry({ |
| 184 | + fetcher: () => new Promise(() => {}), |
| 185 | + }); |
| 186 | + const references = [reference]; |
| 187 | + const { result, unmount } = renderHook(() => |
| 188 | + useWorkstreamPullRequestStatuses(references, registry), |
| 189 | + ); |
| 190 | + assert.deepEqual(result.current.get(reference.identity), { |
| 191 | + status: "loading", |
| 192 | + }); |
| 193 | + unmount(); |
| 194 | + cleanup(); |
| 195 | +}); |
| 196 | + |
| 197 | +test("workstream status hook preserves a synchronous live callback", async () => { |
| 198 | + const { act, cleanup, renderHook } = await import("@testing-library/react"); |
| 199 | + const { useWorkstreamPullRequestStatuses } = await import( |
| 200 | + "./workstreamPullRequestStatus.ts" |
| 201 | + ); |
| 202 | + const reference = parseWorkstreamPullRequestReference( |
| 203 | + "https://github.com/block/buzz/pull/42", |
| 204 | + ); |
| 205 | + const registry = createWorkstreamPollingRegistry({ |
| 206 | + fetcher: async (url) => |
| 207 | + String(url).endsWith("/reviews?per_page=100") |
| 208 | + ? response([]) |
| 209 | + : response({ number: 42, title: "Board", state: "open", draft: false }), |
| 210 | + maxPolls: 1, |
| 211 | + }); |
| 212 | + const prime = registry.subscribe(reference, () => {}); |
| 213 | + await act(async () => { |
| 214 | + await Promise.resolve(); |
| 215 | + await Promise.resolve(); |
| 216 | + }); |
| 217 | + const references = [reference]; |
| 218 | + const { result, unmount } = renderHook(() => |
| 219 | + useWorkstreamPullRequestStatuses(references, registry), |
| 220 | + ); |
| 221 | + await act(async () => { |
| 222 | + await Promise.resolve(); |
| 223 | + }); |
| 224 | + assert.deepEqual(result.current.get(reference.identity), { |
| 225 | + status: "live", |
| 226 | + provider: "github", |
| 227 | + href: "https://github.com/block/buzz/pull/42", |
| 228 | + repository: "block/buzz", |
| 229 | + number: 42, |
| 230 | + title: "Board", |
| 231 | + lifecycle: "open", |
| 232 | + reviewState: "no-reviews", |
| 233 | + }); |
| 234 | + unmount(); |
| 235 | + prime(); |
| 236 | + cleanup(); |
| 237 | +}); |
| 238 | + |
| 239 | +test("workstream status hook removes stale references and preserves unsupported state", async () => { |
| 240 | + const { act, cleanup, renderHook } = await import("@testing-library/react"); |
| 241 | + const { useWorkstreamPullRequestStatuses } = await import( |
| 242 | + "./workstreamPullRequestStatus.ts" |
| 243 | + ); |
| 244 | + const supported = parseWorkstreamPullRequestReference( |
| 245 | + "https://github.com/block/buzz/pull/42", |
| 246 | + ); |
| 247 | + const unsupported = parseWorkstreamPullRequestReference( |
| 248 | + "https://example.com/pr/1", |
| 249 | + ); |
| 250 | + const registry = createWorkstreamPollingRegistry({ |
| 251 | + fetcher: () => new Promise(() => {}), |
| 252 | + }); |
| 253 | + const { result, rerender, unmount } = renderHook( |
| 254 | + ({ refs }) => useWorkstreamPullRequestStatuses(refs, registry), |
| 255 | + { initialProps: { refs: [supported, unsupported] } }, |
| 256 | + ); |
| 257 | + assert.deepEqual( |
| 258 | + result.current.get(unsupported.identity)?.status, |
| 259 | + "unsupported", |
| 260 | + ); |
| 261 | + await act(async () => { |
| 262 | + rerender({ refs: [unsupported] }); |
| 263 | + }); |
| 264 | + assert.equal(result.current.has(supported.identity), false); |
| 265 | + assert.deepEqual( |
| 266 | + result.current.get(unsupported.identity)?.status, |
| 267 | + "unsupported", |
| 268 | + ); |
| 269 | + unmount(); |
| 270 | + cleanup(); |
| 271 | +}); |
0 commit comments