From 5c12914e02bdc835b88c1887b553f7c23834b296 Mon Sep 17 00:00:00 2001 From: mattwr18 Date: Thu, 24 Oct 2019 13:52:21 +0200 Subject: [PATCH] Add test cases for LocaleSwitch - if there is a currentUser then update their locale in the database, otherwise, do not. --- .../LocaleSwitch/LocaleSwitch.spec.js | 70 ++++++++++++++++--- 1 file changed, 59 insertions(+), 11 deletions(-) diff --git a/webapp/components/LocaleSwitch/LocaleSwitch.spec.js b/webapp/components/LocaleSwitch/LocaleSwitch.spec.js index eba7bd068..3e9e17443 100644 --- a/webapp/components/LocaleSwitch/LocaleSwitch.spec.js +++ b/webapp/components/LocaleSwitch/LocaleSwitch.spec.js @@ -2,26 +2,43 @@ import { mount, createLocalVue } from '@vue/test-utils' import Styleguide from '@human-connection/styleguide' import VTooltip from 'v-tooltip' import LocaleSwitch from './LocaleSwitch.vue' +import Vuex from 'vuex' const localVue = createLocalVue() localVue.use(Styleguide) localVue.use(VTooltip) +localVue.use(Vuex) describe('LocaleSwitch.vue', () => { - let wrapper - let mocks - let computed - let deutschLanguageItem + let wrapper, mocks, computed, deutschLanguageItem, getters beforeEach(() => { mocks = { $i18n: { - locale: () => 'de', - set: jest.fn(), + locale: () => 'en', + set: jest.fn(locale => locale), }, $t: jest.fn(), + $toast: { + success: jest.fn(a => a), + error: jest.fn(a => a), + }, setPlaceholderText: jest.fn(), + $apollo: { + mutate: jest + .fn() + .mockResolvedValueOnce({ + data: { + UpdateUser: { + locale: 'de', + }, + }, + }) + .mockRejectedValueOnce({ + message: 'Please log in!', + }), + }, } computed = { current: () => { @@ -40,12 +57,21 @@ describe('LocaleSwitch.vue', () => { ] }, } + getters = { + 'auth/user': () => { + return { id: 'u35' } + }, + } }) - describe('mount', () => { - const Wrapper = () => { - return mount(LocaleSwitch, { mocks, localVue, computed }) - } + const Wrapper = () => { + const store = new Vuex.Store({ + getters, + }) + return mount(LocaleSwitch, { mocks, localVue, computed, store }) + } + + describe('with current user', () => { beforeEach(() => { wrapper = Wrapper() wrapper.find('.locale-menu').trigger('click') @@ -53,8 +79,30 @@ describe('LocaleSwitch.vue', () => { deutschLanguageItem.trigger('click') }) - it("changes a user's locale", () => { + it("sets a user's locale", () => { expect(mocks.$i18n.set).toHaveBeenCalledTimes(1) }) + + it("updates the user's locale in the database", () => { + expect(mocks.$apollo.mutate).toHaveBeenCalledTimes(1) + }) + }) + + describe('no current user', () => { + beforeEach(() => { + getters = { + 'auth/user': () => { + return null + }, + } + wrapper = Wrapper() + wrapper.find('.locale-menu').trigger('click') + deutschLanguageItem = wrapper.findAll('li').at(1) + deutschLanguageItem.trigger('click') + }) + + it('does not send a UpdateUser mutation', () => { + expect(mocks.$apollo.mutate).not.toHaveBeenCalled() + }) }) })