From 74c575882ae79827c180f024110610aa6b431d23 Mon Sep 17 00:00:00 2001 From: elweyn Date: Tue, 10 Jan 2023 13:56:39 +0100 Subject: [PATCH] Tests on GddAmount --- .../Template/ContentHeader/GddAmount.spec.js | 96 ++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/frontend/src/components/Template/ContentHeader/GddAmount.spec.js b/frontend/src/components/Template/ContentHeader/GddAmount.spec.js index fc7d65cc4..a335b0efb 100644 --- a/frontend/src/components/Template/ContentHeader/GddAmount.spec.js +++ b/frontend/src/components/Template/ContentHeader/GddAmount.spec.js @@ -1,20 +1,31 @@ import { mount } from '@vue/test-utils' import GddAmount from './GddAmount' +import { updateUserInfos } from '@/graphql/mutations' +import flushPromises from 'flush-promises' + +import { toastErrorSpy, toastSuccessSpy } from '@test/testSetup' const localVue = global.localVue +const mockAPICall = jest.fn() +const storeCommitMock = jest.fn() + const state = { - language: 'en', + hideAmountGDD: false, } const mocks = { $store: { state, + commit: storeCommitMock, }, $i18n: { locale: 'en', }, $t: jest.fn((t) => t), + $apollo: { + mutate: mockAPICall, + }, } const propsData = { @@ -38,5 +49,88 @@ describe('GddAmount', () => { it('renders the component gdd-amount', () => { expect(wrapper.find('div.gdd-amount').exists()).toBe(true) }) + + describe('API throws exception', () => { + beforeEach(async () => { + mockAPICall.mockRejectedValue({ + message: 'Ouch', + }) + jest.clearAllMocks() + await wrapper.find('div.border-left svg').trigger('click') + await flushPromises() + }) + + it('toasts an error message', () => { + expect(toastErrorSpy).toBeCalledWith('Ouch') + }) + }) + + describe('API call successful', () => { + beforeEach(async () => { + mockAPICall.mockResolvedValue({ + data: { + updateUserInfos: { + validValues: 1, + }, + }, + }) + jest.clearAllMocks() + await wrapper.find('div.border-left svg').trigger('click') + await flushPromises() + }) + + it('calls the API', () => { + expect(mockAPICall).toBeCalledWith( + expect.objectContaining({ + mutation: updateUserInfos, + variables: { + hideAmountGDD: true, + }, + }), + ) + }) + + it('commits hideAmountGDD to store', () => { + expect(storeCommitMock).toBeCalledWith('hideAmountGDD', true) + }) + + it('toasts a success message', () => { + expect(toastSuccessSpy).toBeCalledWith('settings.showAmountGDD') + }) + }) + }) + + describe.skip('second call to API', () => { + beforeEach(async () => { + mockAPICall.mockResolvedValue({ + data: { + updateUserInfos: { + validValues: 1, + }, + }, + }) + jest.clearAllMocks() + await wrapper.find('div.border-left svg').trigger('click') + await flushPromises() + }) + + it('calls the API', () => { + expect(mockAPICall).toBeCalledWith( + expect.objectContaining({ + mutation: updateUserInfos, + variables: { + hideAmountGDD: true, + }, + }), + ) + }) + + it('commits hideAmountGDD to store', () => { + expect(storeCommitMock).toBeCalledWith('hideAmountGDD', false) + }) + + it('toasts a success message', () => { + expect(toastSuccessSpy).toBeCalledWith('settings.hideAmountGDD') + }) }) })