Skip to content

Commit e512c76

Browse files
committed
Get test elements by Text
1 parent 8805f6f commit e512c76

File tree

3 files changed

+25
-30
lines changed

3 files changed

+25
-30
lines changed

packages/dom/src/lib/ElementAssertion.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export class ElementAssertion<T extends Element> extends Assertion<T> {
9494
/**
9595
* Asserts that the element has the specified class.
9696
*
97-
* @param className - The class name to check.
97+
* @param className The class name to check.
9898
* @returns the assertion instance.
9999
*/
100100
public toHaveClass(className: string): this {

packages/dom/test/unit/lib/ElementAssertion.test.tsx

+18-23
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,9 @@ describe("[Unit] ElementAssertion.test.ts", () => {
176176

177177
describe(".toHaveClass", () => {
178178
context("when the element has the expected class", () => {
179-
it("returns the assertion instance", async () => {
180-
const { findByTestId } = render(<HaveClassTestComponent />);
181-
const divTest = await findByTestId("classTest");
182-
divTest.classList.add("foo", "bar");
179+
it("returns the assertion instance", () => {
180+
const { getByText } = render(<HaveClassTestComponent className="foo bar" />);
181+
const divTest = getByText("Test text inside a div");
183182
const test = new ElementAssertion(divTest);
184183

185184
expect(test.toHaveClass("foo")).toBeEqual(test);
@@ -191,10 +190,9 @@ describe("[Unit] ElementAssertion.test.ts", () => {
191190
});
192191

193192
context("when the element does not have the expected class", () => {
194-
it("throws an assertion error", async () => {
195-
const { findByTestId } = render(<HaveClassTestComponent />);
196-
const divTest = await findByTestId("classTest");
197-
divTest.classList.add("foo", "bar");
193+
it("throws an assertion error", () => {
194+
const { getByText } = render(<HaveClassTestComponent className="foo bar" />);
195+
const divTest = getByText("Test text inside a div");
198196
const test = new ElementAssertion(divTest);
199197

200198
expect(() => test.toHaveClass("baz"))
@@ -208,10 +206,9 @@ describe("[Unit] ElementAssertion.test.ts", () => {
208206

209207
describe(".toHaveAnyClass", () => {
210208
context("when the element has at least one of the expected classes", () => {
211-
it("returns the assertion instance", async () => {
212-
const { findByTestId } = render(<HaveClassTestComponent />);
213-
const divTest = await findByTestId("classTest");
214-
divTest.classList.add("foo", "bar");
209+
it("returns the assertion instance", () => {
210+
const { getByText } = render(<HaveClassTestComponent className="foo bar" />);
211+
const divTest = getByText("Test text inside a div");
215212
const test = new ElementAssertion(divTest);
216213

217214
expect(test.toHaveAnyClass("bar", "baz")).toBeEqual(test);
@@ -223,10 +220,9 @@ describe("[Unit] ElementAssertion.test.ts", () => {
223220
});
224221

225222
context("when the element does not have any of the expected classes", () => {
226-
it("throws an assertion error", async () => {
227-
const { findByTestId } = render(<HaveClassTestComponent />);
228-
const divTest = await findByTestId("classTest");
229-
divTest.className = "foo";
223+
it("throws an assertion error", () => {
224+
const { getByText } = render(<HaveClassTestComponent className="foo" />);
225+
const divTest = getByText("Test text inside a div");
230226
const test = new ElementAssertion(divTest);
231227

232228
expect(() => test.toHaveAnyClass("bar", "baz"))
@@ -240,10 +236,9 @@ describe("[Unit] ElementAssertion.test.ts", () => {
240236

241237
describe(".toHaveAllClasses", () => {
242238
context("when the element has all the expected classes", () => {
243-
it("returns the assertion instance", async () => {
244-
const { findByTestId } = render(<HaveClassTestComponent />);
245-
const divTest = await findByTestId("classTest");
246-
divTest.classList.add("foo", "bar", "baz");
239+
it("returns the assertion instance", () => {
240+
const { getByText } = render(<HaveClassTestComponent className="foo bar baz" />);
241+
const divTest = getByText("Test text inside a div");
247242
const test = new ElementAssertion(divTest);
248243

249244
expect(test.toHaveAllClasses("foo", "bar")).toBeEqual(test);
@@ -255,9 +250,9 @@ describe("[Unit] ElementAssertion.test.ts", () => {
255250
});
256251

257252
context("when the element does not have all the expected classes", () => {
258-
it("throws an assertion error", async () => {
259-
const { findByTestId } = render(<HaveClassTestComponent />);
260-
const divTest = await findByTestId("classTest");
253+
it("throws an assertion error", () => {
254+
const { getByText } = render(<HaveClassTestComponent className="foo bar" />);
255+
const divTest = getByText("Test text inside a div");
261256
divTest.classList.add("foo", "bar");
262257
const test = new ElementAssertion(divTest);
263258

Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { ReactElement } from "react";
22

3-
export function HaveClassTestComponent(): ReactElement {
4-
return (
5-
<div data-testid="classTest">
6-
{"Test text inside a div"}
7-
</div>
8-
);
3+
export function HaveClassTestComponent({ className }: { className?: string; }): ReactElement {
4+
return (
5+
<div className={className}>
6+
{"Test text inside a div"}
7+
</div>
8+
);
99
}

0 commit comments

Comments
 (0)