Skip to content

Commit 29cfb98

Browse files
committed
tests: increase test coverage
we've gone from 63% line coverage to 90%
1 parent d816dbc commit 29cfb98

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

src/__tests__/__snapshots__/tests.ts.snap

+27-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,36 @@
22

33
exports[`reactFromHtml E2E tests Should hydrate a basic component 1`] = `"<div class=\\"hydration-root\\" data-hydration-id=\\"1\\"><span>hydrated component</span></div>"`;
44
5-
exports[`reactFromHtml E2E tests Should hydrate components with custom query selectors 1`] = `"<div class=\\"hydration-root\\" data-hydration-id=\\"4\\"><span>hydrated component</span></div>"`;
5+
exports[`reactFromHtml E2E tests Should hydrate components with custom query selectors 1`] = `"<div class=\\"hydration-root\\" data-hydration-id=\\"5\\"><span>hydrated component</span></div>"`;
6+
7+
exports[`reactFromHtml E2E tests Should not mutate static markup 1`] = `
8+
"
9+
<div>
10+
<p>Static HTML, with some <b style=\\"font-weight:bold;\\">interesting</b> markup</p>
11+
<div data-hydratable=\\"test-myComponent\\"></div>
12+
</div>"
13+
`;
14+
15+
exports[`reactFromHtml E2E tests Should not mutate static markup of nested children 1`] = `
16+
"
17+
<div class=\\"hydration-root\\" data-hydration-id=\\"6\\"><span>
18+
<p>Static HTML, with some <b style=\\"font-weight:bold;\\">interesting</b> markup</p>
19+
<form>
20+
<input type=\\"checkbox\\" name=\\"confirm\\" value=\\"confirmed\\" checked=\\"\\">
21+
<select id=\\"favourite-color\\">
22+
<option value=\\"red\\">red</option>
23+
<option value=\\"green\\" selected=\\"\\">green</option>
24+
<option value=\\"blue\\">blue</option>
25+
</select>
26+
<textarea name=\\"message\\">Default value</textarea>
27+
</form>
28+
</span></div>
29+
"
30+
`;
631
732
exports[`reactFromHtml E2E tests Should work for nested hydratables 1`] = `
833
"
9-
<div class=\\"hydration-root\\" data-hydration-id=\\"2\\"><span>
34+
<div class=\\"hydration-root\\" data-hydration-id=\\"3\\"><span>
1035
<b>
1136
Hello, World!
1237
</b>

src/__tests__/tests.ts

+74
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ describe("reactFromHtml E2E tests", async () => {
2121
expect(documentElement.innerHTML).toMatchSnapshot();
2222
});
2323

24+
it("Should not re-hydrate already hydrated components on multiple `reactFromHtml()` calls", async () => {
25+
const componentName: string = "myComponent";
26+
27+
const mockCall = jest.fn();
28+
const hydrator = async () => {
29+
mockCall();
30+
31+
return React.createElement("span", {}, "hydrated component");
32+
};
33+
34+
const hydrators = { [`.${componentName}`]: hydrator };
35+
const documentElement = document.createElement("div");
36+
37+
documentElement.innerHTML = `<div class="${componentName}"></div>`;
38+
39+
await reactFromHtml(documentElement, hydrators);
40+
await reactFromHtml(documentElement, hydrators);
41+
expect(mockCall).toBeCalledTimes(1);
42+
});
43+
2444
it("Should work for nested hydratables", async () => {
2545
const mockCall = jest.fn();
2646
const hydrators: IHydrator = {
@@ -71,4 +91,58 @@ describe("reactFromHtml E2E tests", async () => {
7191

7292
expect(documentElement.innerHTML).toMatchSnapshot();
7393
});
94+
95+
it("Should not mutate static markup", async () => {
96+
const componentName: string = "myComponent";
97+
98+
const hydrator = async () => {
99+
return React.createElement("span", {}, "hydrated component");
100+
};
101+
102+
const hydrators = { [`.${componentName}`]: hydrator };
103+
const documentElement = document.createElement("div");
104+
105+
documentElement.innerHTML = `
106+
<div>
107+
<p>Static HTML, with some <b style="font-weight:bold;">interesting</b> markup</p>
108+
<div data-hydratable="test-${componentName}"></div>
109+
</div>`;
110+
111+
await reactFromHtml(documentElement, hydrators);
112+
113+
expect(documentElement.innerHTML).toMatchSnapshot();
114+
});
115+
116+
it("Should not mutate static markup of nested children", async () => {
117+
const mockCall = jest.fn();
118+
const hydrators: IHydrator = {
119+
[`.Parent`]: async (el: Element, hydrate) => {
120+
mockCall();
121+
122+
return React.createElement("span", {}, await hydrate(el));
123+
},
124+
};
125+
126+
const documentElement = document.createElement("div");
127+
128+
documentElement.innerHTML = `
129+
<div class="Parent">
130+
<p>Static HTML, with some <b style="font-weight:bold;">interesting</b> markup</p>
131+
<form>
132+
<input type="checkbox" name="confirm" value="confirmed" checked>
133+
<select id="favourite-color">
134+
<option value="red">red</option>
135+
<option value="green" selected>green</option>
136+
<option value="blue">blue</option>
137+
</select>
138+
<textarea name="message">Default value</textarea>
139+
</form>
140+
</div>
141+
`;
142+
143+
await reactFromHtml(documentElement, hydrators);
144+
145+
expect(documentElement.innerHTML).toMatchSnapshot();
146+
expect(mockCall).toBeCalledTimes(1);
147+
});
74148
});

0 commit comments

Comments
 (0)