@thisisagile/easy-test-react
Version:
Straightforward wrapper library for testing-library/react
120 lines (113 loc) • 5.28 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
ElementTester: () => ElementTester,
Tester: () => Tester,
mockTimezone: () => mockTimezone,
renders: () => renders,
rendersWait: () => rendersWait,
waitForRender: () => waitForRender
});
module.exports = __toCommonJS(src_exports);
// src/ElementTester.ts
var import_react4 = require("@testing-library/react");
var import_easy = require("@thisisagile/easy");
// src/Tester.ts
var import_react3 = require("@testing-library/react");
// src/waitForRender.ts
var import_react = require("@testing-library/react");
var import_react2 = require("react");
async function waitForRender(ui, options) {
let r = {};
await (0, import_react2.act)(async () => {
r = (0, import_react.render)(ui, options);
await (0, import_react.waitFor)(() => r.container, { container: r.container });
});
return r;
}
// 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((0, import_react3.render)(component).container);
byText = (text, index) => index ? import_react3.screen.getAllByText(text)[index] : import_react3.screen.getByText(text);
atText = (text, index) => new ElementTester(() => this.byText(text, index));
byId = (id, index) => index ? import_react3.screen.getAllByTestId(id.toString())[index] : import_react3.screen.getByTestId(id.toString());
atId = (id, index) => new ElementTester(() => this.byId(id, index));
byRole = (role, index) => index ? import_react3.screen.getAllByRole(role)[index] : import_react3.screen.getByRole(role);
atRole = (role, index) => new ElementTester(() => this.byRole(role, index));
byRow = (index) => index ? import_react3.screen.getAllByRole("row")[index] : import_react3.screen.getByRole("row");
atRow = (index) => new ElementTester(() => this.byRow(index));
byTitle = (title, index) => index ? import_react3.screen.getAllByTitle(title)[index] : import_react3.screen.getByTitle(title);
atTitle = (title, index) => new ElementTester(() => this.byTitle(title, index));
byPlaceholder = (placeholder, index) => index ? import_react3.screen.getAllByPlaceholderText(placeholder)[index] : import_react3.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 = () => import_react3.screen.debug();
};
var rendersWait = async (component) => await Tester.render(component);
var renders = (component) => Tester.renderSync(component);
// src/ElementTester.ts
var ElementTester = class {
constructor(element) {
this.element = element;
}
get value() {
return this.element()?.value;
}
get isValid() {
return (0, import_easy.tryTo)(() => this.element()).is.defined().map(() => true).or(false);
}
get then() {
return new Tester(this.element());
}
click = () => this.element() && import_react4.fireEvent.click(this.element()) ? this : void 0;
awaitClick = () => (0, import_react4.waitFor)(() => import_react4.fireEvent.click(this.element()));
keyDown = (key) => this.element() && import_react4.fireEvent.keyDown(this.element(), { key }) ? this : void 0;
mouseDown = (index) => this.element() && import_react4.fireEvent.mouseDown((0, import_easy.isDefined)(index) ? this.element().children[index] : this.element()) ? this : void 0;
pressEnter = () => this.keyDown("Enter");
clear = () => this.type("");
type = (value) => import_react4.fireEvent.change(this.element(), { target: { value } });
wait = () => (0, import_react4.waitFor)(this.element);
waitForRemove = () => (0, import_react4.waitForElementToBeRemoved)(this.element);
};
// src/index.ts
var mockTimezone = () => {
jest.spyOn(Intl.DateTimeFormat.prototype, "resolvedOptions").mockReturnValue(Intl.DateTimeFormat("en-US", { timeZone: "UTC" }).resolvedOptions());
};
mockTimezone();
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ElementTester,
Tester,
mockTimezone,
renders,
rendersWait,
waitForRender
});
//# sourceMappingURL=index.js.map
;