Farmbot-Web-App/frontend/__test_support__/helpers.ts

29 lines
1.1 KiB
TypeScript
Raw Normal View History

2018-06-15 17:59:32 -06:00
import { ReactWrapper, ShallowWrapper } from "enzyme";
import { range } from "lodash";
/** Simulate a click and check button text for a button in a wrapper. */
export function clickButton(
wrapper: ReactWrapper | ShallowWrapper,
position: number,
text: string,
options?: { partial_match?: boolean, button_tag?: string }) {
2020-01-03 13:04:45 -07:00
const btnTag = options?.button_tag ? options.button_tag : "button";
2018-06-15 17:59:32 -06:00
const button = wrapper.find(btnTag).at(position);
const expectedText = text.toLowerCase();
const actualText = button.text().toLowerCase();
2020-01-03 13:04:45 -07:00
options?.partial_match
2018-06-15 17:59:32 -06:00
? expect(actualText).toContain(expectedText)
: expect(actualText).toEqual(expectedText);
button.simulate("click");
}
/** Like `wrapper.text()`, but only includes buttons. */
export function allButtonText(wrapper: ReactWrapper | ShallowWrapper): string {
const buttons = wrapper.find("button");
const btnCount = buttons.length;
const btnPositions = range(btnCount);
const btnTextArray = btnPositions.map(position =>
wrapper.find("button").at(position).text());
return btnTextArray.join("");
}