react-unit-test-generator
Version:
react-unit-test-generator is a helper for writing unit tests for React apps. Its main goal is to automatically generate a suite of the most common test cases for a given component.
39 lines (32 loc) • 1.12 kB
JavaScript
import onClickHandlers from '../onClickHandlers';
import testRedirectToUrl from './testRedirectToUrl';
import { noInlineMethods, noIdentifierSpecified } from '../warnings';
export default function testButtonsBehaviours(
component,
testRendererInstance,
testProps,
buttonIdentifiers,
) {
return buttonIdentifiers
.map(element => {
if (!element.identifier) {
return noIdentifierSpecified(element);
}
if (element.redirectTo) {
return testRedirectToUrl(element);
}
const reactElement = component.find(`[data-testid="${element.identifier}"]`).at(0);
let boundedMethod = reactElement.props().onClick;
if (!boundedMethod) {
return;
}
boundedMethod = boundedMethod.name.split(' ').slice(-1)[0];
if (boundedMethod === 'onClick') {
return noInlineMethods(element);
}
if (testRendererInstance[boundedMethod] || testRendererInstance.props[boundedMethod]) {
return onClickHandlers(element, boundedMethod, testProps, 'click', true);
}
})
.join('');
}