fix tests

This commit is contained in:
Moriz Wahl 2023-03-10 14:22:07 +01:00
parent 0f01027f6b
commit cef0e00f6e
2 changed files with 56 additions and 26 deletions

View File

@ -2,14 +2,18 @@ import { mount } from '@vue/test-utils'
import CreationFormular from './CreationFormular' import CreationFormular from './CreationFormular'
import { adminCreateContribution } from '../graphql/adminCreateContribution' import { adminCreateContribution } from '../graphql/adminCreateContribution'
import { toastErrorSpy, toastSuccessSpy } from '../../test/testSetup' import { toastErrorSpy, toastSuccessSpy } from '../../test/testSetup'
import VueApollo from 'vue-apollo'
import { createMockClient } from 'mock-apollo-client'
import { adminOpenCreations } from '../graphql/adminOpenCreations'
const mockClient = createMockClient()
const apolloProvider = new VueApollo({
defaultClient: mockClient,
})
const localVue = global.localVue const localVue = global.localVue
localVue.use(VueApollo)
const apolloMutateMock = jest.fn().mockResolvedValue({
data: {
adminCreateContribution: [0, 0, 0],
},
})
const stateCommitMock = jest.fn() const stateCommitMock = jest.fn()
const mocks = { const mocks = {
@ -18,9 +22,6 @@ const mocks = {
const date = new Date(d) const date = new Date(d)
return date.toISOString().split('T')[0] return date.toISOString().split('T')[0]
}), }),
$apollo: {
mutate: apolloMutateMock,
},
$store: { $store: {
commit: stateCommitMock, commit: stateCommitMock,
}, },
@ -31,7 +32,8 @@ const propsData = {
creation: [], creation: [],
} }
const now = new Date(Date.now()) const now = new Date()
const getCreationDate = (sub) => { const getCreationDate = (sub) => {
const date = sub === 0 ? now : new Date(now.getFullYear(), now.getMonth() - sub, 1, 0) const date = sub === 0 ? now : new Date(now.getFullYear(), now.getMonth() - sub, 1, 0)
return date.toISOString().split('T')[0] return date.toISOString().split('T')[0]
@ -40,8 +42,43 @@ const getCreationDate = (sub) => {
describe('CreationFormular', () => { describe('CreationFormular', () => {
let wrapper let wrapper
const adminOpenCreationsMock = jest.fn()
const adminCreateContributionMock = jest.fn()
mockClient.setRequestHandler(
adminOpenCreations,
adminOpenCreationsMock.mockResolvedValue({
data: {
adminOpenCreations: [
{
month: new Date(now.getFullYear(), now.getMonth() - 2).getMonth(),
year: new Date(now.getFullYear(), now.getMonth() - 2).getFullYear(),
amount: '200',
},
{
month: new Date(now.getFullYear(), now.getMonth() - 1).getMonth(),
year: new Date(now.getFullYear(), now.getMonth() - 1).getFullYear(),
amount: '400',
},
{
month: now.getMonth(),
year: now.getFullYear(),
amount: '600',
},
],
},
}),
)
mockClient.setRequestHandler(
adminCreateContribution,
adminCreateContributionMock.mockResolvedValue({
data: {
adminCreateContribution: [0, 0, 0],
},
}),
)
const Wrapper = () => { const Wrapper = () => {
return mount(CreationFormular, { localVue, mocks, propsData }) return mount(CreationFormular, { localVue, mocks, propsData, apolloProvider })
} }
describe('mount', () => { describe('mount', () => {
@ -107,17 +144,11 @@ describe('CreationFormular', () => {
}) })
it('sends ... to apollo', () => { it('sends ... to apollo', () => {
expect(apolloMutateMock).toBeCalledWith( expect(adminCreateContributionMock).toBeCalledWith({
expect.objectContaining({
mutation: adminCreateContribution,
variables: {
email: 'benjamin@bluemchen.de',
creationDate: getCreationDate(2), creationDate: getCreationDate(2),
amount: 90, amount: 90,
memo: 'Test create coins', memo: 'Test create coins',
}, })
}),
)
}) })
it('emits update-user-data', () => { it('emits update-user-data', () => {
@ -144,7 +175,7 @@ describe('CreationFormular', () => {
describe('sendForm with server error', () => { describe('sendForm with server error', () => {
beforeEach(async () => { beforeEach(async () => {
apolloMutateMock.mockRejectedValueOnce({ message: 'Ouch!' }) adminCreateContributionMock.mockRejectedValueOnce({ message: 'Ouch!' })
await wrapper.find('.test-submit').trigger('click') await wrapper.find('.test-submit').trigger('click')
}) })
@ -212,7 +243,7 @@ describe('CreationFormular', () => {
}) })
it('sends ... to apollo', () => { it('sends ... to apollo', () => {
expect(apolloMutateMock).toBeCalled() expect(adminCreateContributionMock).toBeCalled()
}) })
}) })
@ -275,7 +306,7 @@ describe('CreationFormular', () => {
}) })
it('sends mutation to apollo', () => { it('sends mutation to apollo', () => {
expect(apolloMutateMock).toBeCalled() expect(adminCreateContributionMock).toBeCalled()
}) })
it('toast success message', () => { it('toast success message', () => {

View File

@ -133,14 +133,13 @@ export default {
// do we want to reset the memo everytime the month changes? // do we want to reset the memo everytime the month changes?
this.text = this.$t('creation_form.creation_for') + ' ' + name.short + ' ' + name.year this.text = this.$t('creation_form.creation_for') + ' ' + name.short + ' ' + name.year
this.rangeMin = 0 this.rangeMin = 0
this.rangeMax = name.creation this.rangeMax = Number(name.creation)
}, },
submitCreation() { submitCreation() {
this.$apollo this.$apollo
.mutate({ .mutate({
mutation: adminCreateContribution, mutation: adminCreateContribution,
variables: { variables: {
email: this.item.email,
creationDate: this.selected.date, creationDate: this.selected.date,
amount: Number(this.value), amount: Number(this.value),
memo: this.text, memo: this.text,