From 72a45285e6b9b499f841bb46404183140d908218 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Mon, 6 Dec 2021 12:51:38 +0100 Subject: [PATCH] setup test ApolloLink --- admin/src/main.test.js | 108 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 99 insertions(+), 9 deletions(-) diff --git a/admin/src/main.test.js b/admin/src/main.test.js index bf3df3799..747ef5d2a 100644 --- a/admin/src/main.test.js +++ b/admin/src/main.test.js @@ -4,19 +4,20 @@ import CONFIG from './config' import Vue from 'vue' import VueApollo from 'vue-apollo' -import Vuex from 'vuex' -import VueI18n from 'vue-i18n' +import i18n from './i18n' import { BootstrapVue, IconsPlugin } from 'bootstrap-vue' import moment from 'vue-moment' +import store from './store/store' +import router from './router/router' jest.mock('vue') jest.mock('vue-apollo') jest.mock('vuex') jest.mock('vue-i18n') jest.mock('vue-moment') - -const storeMock = jest.fn() -Vuex.Store = storeMock +jest.mock('./store/store') +jest.mock('./i18n') +jest.mock('./router/router') jest.mock('apollo-boost', () => { return { @@ -65,8 +66,12 @@ describe('main', () => { expect(Vue).toBeCalled() }) - it('calls VueI18n', () => { - expect(VueI18n).toBeCalled() + it('calls i18n', () => { + expect(Vue).toBeCalledWith( + expect.objectContaining({ + i18n, + }), + ) }) it('calls BootstrapVue', () => { @@ -81,7 +86,92 @@ describe('main', () => { expect(Vue.use).toBeCalledWith(moment) }) - it.skip('creates a store', () => { - expect(storeMock).toBeCalled() + it('creates a store', () => { + expect(Vue).toBeCalledWith( + expect.objectContaining({ + store, + }), + ) + }) + + it('creates a router', () => { + expect(Vue).toBeCalledWith( + expect.objectContaining({ + router, + }), + ) + }) + + describe('ApolloLink', () => { + // mock store + const storeDispatchMock = jest.fn() + store.state = { + token: 'some-token', + } + store.dispatch = storeDispatchMock + + // mock i18n.t + i18n.t = jest.fn((t) => t) + + // mock apllo response + const responseMock = { + errors: [{ message: '403.13 - Client certificate revoked' }], + } + + // mock router + const routerPushMock = jest.fn() + router.push = routerPushMock + router.currentRoute = { + path: '/overview', + } + + // mock context + const setContextMock = jest.fn() + const getContextMock = jest.fn(() => { + return { + response: { + headers: { + get: jest.fn(), + }, + }, + } + }) + + // mock apollo link function params + const operationMock = { + setContext: setContextMock, + getContext: getContextMock, + } + + const forwardMock = jest.fn(() => { + return [responseMock] + }) + + // get apollo link callback + const middleware = ApolloLink.mock.calls[0][0] + + beforeEach(() => { + jest.clearAllMocks() + // run the callback with mocked params + middleware(operationMock, forwardMock) + }) + + it('sets authorization header', () => { + expect(setContextMock).toBeCalledWith({ + headers: { + Authorization: 'Bearer some-token', + }, + }) + }) + + describe('apollo response is 403.13', () => { + it.skip('dispatches logout', () => { + expect(storeDispatchMock).toBeCalledWith('logout', null) + }) + + it.skip('redirects to logout', () => { + expect(routerPushMock).toBeCalledWith('/logout') + }) + }) }) })