Skip to content

Commit f3ea0c6

Browse files
authored
fix: landing page link to playground didn't have any code in it (#1270)
1 parent 2366fb2 commit f3ea0c6

3 files changed

Lines changed: 82 additions & 5 deletions

File tree

__tests__/LandingPage_.test.res

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
open ReactRouter
2+
open Vitest
3+
4+
let expectedExample = `module Button = {
5+
@react.component
6+
let make = (~count) => {
7+
let times = switch count {
8+
| 1 => "once"
9+
| 2 => "twice"
10+
| n => n->Int.toString ++ " times"
11+
}
12+
let text = \`Click me $\{times\}\`
13+
14+
<button> {text->React.string} </button>
15+
}
16+
}`
17+
18+
test(
19+
"landing page playground link uses compressed code that the playground can decode",
20+
async () => {
21+
let screen = await render(
22+
<MemoryRouter initialEntries=["/"]>
23+
<LandingPage />
24+
</MemoryRouter>,
25+
)
26+
27+
let _ = await screen->getByText("Edit this example in Playground")
28+
29+
let href = switch document->WebAPI.Document.querySelector("a[href*='/try?code=']") {
30+
| Value(link) =>
31+
switch link->WebAPI.Element.getAttribute("href") {
32+
| Value(href) => href
33+
| Null => failwith("expected landing page playground link to have an href")
34+
}
35+
| Null => failwith("expected to find the landing page playground link")
36+
}
37+
38+
let {pathname, searchParams} = WebAPI.URL.make(~url=href, ~base="https://rescript-lang.org")
39+
40+
expect(pathname)->toBe("/try")
41+
42+
let compressedCode =
43+
searchParams->WebAPI.URLSearchParams.get("code")->Nullable.make->Nullable.toOption
44+
45+
let decodedCode =
46+
compressedCode
47+
->Option.getOrThrow
48+
->LzString.lzString.decompressFromEncodedURIComponent
49+
->Nullable.make
50+
->Nullable.toOption
51+
52+
expect(decodedCode->Option.isSome)->toBe(true)
53+
expect(decodedCode->Option.getOrThrow)->toBe(expectedExample)
54+
},
55+
)

app/routes/LandingPage.res

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ module PlaygroundHero = {
5757
js: `import * as JsxRuntime from "react/jsx-runtime";
5858
5959
function Playground$Button(props) {
60-
var count = props.count;
61-
var times = count !== 1 ? (
60+
let count = props.count;
61+
let times = count !== 1 ? (
6262
count !== 2 ? count.toString() + " times" : "twice"
6363
) : "once";
64-
var text = "Click me " + times;
64+
let text = "Click me " + times;
6565
return JsxRuntime.jsx("button", {
6666
children: text
6767
});
6868
}
6969
70-
var Button = {
70+
let Button = {
7171
make: Playground$Button
7272
};
7373
@@ -118,7 +118,7 @@ export {
118118
/* ---Link to Playground--- */
119119
<div>
120120
<ReactRouter.Link.String
121-
to={`/try?code=${encodeURIComponent(example.res)}`}
121+
to={`/try?code=${LzString.lzString.compressToEncodedURIComponent(example.res)}`}
122122
className="captions md:px-0 border-b border-gray-40 hover:border-gray-60 text-gray-60"
123123
>
124124
{React.string("Edit this example in Playground")}

e2e/Playground.cy.res

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,28 @@ describe("Playground", () => {
6565
->ignore
6666
})
6767

68+
it("should open the landing page example in the playground with code and compiled output", () => {
69+
containsSelector("a", "Edit this example in Playground")
70+
->scrollIntoView
71+
->should("be.visible")
72+
->click
73+
->ignore
74+
75+
url()->shouldInclude("/try?code=")->ignore
76+
waitForPlayground()
77+
78+
get(".cm-content")
79+
->shouldContainText("module Button = {")
80+
->shouldContainText("Click me")
81+
->ignore
82+
83+
get("pre.whitespace-pre-wrap")
84+
->should("be.visible")
85+
->shouldContainText("react/jsx-runtime")
86+
->shouldContainText("Click me")
87+
->ignore
88+
})
89+
6890
it("should switch to light mode from toast and back to dark mode in settings", () => {
6991
// Navigate to playground and wait for initial render
7092
clickNavLink(~testId="navbar-primary-left-content", ~text="Playground")

0 commit comments

Comments
 (0)