component-test-setup
Version:
Standardized test setup methods for React components in tests.
41 lines (40 loc) • 1.5 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setupRtl = void 0;
const react_1 = __importDefault(require("react"));
/**
* Creates a `renderView` function that can be used in unit tests to mount a component.
*
* @param Component - React component to be rendered.
* @param baseProps - Any default props to pass to the component in all tests.
* @example
* ```tsx
* const renderView = setupRtl(ButtonWithText, { onClick: jest.fn() })
*
* const text = 'Hooray!';
* const { props: { onClick }, view } = renderView({ text });
*
* FireEvent.click(view.getByRole('button'));
*
* expect(onClick).toHaveBeenCalledWith(text);
* ```
*/
function setupRtl(Component, baseProps) {
const { render } = require("@testing-library/react");
let options;
function renderView(testProps) {
const props = Object.assign(Object.assign({}, baseProps), testProps);
const view = render(react_1.default.createElement(Component, Object.assign({}, props)), options);
const update = (updatedProps) => view.rerender(react_1.default.createElement(Component, Object.assign({}, props, updatedProps)));
return { props, view, update };
}
renderView.options = (newOptions) => {
options = newOptions;
return renderView;
};
return renderView;
}
exports.setupRtl = setupRtl;