mirror of
https://github.com/directus/directus.git
synced 2026-01-26 23:19:15 -05:00
Add router + tests
This commit is contained in:
88
src/router.test.ts
Normal file
88
src/router.test.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import Vue from 'vue';
|
||||
import VueCompositionAPI from '@vue/composition-api';
|
||||
import VueRouter, { Route } from 'vue-router';
|
||||
import { onBeforeEach, useRouter } from './router';
|
||||
import { useProjectsStore } from '@/stores/projects';
|
||||
|
||||
const route: Route = {
|
||||
name: undefined,
|
||||
path: '',
|
||||
query: {},
|
||||
hash: '',
|
||||
params: {},
|
||||
fullPath: '',
|
||||
matched: []
|
||||
};
|
||||
|
||||
describe('Router', () => {
|
||||
beforeAll(() => {
|
||||
Vue.config.productionTip = false;
|
||||
Vue.config.devtools = false;
|
||||
Vue.use(VueCompositionAPI);
|
||||
});
|
||||
|
||||
it('Fetches the projects using projectsStore on first load', async () => {
|
||||
const projectsStore = useProjectsStore({});
|
||||
projectsStore.getProjects = jest.fn();
|
||||
|
||||
const toRoute = route;
|
||||
const fromRoute = {
|
||||
...route,
|
||||
name: null
|
||||
};
|
||||
const callback = jest.fn();
|
||||
|
||||
await onBeforeEach(toRoute, fromRoute as any, callback);
|
||||
|
||||
expect(projectsStore.getProjects).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('Redirects to /install if projectStore.needsInstall is true', async () => {
|
||||
const projectsStore = useProjectsStore({});
|
||||
projectsStore.getProjects = jest.fn();
|
||||
|
||||
const toRoute = route;
|
||||
const fromRoute = {
|
||||
...route,
|
||||
name: null
|
||||
};
|
||||
|
||||
const callback = jest.fn();
|
||||
|
||||
projectsStore.state.needsInstall = true;
|
||||
|
||||
await onBeforeEach(toRoute, fromRoute as any, callback);
|
||||
|
||||
expect(callback).toHaveBeenCalledWith('/install');
|
||||
});
|
||||
|
||||
it('Sets the project store currentProjectKey on route changes if project param exists', async () => {
|
||||
const projectsStore = useProjectsStore({});
|
||||
|
||||
const toRoute = {
|
||||
...route,
|
||||
params: {
|
||||
project: 'my-project'
|
||||
}
|
||||
};
|
||||
|
||||
const fromRoute = {
|
||||
...route,
|
||||
name: 'login'
|
||||
};
|
||||
|
||||
const callback = jest.fn();
|
||||
|
||||
await onBeforeEach(toRoute, fromRoute as any, callback);
|
||||
|
||||
expect(projectsStore.state.currentProjectKey).toBe('my-project');
|
||||
});
|
||||
|
||||
describe('useRouter', () => {
|
||||
it('Returns the store instance', () => {
|
||||
const router = useRouter();
|
||||
|
||||
expect(router instanceof VueRouter).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,45 @@
|
||||
import VueRouter from 'vue-router';
|
||||
import VueRouter, { NavigationGuard } from 'vue-router';
|
||||
import Debug from '@/routes/debug.vue';
|
||||
import { useProjectsStore } from '@/stores/projects';
|
||||
|
||||
const router = new VueRouter();
|
||||
const router = new VueRouter({
|
||||
mode: 'hash',
|
||||
routes: [
|
||||
{
|
||||
path: '/:project/*',
|
||||
component: Debug
|
||||
},
|
||||
{
|
||||
path: '/',
|
||||
component: Debug
|
||||
},
|
||||
{
|
||||
path: '*',
|
||||
component: Debug
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
export const onBeforeEach: NavigationGuard = async (to, from, next) => {
|
||||
const projectsStore = useProjectsStore();
|
||||
|
||||
// First load
|
||||
if (from.name === null) {
|
||||
await projectsStore.getProjects();
|
||||
|
||||
if (projectsStore.state.needsInstall === true) {
|
||||
return next('/install');
|
||||
}
|
||||
}
|
||||
|
||||
if (to.params.project) {
|
||||
projectsStore.state.currentProjectKey = to.params.project;
|
||||
}
|
||||
|
||||
return next();
|
||||
};
|
||||
|
||||
router.beforeEach(onBeforeEach);
|
||||
|
||||
export function useRouter() {
|
||||
return router;
|
||||
|
||||
Reference in New Issue
Block a user