mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
test storing pid from routes
This commit is contained in:
parent
20466d311c
commit
505c2f030b
@ -6,6 +6,7 @@ import { loadAllRules } from './validation-rules'
|
||||
import { ApolloClient, ApolloLink, InMemoryCache, HttpLink } from 'apollo-boost'
|
||||
import VueApollo from 'vue-apollo'
|
||||
import CONFIG from './config'
|
||||
import addNavigationGuards from './routes/guards'
|
||||
|
||||
import { store } from './store/store'
|
||||
|
||||
@ -49,18 +50,7 @@ Vue.config.productionTip = false
|
||||
|
||||
loadAllRules(i18n)
|
||||
|
||||
router.beforeEach((to, from, next) => {
|
||||
const publisherId = to.query.pid
|
||||
if (publisherId) {
|
||||
store.commit('publisherId', publisherId)
|
||||
delete to.query.pid
|
||||
}
|
||||
if (to.meta.requiresAuth && !store.state.token) {
|
||||
next({ path: '/login' })
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
})
|
||||
addNavigationGuards(router, store)
|
||||
|
||||
/* eslint-disable no-new */
|
||||
new Vue({
|
||||
|
||||
16
frontend/src/routes/guards.js
Normal file
16
frontend/src/routes/guards.js
Normal file
@ -0,0 +1,16 @@
|
||||
const addNavigationGuards = (router, store) => {
|
||||
router.beforeEach((to, from, next) => {
|
||||
const publisherId = to.query.pid
|
||||
if (publisherId) {
|
||||
store.commit('publisherId', publisherId)
|
||||
delete to.query.pid
|
||||
}
|
||||
if (to.meta.requiresAuth && !store.state.token) {
|
||||
next({ path: '/login' })
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export default addNavigationGuards
|
||||
38
frontend/src/routes/guards.test.js
Normal file
38
frontend/src/routes/guards.test.js
Normal file
@ -0,0 +1,38 @@
|
||||
import addNavigationGuards from './guards'
|
||||
import router from './router'
|
||||
|
||||
const storeCommitMock = jest.fn()
|
||||
|
||||
const store = {
|
||||
commit: storeCommitMock,
|
||||
state: {
|
||||
token: null,
|
||||
},
|
||||
}
|
||||
|
||||
addNavigationGuards(router, store)
|
||||
|
||||
describe('navigation guards', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
describe('publisher ID', () => {
|
||||
it('commits the pid to the store when present', () => {
|
||||
router.push({ path: 'login', query: { pid: 42 } })
|
||||
expect(storeCommitMock).toBeCalledWith('publisherId', '42')
|
||||
})
|
||||
|
||||
it('does not commit the pid when not present', () => {
|
||||
router.push({ path: 'register' })
|
||||
expect(storeCommitMock).not.toBeCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe('authorization', () => {
|
||||
it.skip('redirects to login when not authorized', async () => {
|
||||
router.push({ path: 'overview' })
|
||||
expect(router.history.current.path).toBe('/login')
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -5,10 +5,9 @@ import CONFIG from '../config'
|
||||
|
||||
Vue.use(VueRouter)
|
||||
|
||||
// configure router
|
||||
const router = new VueRouter({
|
||||
base: '/vue',
|
||||
routes, // short for routes: routes
|
||||
routes,
|
||||
linkActiveClass: 'active',
|
||||
mode: 'history',
|
||||
scrollBehavior: (to, from, savedPosition) => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user