From b8fd965f5029f6283d3775c5ad85b9b7de0f777d Mon Sep 17 00:00:00 2001 From: einhornimmond Date: Fri, 16 May 2025 07:04:06 +0200 Subject: [PATCH] fix frontend test, decouple some more --- .../ContributionMessagesFormular.spec.js | 15 +- .../ContributionMessagesList.spec.js | 12 - .../Contributions/ContributionForm.spec.js | 123 +++++----- .../Contributions/ContributionForm.vue | 3 +- .../Contributions/ContributionList.spec.js | 178 +++++--------- .../Contributions/ContributionListAll.spec.js | 158 +++++++++++++ .../Contributions/ContributionListAll.vue | 2 +- .../ContributionListAllItem.spec.js | 217 ++++++++++++++++++ frontend/src/pages/Community.spec.js | 122 ++-------- 9 files changed, 529 insertions(+), 301 deletions(-) create mode 100644 frontend/src/components/Contributions/ContributionListAll.spec.js create mode 100644 frontend/src/components/Contributions/ContributionListAllItem.spec.js diff --git a/frontend/src/components/ContributionMessages/ContributionMessagesFormular.spec.js b/frontend/src/components/ContributionMessages/ContributionMessagesFormular.spec.js index 1f182218c..a582a0284 100644 --- a/frontend/src/components/ContributionMessages/ContributionMessagesFormular.spec.js +++ b/frontend/src/components/ContributionMessages/ContributionMessagesFormular.spec.js @@ -52,7 +52,7 @@ describe('ContributionMessagesFormular', () => { } beforeEach(() => { - mockMutate.mockResolvedValue({}) + mockMutate.mockResolvedValue({ data: { createContributionMessage: {} } }) wrapper = createWrapper() }) @@ -66,7 +66,7 @@ describe('ContributionMessagesFormular', () => { it('resets the form on reset event', async () => { await wrapper.find('form').trigger('reset') - expect(wrapper.vm.form.text).toBe('') + expect(wrapper.vm.formText).toBe('') }) describe('form submission', () => { @@ -82,19 +82,14 @@ describe('ContributionMessagesFormular', () => { }) }) - it('emits get-list-contribution-messages event', async () => { + it('emits add-contribution-message event', async () => { await nextTick() - expect(wrapper.emitted('get-list-contribution-messages')).toEqual([[false]]) - }) - - it('emits update-status event', async () => { - await nextTick() - expect(wrapper.emitted('update-status')).toEqual([[42]]) + expect(wrapper.emitted('add-contribution-message')).toEqual([[{}]]) }) it('resets the form text', async () => { await nextTick() - expect(wrapper.vm.form.text).toBe('') + expect(wrapper.vm.formText).toBe('') }) it('shows success toast', async () => { diff --git a/frontend/src/components/ContributionMessages/ContributionMessagesList.spec.js b/frontend/src/components/ContributionMessages/ContributionMessagesList.spec.js index 12079a1f7..030ce0af5 100644 --- a/frontend/src/components/ContributionMessages/ContributionMessagesList.spec.js +++ b/frontend/src/components/ContributionMessages/ContributionMessagesList.spec.js @@ -77,16 +77,4 @@ describe('ContributionMessagesList', () => { expect(wrapper.find('button').text()).toContain('form.close') }) }) - - describe('events', () => { - beforeEach(() => { - wrapper = createWrapper() - }) - - it('emits update-status event when updateStatus method is called', async () => { - await wrapper.vm.updateStatus(42) - expect(wrapper.emitted('update-status')).toBeTruthy() - expect(wrapper.emitted('update-status')[0]).toEqual([42]) - }) - }) }) diff --git a/frontend/src/components/Contributions/ContributionForm.spec.js b/frontend/src/components/Contributions/ContributionForm.spec.js index 626d5e062..40a15c855 100644 --- a/frontend/src/components/Contributions/ContributionForm.spec.js +++ b/frontend/src/components/Contributions/ContributionForm.spec.js @@ -1,7 +1,6 @@ import { describe, it, expect, vi, beforeEach } from 'vitest' import { mount } from '@vue/test-utils' import ContributionForm from './ContributionForm.vue' -import { useForm } from 'vee-validate' // Mock external components and dependencies vi.mock('@/components/Inputs/InputAmount', () => ({ @@ -37,12 +36,16 @@ vi.mock('vee-validate', () => ({ })), })) +const global = { + stubs: ['BForm', 'BFormInput', 'BRow', 'BCol', 'BButton'], +} + describe('ContributionForm', () => { let wrapper const defaultProps = { modelValue: { - date: '2024-09-12', + contributionDate: '2024-09-12', memo: 'Test memo', hours: 2, amount: 40, @@ -51,12 +54,26 @@ describe('ContributionForm', () => { maxGddThisMonth: 200, } + const createWrapperWithDate = (date) => { + return mount(ContributionForm, { + props: { + ...defaultProps, + modelValue: { + ...defaultProps.modelValue, + contributionDate: date.toISOString(), + }, + }, + global, + }) + } + const thisMonth = new Date() + const lastMonth = new Date() + lastMonth.setMonth(lastMonth.getMonth() - 1, 1) + beforeEach(() => { wrapper = mount(ContributionForm, { props: defaultProps, - global: { - stubs: ['BForm', 'BFormInput', 'BRow', 'BCol', 'BButton'], - }, + global, }) }) @@ -65,19 +82,17 @@ describe('ContributionForm', () => { }) describe('compute isThisMonth', () => { - it('return true', async () => { - await wrapper.setProps({ - modelValue: { date: new Date().toISOString() }, - }) - expect(wrapper.vm.isThisMonth).toBe(true) - }) - it('return false', async () => { - const now = new Date() - const lastMonth = new Date(now.setMonth(now.getMonth() - 1, 1)) - await wrapper.setProps({ - modelValue: { date: lastMonth.toISOString() }, - }) - expect(wrapper.vm.isThisMonth).toBe(false) + it.each([ + [thisMonth, true, 'should return true for current month'], + [lastMonth, false, 'should return false for last month'], + [ + new Date(new Date().setFullYear(new Date().getFullYear() + 1)), + false, + 'should return false for next year', + ], + ])('%s => %s (%s)', (date, expected, desc) => { + const wrapper = createWrapperWithDate(date) + expect(wrapper.vm.isThisMonth).toBe(expected) }) }) @@ -85,21 +100,39 @@ describe('ContributionForm', () => { it('if both max gdd are > 0', () => { expect(wrapper.vm.noOpenCreation).toBeUndefined() }) - it('if max gdd for this month is 0, and form.date is in last month', async () => { - const now = new Date() - const lastMonth = new Date(now.setMonth(now.getMonth() - 1, 1)) - await wrapper.setProps({ - maxGddThisMonth: 0, - modelValue: { date: lastMonth.toISOString() }, + describe('if form.date is in last month', () => { + beforeEach(() => { + wrapper = createWrapperWithDate(lastMonth) + }) + it('if max gdd for this month is 0', async () => { + await wrapper.setProps({ + maxGddThisMonth: 0, + }) + expect(wrapper.vm.noOpenCreation).toBeUndefined() + }) + it('if max gdd last month is 0', async () => { + await wrapper.setProps({ + maxGddLastMonth: 0, + }) + expect(wrapper.vm.noOpenCreation).toBe('contribution.noOpenCreation.lastMonth') }) - expect(wrapper.vm.noOpenCreation).toBeUndefined() }) - it('if max gdd for last month is 0, and form.date is in this month', async () => { - await wrapper.setProps({ - maxGddLastMonth: 0, - modelValue: { date: new Date().toISOString() }, + describe('if form.date is in this month', () => { + beforeEach(() => { + wrapper = createWrapperWithDate(thisMonth) + }) + it('if max gdd for last month is 0', async () => { + await wrapper.setProps({ + maxGddLastMonth: 0, + }) + expect(wrapper.vm.noOpenCreation).toBeUndefined() + }) + it('if max gdd this month is 0', async () => { + await wrapper.setProps({ + maxGddThisMonth: 0, + }) + expect(wrapper.vm.noOpenCreation).toBe('contribution.noOpenCreation.thisMonth') }) - expect(wrapper.vm.noOpenCreation).toBeUndefined() }) it('if max gdd is 0 for both months', async () => { await wrapper.setProps({ @@ -108,30 +141,12 @@ describe('ContributionForm', () => { }) expect(wrapper.vm.noOpenCreation).toBe('contribution.noOpenCreation.allMonth') }) - it('if max gdd this month is zero and form.date is inside this month', async () => { - await wrapper.setProps({ - maxGddThisMonth: 0, - modelValue: { date: new Date().toISOString() }, - }) - expect(wrapper.vm.noOpenCreation).toBe('contribution.noOpenCreation.thisMonth') - }) - it('if max gdd last month is zero and form.date is inside last month', async () => { - const now = new Date() - const lastMonth = new Date(now.setMonth(now.getMonth() - 1, 1)) - await wrapper.setProps({ - maxGddLastMonth: 0, - modelValue: { date: lastMonth.toISOString() }, - }) - expect(wrapper.vm.noOpenCreation).toBe('contribution.noOpenCreation.lastMonth') - }) }) it('computes disabled correctly', async () => { expect(wrapper.vm.disabled).toBe(true) - await wrapper.setProps({ - modelValue: { date: new Date().toISOString().slice(0, 10) }, - }) + wrapper = createWrapperWithDate(thisMonth) wrapper.vm.form.amount = 100 @@ -154,7 +169,7 @@ describe('ContributionForm', () => { expect(wrapper.vm.form.amount).toBe('60.00') }) - it('emits update-contribution event on submit for existing contribution', async () => { + it('emits upsert-contribution event on submit for existing contribution', async () => { const existingContribution = { ...defaultProps.modelValue, id: '123', @@ -174,17 +189,17 @@ describe('ContributionForm', () => { wrapper.vm.submit() - expect(wrapper.emitted('update-contribution')).toBeTruthy() - expect(wrapper.emitted('update-contribution')[0][0]).toEqual( + expect(wrapper.emitted('upsert-contribution')).toBeTruthy() + expect(wrapper.emitted('upsert-contribution')[0][0]).toEqual( expect.objectContaining({ id: '123', }), ) }) - it('emits set-contribution event on submit for new contribution', async () => { + it('emits upsert-contribution event on submit for new contribution', async () => { wrapper.vm.submit() - expect(wrapper.emitted('set-contribution')).toBeTruthy() + expect(wrapper.emitted('upsert-contribution')).toBeTruthy() }) }) diff --git a/frontend/src/components/Contributions/ContributionForm.vue b/frontend/src/components/Contributions/ContributionForm.vue index 997cdd084..237d38c66 100644 --- a/frontend/src/components/Contributions/ContributionForm.vue +++ b/frontend/src/components/Contributions/ContributionForm.vue @@ -85,11 +85,12 @@