@thisisagile/easy-test-react
Version:
Straightforward wrapper library for testing-library/react
71 lines (67 loc) • 3.2 kB
JavaScript
import {
waitForRender
} from "./chunk-UIR5DDLV.mjs";
// src/Tester.ts
import { render, screen } from "@testing-library/react";
// src/ElementTester.ts
import { fireEvent, waitFor, waitForElementToBeRemoved } from "@testing-library/react";
import { isDefined, tryTo } from "@thisisagile/easy";
var ElementTester = class {
constructor(element) {
this.element = element;
}
get value() {
return this.element()?.value;
}
get isValid() {
return tryTo(() => this.element()).is.defined().map(() => true).or(false);
}
get then() {
return new Tester(this.element());
}
click = () => this.element() && fireEvent.click(this.element()) ? this : void 0;
awaitClick = () => waitFor(() => fireEvent.click(this.element()));
keyDown = (key) => this.element() && fireEvent.keyDown(this.element(), { key }) ? this : void 0;
mouseDown = (index) => this.element() && fireEvent.mouseDown(isDefined(index) ? this.element().children[index] : this.element()) ? this : void 0;
pressEnter = () => this.keyDown("Enter");
clear = () => this.type("");
type = (value) => fireEvent.change(this.element(), { target: { value } });
wait = () => waitFor(this.element);
waitForRemove = () => waitForElementToBeRemoved(this.element);
};
// src/Tester.ts
var Tester = class _Tester {
constructor(container) {
this.container = container;
}
static render = (component) => waitForRender(component).then((c) => new _Tester(c.container));
static renderSync = (component) => new _Tester(render(component).container);
byText = (text, index) => index ? screen.getAllByText(text)[index] : screen.getByText(text);
atText = (text, index) => new ElementTester(() => this.byText(text, index));
byId = (id, index) => index ? screen.getAllByTestId(id.toString())[index] : screen.getByTestId(id.toString());
atId = (id, index) => new ElementTester(() => this.byId(id, index));
byRole = (role, index) => index ? screen.getAllByRole(role)[index] : screen.getByRole(role);
atRole = (role, index) => new ElementTester(() => this.byRole(role, index));
byRow = (index) => index ? screen.getAllByRole("row")[index] : screen.getByRole("row");
atRow = (index) => new ElementTester(() => this.byRow(index));
byTitle = (title, index) => index ? screen.getAllByTitle(title)[index] : screen.getByTitle(title);
atTitle = (title, index) => new ElementTester(() => this.byTitle(title, index));
byPlaceholder = (placeholder, index) => index ? screen.getAllByPlaceholderText(placeholder)[index] : screen.getByPlaceholderText(placeholder);
atPlaceholder = (placeholder, index) => new ElementTester(() => this.byPlaceholder(placeholder, index));
byQuery = (query, index = 0) => {
const elements = this.container.querySelectorAll(query);
return elements[index];
};
atQuery = (query, index) => new ElementTester(() => this.byQuery(query, index));
submit = (id = "btn-submit") => this.atId(id);
debug = () => screen.debug();
};
var rendersWait = async (component) => await Tester.render(component);
var renders = (component) => Tester.renderSync(component);
export {
Tester,
rendersWait,
renders,
ElementTester
};
//# sourceMappingURL=chunk-NDS2MSSJ.mjs.map