Skip to content

Commit c71daf5

Browse files
ChristianSamamatycarolinafonsiher
authored
feat(dom): DOM Element Assertion (#129)
Co-authored-by: matycarolina <contrerasmarialejandra1@gmail.com> Co-authored-by: Edwin Hernandez <edhernandez@stackbuilders.com>
1 parent 5372460 commit c71daf5

File tree

5 files changed

+343
-2
lines changed

5 files changed

+343
-2
lines changed

packages/dom/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,20 @@
4040
},
4141
"devDependencies": {
4242
"@assertive-ts/core": "workspace:^",
43+
"@testing-library/dom": "^10.1.0",
44+
"@testing-library/react": "^16.0.0",
4345
"@types/jsdom-global": "^3",
4446
"@types/mocha": "^10.0.6",
4547
"@types/node": "^20.11.19",
48+
"@types/react": "^18.3.3",
49+
"@types/react-dom": "^18.3.0",
50+
"@types/react-test-renderer": "^18",
4651
"jsdom": "^24.0.0",
4752
"jsdom-global": "^3.0.2",
4853
"mocha": "^10.3.0",
54+
"react": "^18.3.1",
55+
"react-dom": "^18.3.1",
56+
"react-test-renderer": "^18.3.1",
4957
"semantic-release": "^23.0.2",
5058
"semantic-release-yarn": "^3.0.2",
5159
"ts-node": "^10.9.2",
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Assertion, AssertionError } from "@assertive-ts/core";
2+
3+
export class ElementAssertion<T extends Element> extends Assertion<T> {
4+
5+
public constructor(actual: T) {
6+
super(actual);
7+
}
8+
9+
/**
10+
* Check if the element is in the document.
11+
*
12+
* @returns the assertion instance.
13+
*/
14+
public toBeInTheDocument(): this {
15+
const error = new AssertionError({
16+
actual: this.actual,
17+
message: "Expected the element to be in the document",
18+
});
19+
const invertedError = new AssertionError({
20+
actual: this.actual,
21+
message: "Expected the element to NOT be in the document",
22+
});
23+
24+
return this.execute({
25+
assertWhen: (
26+
this.actual.ownerDocument.defaultView !== null
27+
&& this.actual.ownerDocument === this.actual.getRootNode({ composed: true })
28+
),
29+
error,
30+
invertedError,
31+
});
32+
}
33+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { AssertionError, expect } from "@assertive-ts/core";
2+
import { render } from "@testing-library/react";
3+
import { ReactElement } from "react";
4+
5+
import { ElementAssertion } from "../../../src/lib/ElementAssertion";
6+
7+
function TestComponent(): ReactElement {
8+
return (
9+
<div>
10+
<button>click me</button>
11+
</div>
12+
);
13+
}
14+
15+
describe("[Unit] ElementAssertion.test.ts", () => {
16+
describe(".toBeInTheDocument", () => {
17+
context("when the element is in the document", () => {
18+
it("returns the assertion instance", async () => {
19+
const { findByRole } = render(<TestComponent />);
20+
const button = await findByRole("button", { name: "click me" });
21+
const test = new ElementAssertion(button);
22+
23+
expect(test.toBeInTheDocument()).toBeEqual(test);
24+
25+
expect(() => test.not.toBeInTheDocument())
26+
.toThrowError(AssertionError)
27+
.toHaveMessage("Expected the element to NOT be in the document");
28+
});
29+
});
30+
31+
context("when the element is not in the document", () => {
32+
it("throws an assertion error", () => {
33+
const detachedElement = document.createElement("div");
34+
35+
const test = new ElementAssertion(detachedElement);
36+
37+
expect(() => test.toBeInTheDocument())
38+
.toThrowError(AssertionError)
39+
.toHaveMessage("Expected the element to be in the document");
40+
41+
expect(test.not.toBeInTheDocument()).toBeEqual(test);
42+
});
43+
});
44+
});
45+
});

packages/dom/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"extends": "../../tsconfig.json",
33
"compilerOptions": {
44
"lib": ["DOM", "DOM.Iterable"],
5+
"jsx": "react-jsx",
56
"outDir": "./build",
67
"typeRoots": [
78
"../../node_modules/@types/",

0 commit comments

Comments
 (0)