@render-with/react-router
Version:
Render decorators for components under test that require a React Router or Routes.
96 lines (95 loc) • 3.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.withRouting = exports.withRouter = exports.withLocation = exports.Page = void 0;
var _propTypes = _interopRequireDefault(require("prop-types"));
var _reactRouterDom = require("react-router-dom");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const assertRoot = path => path[0] === '/' ? path : `/${path}`;
const resolvePathParams = params => path => assertRoot((0, _reactRouterDom.generatePath)(path, params));
const resolveInitialEntries = (paths, params) => ({
initialEntries: paths.map(resolvePathParams(params)),
initialIndex: paths.length - 1
});
const Page = ({
name,
children
}) => {
const location = (0, _reactRouterDom.useLocation)();
const url = location.pathname + location.search;
return /*#__PURE__*/(0, _jsxRuntime.jsxs)("main", {
"aria-label": name,
children: [children, /*#__PURE__*/(0, _jsxRuntime.jsx)("nav", {
"aria-label": "Breadcrumb",
children: /*#__PURE__*/(0, _jsxRuntime.jsx)("a", {
href: url,
"aria-current": "page",
children: url
})
})]
});
};
exports.Page = Page;
Page.propTypes = {
name: _propTypes.default.string.isRequired,
children: _propTypes.default.node
};
const withLocation = (path = '/', params = {}) => node => /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactRouterDom.MemoryRouter, {
...resolveInitialEntries([assertRoot(path)], params),
children: node
});
exports.withLocation = withLocation;
const withRouter = () => withLocation('/');
exports.withRouter = withRouter;
const withRouting = ({
name = 'Current Page',
path = '/current',
params = {},
routes = {},
subroutes = {}
} = {}) => node => /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactRouterDom.MemoryRouter, {
initialEntries: ['/previous', assertRoot((0, _reactRouterDom.generatePath)(path, params)), '/next'],
initialIndex: 1,
children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactRouterDom.Routes, {
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactRouterDom.Route, {
path: "/",
element: /*#__PURE__*/(0, _jsxRuntime.jsx)(Page, {
name: "Root Page"
})
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactRouterDom.Route, {
path: "previous",
element: /*#__PURE__*/(0, _jsxRuntime.jsx)(Page, {
name: "Previous Page"
})
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactRouterDom.Route, {
path: `${path}/*`,
element: /*#__PURE__*/(0, _jsxRuntime.jsx)(Page, {
name: name,
children: node
})
}), Object.keys(subroutes).map(subpath => /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactRouterDom.Route, {
path: `${path}/${subpath}`,
element: /*#__PURE__*/(0, _jsxRuntime.jsx)(Page, {
name: subroutes[subpath]
})
}, subpath)), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactRouterDom.Route, {
path: "next",
element: /*#__PURE__*/(0, _jsxRuntime.jsx)(Page, {
name: "Next Page"
})
}), Object.keys(routes).map(path => /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactRouterDom.Route, {
path: path,
element: /*#__PURE__*/(0, _jsxRuntime.jsx)(Page, {
name: routes[path]
})
}, path)), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactRouterDom.Route, {
path: "*",
element: /*#__PURE__*/(0, _jsxRuntime.jsx)(Page, {
name: "Other Page"
})
})]
})
});
exports.withRouting = withRouting;