-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
102 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/System/Router/Utils/__tests__/serializeRelayHydrationData.jest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { SSRCache } from "react-relay-network-modern-ssr/lib/server" | ||
import { serializeRelayHydrationData } from "System/Router/Utils/serializeRelayHydrationData" | ||
import mockSerialize from "serialize-javascript" | ||
|
||
jest.mock("serialize-javascript", () => jest.fn()) | ||
|
||
describe("serializeRelayHydrationData", () => { | ||
afterEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
it("should serialize the relay data correctly", () => { | ||
const mockData = ([ | ||
[ | ||
{ | ||
id: 1, | ||
_res: "some-network-response", | ||
data: "value1", | ||
}, | ||
{ | ||
id: 2, | ||
_res: "another-response", | ||
data: "value2", | ||
}, | ||
], | ||
] as unknown) as SSRCache | ||
|
||
mockSerialize.mockImplementation((data, options) => JSON.stringify(data)) | ||
|
||
const result = serializeRelayHydrationData(mockData) | ||
|
||
expect(mockSerialize).toHaveBeenCalledTimes(2) | ||
expect(mockSerialize).toHaveBeenCalledWith(mockData, { isJSON: true }) | ||
expect(mockSerialize).toHaveBeenCalledWith(JSON.stringify(mockData), { | ||
isJSON: true, | ||
}) | ||
|
||
expect(result).toBe(JSON.stringify(JSON.stringify(mockData))) | ||
expect(mockData[0][0]).not.toHaveProperty("_res") | ||
expect(mockData[0][1]).not.toHaveProperty("_res") | ||
}) | ||
|
||
it("should handle empty data gracefully", () => { | ||
mockSerialize.mockImplementation((data, options) => JSON.stringify(data)) | ||
|
||
const result = serializeRelayHydrationData([]) | ||
|
||
expect(mockSerialize).toHaveBeenCalledTimes(2) | ||
expect(mockSerialize).toHaveBeenCalledWith([], { isJSON: true }) | ||
expect(mockSerialize).toHaveBeenCalledWith(JSON.stringify([]), { | ||
isJSON: true, | ||
}) | ||
|
||
expect(result).toBe(JSON.stringify(JSON.stringify([]))) | ||
}) | ||
|
||
it("should return an empty serialized array when an error occurs", () => { | ||
const mockData = ([ | ||
[ | ||
{ | ||
id: 1, | ||
_res: "some-network-response", | ||
data: "value1", | ||
}, | ||
], | ||
] as unknown) as SSRCache | ||
|
||
mockSerialize.mockImplementation(() => { | ||
throw new Error("Serialization Error") | ||
}) | ||
|
||
expect(() => { | ||
const result = serializeRelayHydrationData(mockData) | ||
expect(mockSerialize).toHaveBeenCalledTimes(1) | ||
expect(result).toBe(mockSerialize("[]", { isJSON: true })) | ||
}).toThrowError("Serialization Error") | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters