-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dom): DOM Element Assertion (#129)
Co-authored-by: matycarolina <[email protected]> Co-authored-by: Edwin Hernandez <[email protected]>
- Loading branch information
1 parent
5372460
commit c71daf5
Showing
5 changed files
with
343 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Assertion, AssertionError } from "@assertive-ts/core"; | ||
|
||
export class ElementAssertion<T extends Element> extends Assertion<T> { | ||
|
||
public constructor(actual: T) { | ||
super(actual); | ||
} | ||
|
||
/** | ||
* Check if the element is in the document. | ||
* | ||
* @returns the assertion instance. | ||
*/ | ||
public toBeInTheDocument(): this { | ||
const error = new AssertionError({ | ||
actual: this.actual, | ||
message: "Expected the element to be in the document", | ||
}); | ||
const invertedError = new AssertionError({ | ||
actual: this.actual, | ||
message: "Expected the element to NOT be in the document", | ||
}); | ||
|
||
return this.execute({ | ||
assertWhen: ( | ||
this.actual.ownerDocument.defaultView !== null | ||
&& this.actual.ownerDocument === this.actual.getRootNode({ composed: true }) | ||
), | ||
error, | ||
invertedError, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { AssertionError, expect } from "@assertive-ts/core"; | ||
import { render } from "@testing-library/react"; | ||
import { ReactElement } from "react"; | ||
|
||
import { ElementAssertion } from "../../../src/lib/ElementAssertion"; | ||
|
||
function TestComponent(): ReactElement { | ||
return ( | ||
<div> | ||
<button>click me</button> | ||
</div> | ||
); | ||
} | ||
|
||
describe("[Unit] ElementAssertion.test.ts", () => { | ||
describe(".toBeInTheDocument", () => { | ||
context("when the element is in the document", () => { | ||
it("returns the assertion instance", async () => { | ||
const { findByRole } = render(<TestComponent />); | ||
const button = await findByRole("button", { name: "click me" }); | ||
const test = new ElementAssertion(button); | ||
|
||
expect(test.toBeInTheDocument()).toBeEqual(test); | ||
|
||
expect(() => test.not.toBeInTheDocument()) | ||
.toThrowError(AssertionError) | ||
.toHaveMessage("Expected the element to NOT be in the document"); | ||
}); | ||
}); | ||
|
||
context("when the element is not in the document", () => { | ||
it("throws an assertion error", () => { | ||
const detachedElement = document.createElement("div"); | ||
|
||
const test = new ElementAssertion(detachedElement); | ||
|
||
expect(() => test.toBeInTheDocument()) | ||
.toThrowError(AssertionError) | ||
.toHaveMessage("Expected the element to be in the document"); | ||
|
||
expect(test.not.toBeInTheDocument()).toBeEqual(test); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.