From 6b670c7878af7711f0da7c99f3e981f309bacd4b Mon Sep 17 00:00:00 2001 From: ogerly Date: Thu, 23 Jun 2022 10:59:05 +0200 Subject: [PATCH] add test example --- .../components/SessionLogoutTimeout.spec.js | 46 +++++++++++++++++ .../src/components/SessionLogoutTimeout.vue | 2 +- frontend/src/store/store.js | 1 + frontend/src/store/store.test.js | 49 +++++++++++++++++++ 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 frontend/src/components/SessionLogoutTimeout.spec.js diff --git a/frontend/src/components/SessionLogoutTimeout.spec.js b/frontend/src/components/SessionLogoutTimeout.spec.js new file mode 100644 index 000000000..a76ce1461 --- /dev/null +++ b/frontend/src/components/SessionLogoutTimeout.spec.js @@ -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) + }) + }) +}) diff --git a/frontend/src/components/SessionLogoutTimeout.vue b/frontend/src/components/SessionLogoutTimeout.vue index a478b5fd8..dfb601eab 100644 --- a/frontend/src/components/SessionLogoutTimeout.vue +++ b/frontend/src/components/SessionLogoutTimeout.vue @@ -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() diff --git a/frontend/src/store/store.js b/frontend/src/store/store.js index e95eec7b9..109a2a1ea 100644 --- a/frontend/src/store/store.js +++ b/frontend/src/store/store.js @@ -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 diff --git a/frontend/src/store/store.test.js b/frontend/src/store/store.test.js index e8d680f94..c7316083a 100644 --- a/frontend/src/store/store.test.js +++ b/frontend/src/store/store.test.js @@ -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', () => {