Skip to content

Commit 02c109a

Browse files
committed
reorganising / refactoring
1 parent ce4ce12 commit 02c109a

File tree

5 files changed

+41
-6
lines changed

5 files changed

+41
-6
lines changed

tests/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
// the actual lib might not have, giving the false impression that something
44
// works while it might actually not, if you use the lib without babel-polyfill.
55
import "babel-regenerator-runtime";
6-
import "./react";
6+
import "./react/basic/index";
7+
import "./react/asd/index";

tests/react/Basic/Basic.js renamed to tests/react/basic/Basic/Basic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { Component } from "react";
2-
import { ContainerQuery } from "../../../packages/react-container-query/dist/bundle.esm";
2+
import { ContainerQuery } from "../../../../packages/react-container-query/dist/bundle.esm";
33
import { meta as rawMeta } from "./Basic.pcss";
44

55
// todo this shouldn't be needed
File renamed without changes.

tests/react/index.js renamed to tests/react/basic/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import {
77
changeRootSize,
88
expectTestComponentToHaveStyle,
99
expectTestComponentToHaveCustomProperties,
10-
expectElementToHaveStyle
11-
} from "../utils";
10+
expectElementToHaveStyle,
11+
clearDOM,
12+
expectTextContent
13+
} from "../../utils";
1214

1315
// Features covered:
1416
// - Style applying and restoring on width, height and orientation change
@@ -18,6 +20,7 @@ import {
1820
describe("Basic", () => {
1921
const refs = {};
2022
beforeAll(() => {
23+
clearDOM();
2124
renderTestComponent(<Basic />, {
2225
width: 100,
2326
height: 50
@@ -31,7 +34,7 @@ describe("Basic", () => {
3134
expect(getNodeText(refs.content)).toBe("1x1");
3235

3336
// Wait for resize observer to kick in
34-
await wait(() => expect(getNodeText(refs.content)).toBe("100x50"));
37+
await expectTextContent(refs.content, "100x50");
3538

3639
expectTestComponentToHaveCustomProperties({
3740
"--w": "100px",
@@ -107,6 +110,8 @@ describe("Basic", () => {
107110
// changing to portrait, as well as being bigger than 100x50
108111
await changeRootSize({ width: 200, height: 300 });
109112

113+
await expectTextContent(refs.content, "200x300");
114+
110115
expectTestComponentToHaveStyle({
111116
backgroundColor: "rgb(0, 128, 0)",
112117
color: "rgb(255, 255, 255)",

tests/utils/index.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import ReactDOM from "react-dom";
2-
import { getByTestId as rawGetByTestId } from "dom-testing-library";
2+
import {
3+
getByTestId as rawGetByTestId,
4+
getNodeText,
5+
wait
6+
} from "dom-testing-library";
37

48
let root = null;
59
let componentElement = null;
@@ -34,6 +38,8 @@ export const renderTestComponent = (
3438
return componentElement;
3539
};
3640

41+
export const clearDOM = () => (document.body.innerHTML = "");
42+
3743
/**
3844
* Changing the root size, which results in changing the size of the component
3945
* rendered inside.
@@ -96,3 +102,26 @@ export const expectElementToHaveCustomProperties = (element, props) => {
96102
*/
97103
export const expectTestComponentToHaveCustomProperties = props =>
98104
expectElementToHaveCustomProperties(componentElement, props);
105+
106+
/**
107+
* The reason we need this solution instead of just using wait() is because
108+
* jasmine's expect doesn't throw an exception on failure, but somehow communicates
109+
* that event back to the it() and describe() blocks, so that the test immediately
110+
* fails.
111+
*
112+
* @param {HTMLElement} element
113+
* @param {string} text
114+
* @return {Promise}
115+
*/
116+
export const expectTextContent = async (element, text) => {
117+
await wait(() => {
118+
const textContent = getNodeText(element);
119+
if (textContent !== text) {
120+
throw new Error(
121+
`Text content not yet what is expected. Got: ${textContent}. Expecting: ${text}.`
122+
);
123+
} else {
124+
expect(textContent).toBe(text);
125+
}
126+
});
127+
};

0 commit comments

Comments
 (0)