refactor: Bootstrap Vue Toast in Admin Interface

This commit is contained in:
Moriz Wahl 2022-03-03 12:07:35 +01:00
parent e6a2fbe6c7
commit bfaa77482c
7 changed files with 50 additions and 24 deletions

View File

@ -1,21 +1,17 @@
import { mount } from '@vue/test-utils' import { mount } from '@vue/test-utils'
import ConfirmRegisterMailFormular from './ConfirmRegisterMailFormular.vue' import ConfirmRegisterMailFormular from './ConfirmRegisterMailFormular.vue'
import { toastErrorSpy, toastSuccessSpy } from '../../test/testSetup'
const localVue = global.localVue const localVue = global.localVue
const apolloMutateMock = jest.fn().mockResolvedValue() const apolloMutateMock = jest.fn().mockResolvedValue()
const toastSuccessMock = jest.fn()
const toastErrorMock = jest.fn()
const mocks = { const mocks = {
$t: jest.fn((t) => t), $t: jest.fn((t) => t),
$apollo: { $apollo: {
mutate: apolloMutateMock, mutate: apolloMutateMock,
}, },
$toasted: {
success: toastSuccessMock,
error: toastErrorMock,
},
} }
const propsData = { const propsData = {
@ -54,7 +50,7 @@ describe('ConfirmRegisterMailFormular', () => {
}) })
it('toasts a success message', () => { it('toasts a success message', () => {
expect(toastSuccessMock).toBeCalledWith('unregister_mail.success') expect(toastSuccessSpy).toBeCalledWith('unregister_mail.success')
}) })
}) })
@ -66,7 +62,7 @@ describe('ConfirmRegisterMailFormular', () => {
}) })
it('toasts an error message', () => { it('toasts an error message', () => {
expect(toastErrorMock).toBeCalledWith('unregister_mail.error') expect(toastErrorSpy).toBeCalledWith('unregister_mail.error')
}) })
}) })
}) })

View File

@ -48,10 +48,10 @@ export default {
}, },
}) })
.then(() => { .then(() => {
this.$toasted.success(this.$t('unregister_mail.success', { email: this.email })) this.toastSuccess(this.$t('unregister_mail.success', { email: this.email }))
}) })
.catch((error) => { .catch((error) => {
this.$toasted.error(this.$t('unregister_mail.error', { message: error.message })) this.toastError(this.$t('unregister_mail.error', { message: error.message }))
}) })
}, },
}, },

View File

@ -92,5 +92,7 @@
"userIsDeleted": "Der Nutzer ist gelöscht. Es können keine GDD mehr geschöpft werden.", "userIsDeleted": "Der Nutzer ist gelöscht. Es können keine GDD mehr geschöpft werden.",
"user_deleted": "Nutzer ist gelöscht.", "user_deleted": "Nutzer ist gelöscht.",
"user_recovered": "Nutzer ist wiederhergestellt.", "user_recovered": "Nutzer ist wiederhergestellt.",
"user_search": "Nutzer-Suche" "user_search": "Nutzer-Suche",
"error": "Fehler",
"success": "Erfolg"
} }

View File

@ -92,5 +92,7 @@
"userIsDeleted": "The user is deleted. No more GDD can be created.", "userIsDeleted": "The user is deleted. No more GDD can be created.",
"user_deleted": "User is deleted.", "user_deleted": "User is deleted.",
"user_recovered": "User is recovered.", "user_recovered": "User is recovered.",
"user_search": "User search" "user_search": "User search",
"error": "Error",
"success": "Success"
} }

View File

@ -17,7 +17,7 @@ import { BootstrapVue, IconsPlugin } from 'bootstrap-vue'
import 'bootstrap/dist/css/bootstrap.css' import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css' import 'bootstrap-vue/dist/bootstrap-vue.css'
import Toasted from 'vue-toasted' import { toasters } from './mixins/toaster'
import { apolloProvider } from './plugins/apolloProvider' import { apolloProvider } from './plugins/apolloProvider'
@ -27,17 +27,7 @@ Vue.use(IconsPlugin)
Vue.use(VueApollo) Vue.use(VueApollo)
Vue.use(Toasted, { Vue.mixin(toasters)
position: 'top-center',
duration: 5000,
fullWidth: true,
action: {
text: 'x',
onClick: (e, toastObject) => {
toastObject.goAway(0)
},
},
})
addNavigationGuards(router, store, apolloProvider.defaultClient, i18n) addNavigationGuards(router, store, apolloProvider.defaultClient, i18n)

View File

@ -0,0 +1,29 @@
export const toasters = {
methods: {
toastSuccess(message) {
this.toast(message, {
title: this.$t('success'),
variant: 'success',
})
},
toastError(message) {
this.toast(message, {
title: this.$t('error'),
variant: 'danger',
})
},
toast(message, options) {
message = message.replace(/^GraphQL error: /, '')
this.$bvToast.toast(message, {
autoHideDelay: 5000,
appendToast: true,
solid: true,
toaster: 'b-toaster-top-right',
headerClass: 'gdd-toaster-title',
bodyClass: 'gdd-toaster-body',
toastClass: 'gdd-toaster',
...options,
})
},
},
}

View File

@ -5,11 +5,18 @@ import { BootstrapVue, IconsPlugin } from 'bootstrap-vue'
// without this async calls are not working // without this async calls are not working
import 'regenerator-runtime' import 'regenerator-runtime'
import { toasters } from '../src/mixins/toaster'
export const toastErrorSpy = jest.spyOn(toasters.methods, 'toastError')
export const toastSuccessSpy = jest.spyOn(toasters.methods, 'toastSuccess')
global.localVue = createLocalVue() global.localVue = createLocalVue()
global.localVue.use(BootstrapVue) global.localVue.use(BootstrapVue)
global.localVue.use(IconsPlugin) global.localVue.use(IconsPlugin)
global.localVue.mixin(toasters)
// throw errors for vue warnings to force the programmers to take care about warnings // throw errors for vue warnings to force the programmers to take care about warnings
Vue.config.warnHandler = (w) => { Vue.config.warnHandler = (w) => {
throw new Error(w) throw new Error(w)