add test example

This commit is contained in:
ogerly 2022-06-23 10:59:05 +02:00
parent ee80c65643
commit 6b670c7878
4 changed files with 97 additions and 1 deletions

View File

@ -0,0 +1,46 @@
import { mount } from '@vue/test-utils'
import SessionLogoutTimeout from './SessionLogoutTimeout'
const localVue = global.localVue
const apolloQueryMock = jest.fn()
const state = {
token: '1234',
tokenTime: '123456789',
}
const mocks = {
$store: {
state,
},
$i18n: {
locale: 'en',
},
$t: jest.fn((t) => t),
$apollo: {
query: apolloQueryMock,
},
$route: {
meta: {
requiresAuth: true,
},
},
}
describe('SessionLogoutTimeout', () => {
let wrapper
const Wrapper = () => {
return mount(SessionLogoutTimeout, { localVue, mocks })
}
describe('mount', () => {
beforeEach(() => {
wrapper = Wrapper()
})
it('renders the component div.transaction-link', () => {
expect(wrapper.find('div.session-logout-timeout').exists()).toBe(true)
})
})
})

View File

@ -61,7 +61,7 @@ export default {
this.$emit('logout')
})
},
async log() {
log() {
if (this.$route.meta.requiresAuth) {
const now = new Date().getTime()
const exp = new Date(this.$store.state.tokenTime * 1000).getTime()

View File

@ -28,6 +28,7 @@ export const mutations = {
token: (state, token) => {
state.token = token
if (token) {
// console.log(jwtDecode(token))
state.tokenTime = jwtDecode(token).exp
} else {
state.tokenTime = null

View File

@ -3,6 +3,7 @@ import Vuex from 'vuex'
import Vue from 'vue'
import i18n from '@/i18n.js'
import { localeChanged } from 'vee-validate'
import jwtDecode from 'jwt-decode'
jest.mock('vuex')
jest.mock('@/i18n.js')
@ -11,6 +12,7 @@ jest.mock('vee-validate', () => {
localeChanged: jest.fn(),
}
})
jest.mock('jwtDecode')
i18n.locale = 'blubb'
@ -18,6 +20,7 @@ const {
language,
email,
token,
tokenTime,
firstName,
lastName,
newsletterState,
@ -59,6 +62,52 @@ describe('Vuex store', () => {
token(state, '1234')
expect(state.token).toEqual('1234')
})
it('sets the state of tokenTime', () => {
const state = { token: null, tokenTime: null }
token(state, {
pubKey: {
type: 'Buffer',
data: [
162,
79,
237,
250,
146,
219,
92,
105,
254,
47,
217,
63,
204,
175,
104,
149,
182,
28,
168,
76,
45,
130,
116,
61,
133,
40,
177,
60,
33,
156,
99,
250,
],
},
iat: 1655963974,
exp: 1655964574,
})
tokenTime(state, jwtDecode(token).exp)
expect(state.tokenTime).toEqual('1655964574')
})
})
describe('firstName', () => {