UNPKG

@gravity-ui/data-source

Version:
116 lines 4.12 kB
import _objectSpread from "@babel/runtime/helpers/objectSpread2"; import React from 'react'; import { render, screen } from '@testing-library/react'; import { DataLoader } from '../DataLoader'; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; var MockLoadingView = function MockLoadingView() { return /*#__PURE__*/_jsx("div", { "data-testid": "loading-view", children: "Loading..." }); }; var MockErrorView = function MockErrorView(_ref) { var error = _ref.error, action = _ref.action; return /*#__PURE__*/_jsxs("div", { "data-testid": "error-view", children: [error ? "Error: ".concat(error.message) : 'No error', action ? /*#__PURE__*/_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__*/_jsx("div", { "data-testid": "content", children: "Content" }) }; return render(/*#__PURE__*/_jsx(DataLoader, _objectSpread(_objectSpread({}, defaultProps), props))); }; it('should render children when status is success', function () { renderDataLoader({ status: 'success' }); expect(screen.getByTestId('content')).toBeInTheDocument(); expect(screen.queryByTestId('loading-view')).not.toBeInTheDocument(); expect(screen.queryByTestId('error-view')).not.toBeInTheDocument(); }); it('should render LoadingView when status is loading', function () { renderDataLoader({ status: 'loading' }); expect(screen.getByTestId('loading-view')).toBeInTheDocument(); expect(screen.queryByTestId('content')).not.toBeInTheDocument(); expect(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(screen.getByTestId('error-view')).toBeInTheDocument(); expect(screen.queryByTestId('content')).not.toBeInTheDocument(); expect(screen.queryByTestId('loading-view')).not.toBeInTheDocument(); expect(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(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(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