Files
overleaf-cep/services/web/test/acceptance/src/helpers/injectRoute.mjs
Andrew Rumble e0dba75b81 Convert top level acceptance tests to ES module
GitOrigin-RevId: ab45010ec557d62576c470d2e024549e67261c66
2024-10-16 09:35:34 +00:00

40 lines
1.3 KiB
JavaScript

/**
* Used to inject an endpoint into our app that should only be available
* when running in the test environment.
*
* @param app - a reference to the app.
* @param searchFilter - a filter function to locate a route to position the new route immediatley after.
* @param addRouteCallback - a callback that takes a router and creates the new route.
*/
export function injectRouteAfter(app, searchFilter, addRouteCallback) {
const stack = app._router.stack
stack.forEach(layer => {
if (layer.name !== 'router' || !layer.handle || !layer.handle.stack) {
return
}
// Find the route that we want to position out new route after.
const newRouteIndex = layer.handle.stack.findIndex(
route => route && route.route && searchFilter(route.route)
)
if (newRouteIndex !== -1) {
// Add our new endpoint to the end of the router stack.
addRouteCallback(layer.handle)
const routeStack = layer.handle.stack
const sessionRoute = routeStack[routeStack.length - 1]
// Then we reposition it next to the route we found previously.
layer.handle.stack = [
...routeStack.slice(0, newRouteIndex),
sessionRoute,
...routeStack.slice(newRouteIndex, routeStack.length - 1),
]
}
})
}
export default { injectRouteAfter }