|
| 1 | +import { |
| 2 | + claimRSCReloadDocumentAttempt, |
| 3 | + clearRSCReloadDocumentAttempt, |
| 4 | + getRSCManifestReloadDocumentTarget, |
| 5 | + getRSCReloadDocumentTarget, |
| 6 | + isRSCReloadDocumentResponse, |
| 7 | +} from "../../lib/rsc/browser"; |
| 8 | + |
| 9 | +describe("RSC document reload recovery", () => { |
| 10 | + beforeEach(() => { |
| 11 | + window.history.replaceState(null, "", "/current?tab=one#section"); |
| 12 | + // @ts-expect-error test-only router globals |
| 13 | + window.__reactRouterDataRouter = undefined; |
| 14 | + // @ts-expect-error test-only router globals |
| 15 | + window.__reactRouterRSCVersion = undefined; |
| 16 | + clearRSCReloadDocumentAttempt(); |
| 17 | + }); |
| 18 | + |
| 19 | + it("recognizes reload-document responses", () => { |
| 20 | + expect( |
| 21 | + isRSCReloadDocumentResponse( |
| 22 | + new Response(null, { |
| 23 | + status: 204, |
| 24 | + headers: { "X-Remix-Reload-Document": "true" }, |
| 25 | + }), |
| 26 | + ), |
| 27 | + ).toBe(true); |
| 28 | + |
| 29 | + expect( |
| 30 | + isRSCReloadDocumentResponse( |
| 31 | + new Response(null, { |
| 32 | + status: 200, |
| 33 | + headers: { "X-Remix-Reload-Document": "true" }, |
| 34 | + }), |
| 35 | + ), |
| 36 | + ).toBe(false); |
| 37 | + }); |
| 38 | + |
| 39 | + it("reloads GET RSC responses at the document request target", () => { |
| 40 | + // @ts-expect-error partial router global for this focused unit test |
| 41 | + window.__reactRouterDataRouter = { |
| 42 | + state: { |
| 43 | + navigation: { |
| 44 | + location: { |
| 45 | + pathname: "/target", |
| 46 | + search: "?panel=notes", |
| 47 | + hash: "#source", |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }; |
| 52 | + |
| 53 | + expect( |
| 54 | + getRSCReloadDocumentTarget( |
| 55 | + new Request("http://localhost/target?panel=notes"), |
| 56 | + ), |
| 57 | + ).toBe("/target?panel=notes#source"); |
| 58 | + }); |
| 59 | + |
| 60 | + it("reloads GET fetcher RSC responses at the current document", () => { |
| 61 | + expect( |
| 62 | + getRSCReloadDocumentTarget( |
| 63 | + new Request("http://localhost/target?panel=notes"), |
| 64 | + "fetcher", |
| 65 | + ), |
| 66 | + ).toBe("http://localhost/current?tab=one#section"); |
| 67 | + }); |
| 68 | + |
| 69 | + it("reloads mutation RSC responses at the current document", () => { |
| 70 | + expect( |
| 71 | + getRSCReloadDocumentTarget( |
| 72 | + new Request("http://localhost/target", { method: "POST" }), |
| 73 | + ), |
| 74 | + ).toBe("http://localhost/current?tab=one#section"); |
| 75 | + }); |
| 76 | + |
| 77 | + it("reloads manifest mismatches at the pending navigation target", () => { |
| 78 | + // @ts-expect-error partial router global for this focused unit test |
| 79 | + window.__reactRouterDataRouter = { |
| 80 | + state: { |
| 81 | + navigation: { |
| 82 | + location: { |
| 83 | + pathname: "/target", |
| 84 | + search: "?panel=notes", |
| 85 | + hash: "#source", |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }; |
| 90 | + |
| 91 | + expect(getRSCManifestReloadDocumentTarget("/target")).toBe( |
| 92 | + "/target?panel=notes#source", |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + it("reloads fetcher manifest mismatches at the current document", () => { |
| 97 | + expect(getRSCManifestReloadDocumentTarget("/target", "fetcher")).toBe( |
| 98 | + "http://localhost/current?tab=one#section", |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + it("bounds reload attempts by target URL and client version", () => { |
| 103 | + // @ts-expect-error test-only router globals |
| 104 | + window.__reactRouterRSCVersion = "version-a"; |
| 105 | + expect(claimRSCReloadDocumentAttempt("/target")).toBe(true); |
| 106 | + expect(claimRSCReloadDocumentAttempt("/target")).toBe(false); |
| 107 | + |
| 108 | + // @ts-expect-error test-only router globals |
| 109 | + window.__reactRouterRSCVersion = "version-b"; |
| 110 | + expect(claimRSCReloadDocumentAttempt("/target")).toBe(true); |
| 111 | + expect(claimRSCReloadDocumentAttempt("/other")).toBe(true); |
| 112 | + |
| 113 | + clearRSCReloadDocumentAttempt(); |
| 114 | + expect(claimRSCReloadDocumentAttempt("/target")).toBe(true); |
| 115 | + }); |
| 116 | +}); |
0 commit comments