Skip to content

Commit bd02b3a

Browse files
committed
Apply fix for bug in JSDOM to enable styles to correctly be applied from dynamically added style tags in document.head
1 parent 9e216f0 commit bd02b3a

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

src/components/tests/Common.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ const mutateComponent = (component: React.ReactNode, newProps: Object) => {
88
return React.cloneElement(component as React.DetailedReactHTMLElement<any, HTMLElement>, newProps);
99
};
1010

11+
export const fixUp = (sut: HTMLElement) => {
12+
// This is annoying. A bug in JSDOM means that getComputedStyle() on test elements does
13+
// not correctly return the applied styles when a dynamically added style tag is added to
14+
// the document.head. It doesn't happen in all cases. Quick fix for now is take the style
15+
// from the doc head and apply directly to the test element
16+
// Existing issue raised on JSDOM repo - https://github.com/jsdom/jsdom/issues/2986
17+
const test = document.documentElement.querySelector(`#style_${sut.id}`)!;
18+
const style = test.innerHTML
19+
.replace(`#${sut.id} { `, "")
20+
.split("#")[0]
21+
.replace(" }", "");
22+
sut.setAttribute("style", style);
23+
24+
return sut;
25+
};
26+
1127
export const commonPropsTests = (name: string, component: React.ReactNode, expectedStyle: Partial<CSSStyleDeclaration>) => {
1228
test(`${name} default has correct styles`, async () => {
1329
// arrange, act
@@ -136,8 +152,8 @@ export const commonAnchorTests = (
136152
{mutateComponent(component, { id: "test1", size: 100, order: 1 })}
137153
</ViewPort>,
138154
);
139-
const sut = container.querySelector("#test")!;
140-
const sut1 = container.querySelector("#test1")!;
155+
const sut = fixUp(container.querySelector("#test")!);
156+
const sut1 = fixUp(container.querySelector("#test1")!);
141157

142158
// assert
143159
const style = window.getComputedStyle(sut);
@@ -173,8 +189,8 @@ export const commonAnchorTests = (
173189
{mutateComponent(component, { id: "test", size: 50, order: 0 })}
174190
</ViewPort>,
175191
);
176-
const sut = container.querySelector("#test")!;
177-
const sut1 = container.querySelector("#test1")!;
192+
const sut = fixUp(container.querySelector("#test")!);
193+
const sut1 = fixUp(container.querySelector("#test1")!);
178194

179195
// assert
180196
const style = window.getComputedStyle(sut);

src/components/tests/__tests__/Fill.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Fill } from "../../Fill";
44
import { Left, Top, Right, Bottom, LeftResizable, TopResizable, RightResizable, BottomResizable } from "../../Anchored";
55
import "@testing-library/jest-dom/extend-expect";
66
import { ViewPort } from "../../ViewPort";
7-
import { commonPropsTests } from "../Common";
7+
import { commonPropsTests, fixUp } from "../Common";
88

99
afterEach(cleanup);
1010

@@ -197,7 +197,8 @@ test("Fill with LeftResizable has correct styles", async () => {
197197
<Fill id="test" />
198198
</ViewPort>,
199199
);
200-
const sut = container.querySelector("#test")!;
200+
201+
const sut = fixUp(container.querySelector("#test")!);
201202
const style = window.getComputedStyle(sut);
202203

203204
expect(style.display).toBe("block");
@@ -219,7 +220,7 @@ test("Fill with stacked LeftResizable has correct styles", async () => {
219220
<Fill id="test" />
220221
</ViewPort>,
221222
);
222-
const sut = container.querySelector("#test")!;
223+
const sut = fixUp(container.querySelector("#test")!);
223224
const style = window.getComputedStyle(sut);
224225

225226
expect(style.display).toBe("block");
@@ -240,7 +241,7 @@ test("Fill with TopResizable has correct styles", async () => {
240241
<Fill id="test" />
241242
</ViewPort>,
242243
);
243-
const sut = container.querySelector("#test")!;
244+
const sut = fixUp(container.querySelector("#test")!);
244245
const style = window.getComputedStyle(sut);
245246

246247
expect(style.display).toBe("block");
@@ -262,7 +263,7 @@ test("Fill with stacked TopResizable has correct styles", async () => {
262263
<Fill id="test" />
263264
</ViewPort>,
264265
);
265-
const sut = container.querySelector("#test")!;
266+
const sut = fixUp(container.querySelector("#test")!);
266267
const style = window.getComputedStyle(sut);
267268

268269
expect(style.display).toBe("block");
@@ -283,7 +284,7 @@ test("Fill with RightResizable has correct styles", async () => {
283284
<Fill id="test" />
284285
</ViewPort>,
285286
);
286-
const sut = container.querySelector("#test")!;
287+
const sut = fixUp(container.querySelector("#test")!);
287288
const style = window.getComputedStyle(sut);
288289

289290
expect(style.display).toBe("block");
@@ -305,7 +306,7 @@ test("Fill with stacked RightResizable has correct styles", async () => {
305306
<Fill id="test" />
306307
</ViewPort>,
307308
);
308-
const sut = container.querySelector("#test")!;
309+
const sut = fixUp(container.querySelector("#test")!);
309310
const style = window.getComputedStyle(sut);
310311

311312
expect(style.display).toBe("block");
@@ -326,7 +327,7 @@ test("Fill with BottomResizable has correct styles", async () => {
326327
<Fill id="test" />
327328
</ViewPort>,
328329
);
329-
const sut = container.querySelector("#test")!;
330+
const sut = fixUp(container.querySelector("#test")!);
330331
const style = window.getComputedStyle(sut);
331332

332333
expect(style.display).toBe("block");
@@ -348,7 +349,7 @@ test("Fill with stacked BottomResizable has correct styles", async () => {
348349
<Fill id="test" />
349350
</ViewPort>,
350351
);
351-
const sut = container.querySelector("#test")!;
352+
const sut = fixUp(container.querySelector("#test")!);
352353
const style = window.getComputedStyle(sut);
353354

354355
expect(style.display).toBe("block");

0 commit comments

Comments
 (0)