@gravity-ui/data-source
Version:
A wrapper around data fetching
119 lines (118 loc) • 4.49 kB
JavaScript
;
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var _react = _interopRequireDefault(require("react"));
var _react2 = require("@testing-library/react");
var _DataLoader = require("../DataLoader");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
var MockLoadingView = function MockLoadingView() {
return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
"data-testid": "loading-view",
children: "Loading..."
});
};
var MockErrorView = function MockErrorView(_ref) {
var error = _ref.error,
action = _ref.action;
return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
"data-testid": "error-view",
children: [error ? "Error: ".concat(error.message) : 'No error', action ? /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
"data-testid": "error-action",
onClick: action.handler,
children: action.children || 'Retry'
}) : null]
});
};
describe('DataLoader', function () {
var renderDataLoader = function renderDataLoader() {
var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var defaultProps = {
status: 'success',
error: null,
LoadingView: MockLoadingView,
ErrorView: MockErrorView,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
"data-testid": "content",
children: "Content"
})
};
return (0, _react2.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_DataLoader.DataLoader, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, defaultProps), props)));
};
it('should render children when status is success', function () {
renderDataLoader({
status: 'success'
});
expect(_react2.screen.getByTestId('content')).toBeInTheDocument();
expect(_react2.screen.queryByTestId('loading-view')).not.toBeInTheDocument();
expect(_react2.screen.queryByTestId('error-view')).not.toBeInTheDocument();
});
it('should render LoadingView when status is loading', function () {
renderDataLoader({
status: 'loading'
});
expect(_react2.screen.getByTestId('loading-view')).toBeInTheDocument();
expect(_react2.screen.queryByTestId('content')).not.toBeInTheDocument();
expect(_react2.screen.queryByTestId('error-view')).not.toBeInTheDocument();
});
it('should render ErrorView when status is error', function () {
var error = new Error('Test error');
renderDataLoader({
status: 'error',
error: error
});
expect(_react2.screen.getByTestId('error-view')).toBeInTheDocument();
expect(_react2.screen.queryByTestId('content')).not.toBeInTheDocument();
expect(_react2.screen.queryByTestId('loading-view')).not.toBeInTheDocument();
expect(_react2.screen.getByText(/Test error/)).toBeInTheDocument();
});
it('should pass errorAction to ErrorView', function () {
var error = new Error('Test error');
var errorAction = jest.fn();
renderDataLoader({
status: 'error',
error: error,
errorAction: errorAction
});
expect(_react2.screen.getByTestId('error-action')).toBeInTheDocument();
});
it('should pass errorAction with custom children to ErrorView', function () {
var error = new Error('Test error');
var errorAction = {
handler: jest.fn(),
children: 'Custom action'
};
renderDataLoader({
status: 'error',
error: error,
errorAction: errorAction
});
expect(_react2.screen.getByText('Custom action')).toBeInTheDocument();
});
it('should pass loadingViewProps to LoadingView', function () {
var LoadingView = jest.fn(MockLoadingView);
var loadingViewProps = {
customProp: 'test'
};
renderDataLoader({
status: 'loading',
LoadingView: LoadingView,
loadingViewProps: loadingViewProps
});
expect(LoadingView).toHaveBeenCalledWith(expect.objectContaining(loadingViewProps), undefined);
});
it('should pass errorViewProps to ErrorView', function () {
var ErrorView = jest.fn(MockErrorView);
var errorViewProps = {
customProp: 'test'
};
var error = new Error('Test error');
renderDataLoader({
status: 'error',
error: error,
ErrorView: ErrorView,
errorViewProps: errorViewProps
});
expect(ErrorView).toHaveBeenCalledWith(expect.objectContaining(errorViewProps), undefined);
});
});
// #sourceMappingURL=DataLoader.test.js.map