Skip to content

Commit 297763d

Browse files
test(ui): add render html utils (#570)
1 parent 6b083d3 commit 297763d

File tree

4 files changed

+64
-39
lines changed

4 files changed

+64
-39
lines changed

packages/~/app/ui/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
"./table": {
3838
"default": "./src/table/index.ts"
3939
},
40+
"./testing": {
41+
"default": "./src/testing/index.ts"
42+
},
4043
"./visually_hidden": {
4144
"default": "./src/visually_hidden/index.ts"
4245
},
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
3+
import type { Child } from "hono/jsx";
4+
import { renderToReadableStream } from "hono/jsx/dom/server";
5+
import { format } from "prettier";
6+
7+
//
8+
9+
export async function renderHTML(element: Child) {
10+
const textDecoder = new TextDecoder();
11+
const getStringFromStream = async (
12+
stream: ReadableStream<Uint8Array>,
13+
): Promise<string> => {
14+
const reader = stream.getReader();
15+
let str = "";
16+
for (;;) {
17+
const { done, value } = await reader.read();
18+
if (done) {
19+
break;
20+
}
21+
str += textDecoder.decode(value);
22+
}
23+
return str;
24+
};
25+
return format(
26+
await getStringFromStream(await renderToReadableStream(element)),
27+
{ parser: "html" },
28+
);
29+
}
Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,38 @@
11
//
22

3+
import { renderHTML } from "@~/app.ui/testing";
34
import { expect, test } from "bun:test";
4-
import { Hono } from "hono";
5-
import { jsxRenderer } from "hono/jsx-renderer";
65
import { About } from "./About";
76

87
test("render about section", async () => {
9-
const response = await new Hono()
10-
.get("/", jsxRenderer(), ({ render }) =>
11-
render(
12-
<About
13-
organization={{
14-
cached_activite_principale: "cached_activite_principale",
15-
cached_adresse: "cached_adresse",
16-
cached_categorie_juridique: "cached_categorie_juridique",
17-
cached_code_officiel_geographique:
18-
"cached_code_officiel_geographique",
19-
cached_code_postal: "cached_code_postal",
20-
cached_enseigne: "cached_enseigne",
21-
cached_est_active: true,
22-
cached_etat_administratif: "cached_etat_administratif",
23-
cached_libelle_activite_principale:
24-
"cached_libelle_activite_principale",
25-
cached_libelle_categorie_juridique:
26-
"cached_libelle_categorie_juridique",
27-
cached_libelle_tranche_effectif: "cached_libelle_tranche_effectif",
28-
cached_libelle: "cached_libelle",
29-
cached_nom_complet: "cached_nom_complet",
30-
cached_tranche_effectifs: "cached_tranche_effectifs",
31-
created_at: "2011-11-22 14:34:34.000Z",
32-
id: 42,
33-
siret: "siret",
34-
updated_at: "2011-11-15T13:48:00.000Z",
35-
}}
36-
/>,
37-
),
38-
)
39-
.request("/");
40-
expect(response.status).toBe(200);
41-
// expect(
42-
// await format(await response.text(), { parser: "html" }),
43-
// ).toMatchSnapshot();
8+
expect(
9+
await renderHTML(
10+
<About
11+
organization={{
12+
cached_activite_principale: "cached_activite_principale",
13+
cached_adresse: "cached_adresse",
14+
cached_categorie_juridique: "cached_categorie_juridique",
15+
cached_code_postal: "cached_code_postal",
16+
cached_est_active: true,
17+
cached_nom_complet: "cached_nom_complet",
18+
created_at: "2011-11-22 14:34:34.000Z",
19+
id: 42,
20+
siret: "siret",
21+
cached_code_officiel_geographique:
22+
"cached_code_officiel_geographique",
23+
updated_at: "2011-11-15T13:48:00.000Z",
24+
cached_libelle_activite_principale:
25+
"cached_libelle_activite_principale",
26+
cached_libelle_categorie_juridique:
27+
"cached_libelle_categorie_juridique",
28+
cached_libelle_tranche_effectif: "cached_libelle_tranche_effectif",
29+
cached_libelle: "cached_libelle",
30+
cached_enseigne: "cached_enseigne",
31+
cached_tranche_effectifs: "cached_tranche_effectifs",
32+
cached_etat_administratif: "cached_etat_administratif",
33+
}}
34+
id="about_section"
35+
/>,
36+
),
37+
).toMatchSnapshot();
4438
});

packages/~/organizations/ui/src/info/__snapshots__/About.test.tsx.snap

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// Bun Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`render about section 1`] = `
4-
"<!doctype html>
5-
<section>
4+
"<section id="about_section">
65
<ul class="list-none pl-0">
76
<li>
87
Creation de l&#39;organisation :

0 commit comments

Comments
 (0)