From 8a8cccd540852247b4be08ecc81b83e67427ff26 Mon Sep 17 00:00:00 2001 From: elweyn Date: Thu, 28 Oct 2021 10:34:44 +0200 Subject: [PATCH] [WIP] Test that the mixin does the work. --- frontend/src/mixin/getCommunityInfo.js | 4 +- frontend/src/views/Pages/Login.spec.js | 61 ++++++++++++++------------ 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/frontend/src/mixin/getCommunityInfo.js b/frontend/src/mixin/getCommunityInfo.js index b3feb1267..8c45513dc 100644 --- a/frontend/src/mixin/getCommunityInfo.js +++ b/frontend/src/mixin/getCommunityInfo.js @@ -1,16 +1,18 @@ import { communityInfo } from '../graphql/queries' export const getCommunityInfo = { - created() { + beforeCreate() { if (!this.$store.state.community) { this.$apollo .query({ query: communityInfo, }) .then((result) => { + // console.log('Got a community info: ', result.data.getCommunityInfo) this.$store.commit('community', result.data.getCommunityInfo) }) .catch((error) => { + // console.log('Got a error: ', error.message) this.$toasted.error(error.message) }) } diff --git a/frontend/src/views/Pages/Login.spec.js b/frontend/src/views/Pages/Login.spec.js index 13d12df85..83558d076 100644 --- a/frontend/src/views/Pages/Login.spec.js +++ b/frontend/src/views/Pages/Login.spec.js @@ -1,8 +1,10 @@ -import { mount, RouterLinkStub } from '@vue/test-utils' +import { RouterLinkStub, mount } from '@vue/test-utils' import flushPromises from 'flush-promises' import Login from './Login' +import { getCommunityInfo } from '../../mixin/getCommunityInfo' const localVue = global.localVue +localVue.mixin(getCommunityInfo) const apolloQueryMock = jest.fn().mockResolvedValue({ data: { @@ -17,7 +19,11 @@ const apolloQueryMock = jest.fn().mockResolvedValue({ const toastErrorMock = jest.fn() const mockStoreDispach = jest.fn() -const mockStoreCommit = jest.fn() +const mockStoreCommit = jest.fn((target, community) => { + // console.log('mockStoreCommit', global.$store.state.community) + global.$store.state[target] = community + // console.log('mockStoreCommit', global.$store.state.community) +}) const mockRouterPush = jest.fn() const spinnerHideMock = jest.fn() const spinnerMock = jest.fn(() => { @@ -26,6 +32,21 @@ const spinnerMock = jest.fn(() => { } }) +global.$store = { + dispatch: mockStoreDispach, + commit: mockStoreCommit, + state: { + community: null, + publisherId: 12345, + }, +} +global.$toasted = { + error: toastErrorMock, +} +global.$apollo = { + query: apolloQueryMock, +} + describe('Login', () => { let wrapper @@ -34,31 +55,15 @@ describe('Login', () => { locale: 'en', }, $t: jest.fn((t) => t), - $store: { - dispatch: mockStoreDispach, - commit: mockStoreCommit, - state: { - community: { - name: 'Gradido Entwicklung', - url: 'http://localhost/vue/', - registerUrl: 'http://localhost/vue/register', - description: 'Die lokale Entwicklungsumgebung von Gradido.', - }, - publisherId: 12345, - }, - }, + $store: global.$store, $loading: { show: spinnerMock, }, $router: { push: mockRouterPush, }, - $toasted: { - error: toastErrorMock, - }, - $apollo: { - query: apolloQueryMock, - }, + $toasted: global.$toasted, + $apollo: global.$apollo, } const stubs = { @@ -74,10 +79,6 @@ describe('Login', () => { wrapper = Wrapper() }) - it('renders the Login form', () => { - expect(wrapper.find('div.login-form').exists()).toBeTruthy() - }) - it('commits the community info to the store', () => { expect(mockStoreCommit).toBeCalledWith('community', { name: 'test12', @@ -87,6 +88,10 @@ describe('Login', () => { }) }) + it('renders the Login form', () => { + expect(wrapper.find('div.login-form').exists()).toBeTruthy() + }) + describe('communities gives back error', () => { beforeEach(() => { apolloQueryMock.mockRejectedValue({ @@ -108,13 +113,11 @@ describe('Login', () => { describe('Community Data', () => { it('has a Community name', () => { - expect(wrapper.find('.test-communitydata b').text()).toBe('Gradido Entwicklung') + expect(wrapper.find('.test-communitydata b').text()).toBe('test12') }) it('has a Community description', () => { - expect(wrapper.find('.test-communitydata p').text()).toBe( - 'Die lokale Entwicklungsumgebung von Gradido.', - ) + expect(wrapper.find('.test-communitydata p').text()).toBe('test community 12') }) })