diff --git a/frontend/src/components/Contributions/ContributionForm.spec.js b/frontend/src/components/Contributions/ContributionForm.spec.js index 47ee4a763..626d5e062 100644 --- a/frontend/src/components/Contributions/ContributionForm.spec.js +++ b/frontend/src/components/Contributions/ContributionForm.spec.js @@ -4,13 +4,6 @@ import ContributionForm from './ContributionForm.vue' import { useForm } from 'vee-validate' // Mock external components and dependencies -vi.mock('@/components/Inputs/InputHour', () => ({ - default: { - name: 'InputHour', - template: '', - }, -})) - vi.mock('@/components/Inputs/InputAmount', () => ({ default: { name: 'InputAmount', @@ -54,8 +47,6 @@ describe('ContributionForm', () => { hours: 2, amount: 40, }, - isThisMonth: true, - minimalDate: new Date('2024-01-01'), maxGddLastMonth: 100, maxGddThisMonth: 200, } @@ -73,44 +64,81 @@ describe('ContributionForm', () => { expect(wrapper.find('.contribution-form').exists()).toBe(true) }) - it('computes showMessage correctly', async () => { - expect(wrapper.vm.showMessage).toBe(false) - - await wrapper.setProps({ - maxGddThisMonth: 0, - maxGddLastMonth: 0, + 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) + }) + }) - expect(wrapper.vm.showMessage).toBe(true) + describe('noOpenCreations return correct translation key', () => { + 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() }, + }) + 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() }, + }) + expect(wrapper.vm.noOpenCreation).toBeUndefined() + }) + it('if max gdd is 0 for both months', async () => { + await wrapper.setProps({ + maxGddThisMonth: 0, + maxGddLastMonth: 0, + }) + 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(false) + expect(wrapper.vm.disabled).toBe(true) await wrapper.setProps({ - maxGddThisMonth: 30, + modelValue: { date: new Date().toISOString().slice(0, 10) }, }) wrapper.vm.form.amount = 100 - expect(wrapper.vm.disabled).toBe(true) - }) - - it('computes validMaxGDD correctly', async () => { - expect(wrapper.vm.validMaxGDD).toBe(200) - - await wrapper.setProps({ isThisMonth: false }) - - expect(wrapper.vm.validMaxGDD).toBe(100) + expect(wrapper.vm.disabled).toBe(false) }) it('updates amount when hours change', async () => { - const setFieldValueMock = vi.fn() - vi.mocked(useForm).mockReturnValue({ - ...vi.mocked(useForm)(), - setFieldValue: setFieldValueMock, - }) - wrapper = mount(ContributionForm, { props: defaultProps, global: { @@ -121,9 +149,9 @@ describe('ContributionForm', () => { await wrapper.vm.$nextTick() // Simulate changing hours - wrapper.vm.updateAmount(3) + wrapper.vm.updateField(3, 'hours') - expect(setFieldValueMock).toHaveBeenCalledWith('amount', '60.00') + expect(wrapper.vm.form.amount).toBe('60.00') }) it('emits update-contribution event on submit for existing contribution', async () => { @@ -159,31 +187,4 @@ describe('ContributionForm', () => { expect(wrapper.emitted('set-contribution')).toBeTruthy() }) - - it('resets form on fullFormReset', () => { - const resetFormMock = vi.fn() - vi.mocked(useForm).mockReturnValue({ - ...vi.mocked(useForm)(), - resetForm: resetFormMock, - }) - - wrapper = mount(ContributionForm, { - props: defaultProps, - global: { - stubs: ['BForm', 'BFormInput', 'BRow', 'BCol', 'BButton'], - }, - }) - - wrapper.vm.fullFormReset() - - expect(resetFormMock).toHaveBeenCalledWith({ - values: { - id: null, - date: '', - memo: '', - hours: 0, - amount: '', - }, - }) - }) }) diff --git a/frontend/src/components/Contributions/ContributionForm.vue b/frontend/src/components/Contributions/ContributionForm.vue index 5612ae14b..19540c514 100644 --- a/frontend/src/components/Contributions/ContributionForm.vue +++ b/frontend/src/components/Contributions/ContributionForm.vue @@ -1,63 +1,55 @@ -