try to get the tests running

This commit is contained in:
Moriz Wahl 2021-05-17 15:26:28 +02:00
parent 735414fab0
commit c4440f50e3
6 changed files with 47 additions and 34 deletions

View File

@ -1,6 +1,6 @@
<template>
<div class="language-switch">
<b-dropdown size="sm" :text="currentLanguageName + ' - ' + currentLanguage">
<b-dropdown size="sm" :text="currentLanguage.name + ' - ' + currentLanguage.code">
<b-dropdown-item
v-for="lang in locales"
@click.prevent="saveLocale(lang.code)"
@ -24,7 +24,7 @@ export default {
}
},
methods: {
setLocale(locale) {
async setLocale(locale) {
this.$i18n.locale = locale
this.$store.commit('language', this.$i18n.locale)
localeChanged(locale)
@ -32,7 +32,7 @@ export default {
async saveLocale(locale) {
this.setLocale(locale)
if (this.$store.state.sessionId && this.$store.state.email) {
const result = loginAPI.updateLanguage(
const result = await loginAPI.updateLanguage(
this.$store.state.sessionId,
this.$store.state.email,
locale,
@ -44,15 +44,25 @@ export default {
}
}
},
getLocaleObject(code) {
return this.locales.find((l) => l.code === code)
},
getNavigatorLanguage() {
const lang = navigator.language
if (lang) return lang.split('-')[0]
return lang
},
},
computed: {
currentLanguage() {
const locale = this.$store.state.language || navigator.language || 'en'
this.setLocale(this.$store.state.language)
return locale
},
currentLanguageName() {
return this.locales.find((l) => l.code === this.currentLanguage).name
let locale = this.$store.state.language || this.getNavigatorLanguage() || 'en'
let object = this.getLocaleObject(locale)
if (!object) {
locale = 'en'
object = this.getLocaleObject(locale)
}
this.setLocale(locale)
return object
},
},
}

View File

@ -19,6 +19,7 @@ describe('SideBar', () => {
state: {
email: 'test@example.org',
},
commit: jest.fn(),
},
$i18n: {
locale: 'en',
@ -93,7 +94,7 @@ describe('SideBar', () => {
mocks.$i18n.locale = 'de'
})
it('links to the German elopage when locale is set to de', () => {
it('links to the German elopage when locale is set to de', async () => {
expect(wrapper.findAll('li').at(0).find('a').attributes('href')).toBe(
'https://elopage.com/s/gradido/sign_in?locale=de',
)

View File

@ -36,19 +36,36 @@ describe('Vuex store', () => {
const state = {}
it('calls two commits', () => {
login({ commit, state }, { sessionId: 1234, email: 'someone@there.is' })
expect(commit).toHaveBeenCalledTimes(2)
login(
{ commit, state },
{ sessionId: 1234, user: { email: 'someone@there.is', language: 'en' } },
)
expect(commit).toHaveBeenCalledTimes(3)
})
it('commits sessionId', () => {
login({ commit, state }, { sessionId: 1234, email: 'someone@there.is' })
login(
{ commit, state },
{ sessionId: 1234, user: { email: 'someone@there.is', language: 'en' } },
)
expect(commit).toHaveBeenNthCalledWith(1, 'sessionId', 1234)
})
it('commits email', () => {
login({ commit, state }, { sessionId: 1234, email: 'someone@there.is' })
login(
{ commit, state },
{ sessionId: 1234, user: { email: 'someone@there.is', language: 'en' } },
)
expect(commit).toHaveBeenNthCalledWith(2, 'email', 'someone@there.is')
})
it('commits language', () => {
login(
{ commit, state },
{ sessionId: 1234, user: { email: 'someone@there.is', language: 'en' } },
)
expect(commit).toHaveBeenNthCalledWith(3, 'language', 'en')
})
})
describe('logout', () => {

View File

@ -12,6 +12,9 @@ describe('ContentFooter', () => {
locale: 'en',
},
$t: jest.fn((t) => t),
$store: {
commit: jest.fn(),
},
}
const Wrapper = () => {

View File

@ -1,5 +1,4 @@
import { mount, RouterLinkStub } from '@vue/test-utils'
import Vuex from 'vuex'
import flushPromises from 'flush-promises'
import Login from './Login'
@ -16,20 +15,12 @@ describe('Login', () => {
$t: jest.fn((t) => t),
}
const state = {
loginfail: false,
}
const store = new Vuex.Store({
state,
})
const stubs = {
RouterLink: RouterLinkStub,
}
const Wrapper = () => {
return mount(Login, { localVue, mocks, store, stubs })
return mount(Login, { localVue, mocks, stubs })
}
describe('mount', () => {

View File

@ -1,5 +1,4 @@
import { mount, RouterLinkStub } from '@vue/test-utils'
import Vuex from 'vuex'
import flushPromises from 'flush-promises'
import Register from './Register'
@ -16,20 +15,12 @@ describe('Register', () => {
$t: jest.fn((t) => t),
}
const state = {
// loginfail: false,
}
const store = new Vuex.Store({
state,
})
const stubs = {
RouterLink: RouterLinkStub,
}
const Wrapper = () => {
return mount(Register, { localVue, mocks, store, stubs })
return mount(Register, { localVue, mocks, stubs })
}
describe('mount', () => {