Skip to content

Commit c24d0c6

Browse files
committed
Add failing test for Map and Set order
When the loader data contains a `Map` or `Set` object, the data maintains the correct order in the server-rendered response. However, once the page hydrates, the order of the items in the `Map` or `Set` are reversed, leading to a hydration error from React.
1 parent bf2bc05 commit c24d0c6

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

integration/bug-report-test.ts

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,20 @@ test.beforeAll(async () => {
6464
// `createFixture` will make an app and run your tests against it.
6565
////////////////////////////////////////////////////////////////////////////
6666
files: {
67-
"app/routes/_index.tsx": js`
68-
import { useLoaderData, Link } from "react-router";
69-
67+
"app/routes/map.tsx": js`
7068
export function loader() {
71-
return "pizza";
69+
return new Map([[1, 1], [2, 2], [3, 3]]);
7270
}
73-
74-
export default function Index() {
75-
let data = useLoaderData();
76-
return (
77-
<div>
78-
{data}
79-
<Link to="/burgers">Other Route</Link>
80-
</div>
81-
)
71+
export default function Map({ loaderData }) {
72+
return <div>{JSON.stringify(Array.from(loaderData.entries()))}</div>;
8273
}
8374
`,
84-
85-
"app/routes/burgers.tsx": js`
86-
export default function Index() {
87-
return <div>cheeseburger</div>;
75+
"app/routes/set.tsx": js`
76+
export function loader() {
77+
return new Set([1, 2, 3]);
78+
}
79+
export default function Set({ loaderData }) {
80+
return <div>{JSON.stringify(Array.from(loaderData.values()))}</div>;
8881
}
8982
`,
9083
},
@@ -103,16 +96,19 @@ test.afterAll(() => {
10396
// add a good description for what you expect React Router to do 👇🏽
10497
////////////////////////////////////////////////////////////////////////////////
10598

106-
test("[description of what you expect it to do]", async ({ page }) => {
99+
test("Maintains correct order of Map objects when hydrating", async ({
100+
page,
101+
}) => {
107102
let app = new PlaywrightFixture(appFixture, page);
108103
// You can test any request your app might get using `fixture`.
109-
let response = await fixture.requestDocument("/");
110-
expect(await response.text()).toMatch("pizza");
104+
let response = await fixture.requestDocument("/map");
105+
expect(await response.text()).toMatch("[[1,1],[2,2],[3,3]]");
111106

112107
// If you need to test interactivity use the `app`
113-
await app.goto("/");
114-
await app.clickLink("/burgers");
115-
await page.waitForSelector("text=cheeseburger");
108+
await app.goto("/map", true);
109+
110+
let html = await app.getHtml();
111+
expect(html).toMatch("[[1,1],[2,2],[3,3]]");
116112

117113
// If you're not sure what's going on, you can "poke" the app, it'll
118114
// automatically open up in your browser for 20 seconds, so be quick!
@@ -121,6 +117,19 @@ test("[description of what you expect it to do]", async ({ page }) => {
121117
// Go check out the other tests to see what else you can do.
122118
});
123119

120+
test("Maintains correct order of Set objects when hydrating", async ({
121+
page,
122+
}) => {
123+
let app = new PlaywrightFixture(appFixture, page);
124+
let response = await fixture.requestDocument("/set");
125+
expect(await response.text()).toMatch("[1,2,3]");
126+
127+
await app.goto("/set", true);
128+
129+
let html = await app.getHtml();
130+
expect(html).toMatch("[1,2,3]");
131+
});
132+
124133
////////////////////////////////////////////////////////////////////////////////
125134
// 💿 Finally, push your changes to your fork of React Router
126135
// and open a pull request!

0 commit comments

Comments
 (0)