From 2ac675287c8cee8d99b2bbd96fea29bfba1a269b Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 28 Oct 2021 11:32:59 +0200 Subject: [PATCH] get tests working, define default community in store with empty name and description --- frontend/src/mixin/getCommunityInfo.js | 36 +++++++++------- frontend/src/store/store.js | 5 ++- frontend/src/views/Pages/Login.spec.js | 60 ++++++++++++++------------ frontend/src/views/Pages/Login.vue | 4 +- 4 files changed, 59 insertions(+), 46 deletions(-) diff --git a/frontend/src/mixin/getCommunityInfo.js b/frontend/src/mixin/getCommunityInfo.js index 8c45513dc..f8fda6e45 100644 --- a/frontend/src/mixin/getCommunityInfo.js +++ b/frontend/src/mixin/getCommunityInfo.js @@ -1,20 +1,24 @@ import { communityInfo } from '../graphql/queries' -export const getCommunityInfo = { - 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) - }) - } +export const getCommunityInfoMixin = { + methods: { + getCommunityInfo() { + if (this.$store.state.community.name === '') { + this.$apollo + .query({ + query: communityInfo, + }) + .then((result) => { + this.$store.commit('community', result.data.getCommunityInfo) + return result.data.getCommunityInfo + }) + .catch((error) => { + this.$toasted.error(error.message) + }) + } + }, + }, + created() { + this.getCommunityInfo() }, } diff --git a/frontend/src/store/store.js b/frontend/src/store/store.js index 9f87f0398..6a229c161 100644 --- a/frontend/src/store/store.js +++ b/frontend/src/store/store.js @@ -89,7 +89,10 @@ export const store = new Vuex.Store({ token: null, coinanimation: true, newsletterState: null, - community: null, + community: { + name: '', + description: '', + }, hasElopage: false, publisherId: null, }, diff --git a/frontend/src/views/Pages/Login.spec.js b/frontend/src/views/Pages/Login.spec.js index 83558d076..60abf8821 100644 --- a/frontend/src/views/Pages/Login.spec.js +++ b/frontend/src/views/Pages/Login.spec.js @@ -1,10 +1,10 @@ import { RouterLinkStub, mount } from '@vue/test-utils' import flushPromises from 'flush-promises' import Login from './Login' -import { getCommunityInfo } from '../../mixin/getCommunityInfo' +import { getCommunityInfoMixin } from '../../mixin/getCommunityInfo' const localVue = global.localVue -localVue.mixin(getCommunityInfo) +localVue.mixin(getCommunityInfoMixin) const apolloQueryMock = jest.fn().mockResolvedValue({ data: { @@ -19,11 +19,7 @@ const apolloQueryMock = jest.fn().mockResolvedValue({ const toastErrorMock = jest.fn() const mockStoreDispach = 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 mockStoreCommit = jest.fn() const mockRouterPush = jest.fn() const spinnerHideMock = jest.fn() const spinnerMock = jest.fn(() => { @@ -32,21 +28,6 @@ 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 @@ -55,15 +36,29 @@ describe('Login', () => { locale: 'en', }, $t: jest.fn((t) => t), - $store: global.$store, + $store: { + dispatch: mockStoreDispach, + commit: mockStoreCommit, + state: { + community: { + name: '', + description: '', + }, + publisherId: 12345, + }, + }, $loading: { show: spinnerMock, }, $router: { push: mockRouterPush, }, - $toasted: global.$toasted, - $apollo: global.$apollo, + $toasted: { + error: toastErrorMock, + }, + $apollo: { + query: apolloQueryMock, + }, } const stubs = { @@ -112,12 +107,23 @@ describe('Login', () => { }) describe('Community Data', () => { + beforeEach(() => { + mocks.$store.state.community = { + name: 'Gradido Entwicklung', + url: 'http://localhost/vue/', + registerUrl: 'http://localhost/vue/register', + description: 'Die lokale Entwicklungsumgebung von Gradido.', + } + }) + it('has a Community name', () => { - expect(wrapper.find('.test-communitydata b').text()).toBe('test12') + expect(wrapper.find('.test-communitydata b').text()).toBe('Gradido Entwicklung') }) it('has a Community description', () => { - expect(wrapper.find('.test-communitydata p').text()).toBe('test community 12') + expect(wrapper.find('.test-communitydata p').text()).toBe( + 'Die lokale Entwicklungsumgebung von Gradido.', + ) }) }) diff --git a/frontend/src/views/Pages/Login.vue b/frontend/src/views/Pages/Login.vue index 9b50c9d71..0bb904983 100755 --- a/frontend/src/views/Pages/Login.vue +++ b/frontend/src/views/Pages/Login.vue @@ -63,7 +63,7 @@ import InputPassword from '../../components/Inputs/InputPassword' import InputEmail from '../../components/Inputs/InputEmail' import { login } from '../../graphql/queries' -import { getCommunityInfo } from '../../mixin/getCommunityInfo' +import { getCommunityInfoMixin } from '../../mixin/getCommunityInfo' export default { name: 'login', @@ -71,7 +71,7 @@ export default { InputPassword, InputEmail, }, - mixins: [getCommunityInfo], + mixins: [getCommunityInfoMixin], data() { return { form: {