From b605300753938e10646d5ebd2bda1a7b74f87be7 Mon Sep 17 00:00:00 2001 From: Clerise Swart Date: Mon, 7 Oct 2024 20:32:52 +0200 Subject: [PATCH 1/3] fix(delivery-schedule): correct weekday mapping for cut-off times Replaced manual date setting with from date-fns to ensure weekdays map correctly, respecting the option. Old: - Used a base date and manually set the day. New: - Utilizes from date-fns, aligning weekday logic with locale settings for more reliable mapping. INT-502 --- apps/sandbox/src/composables/useWeekdays.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/sandbox/src/composables/useWeekdays.ts b/apps/sandbox/src/composables/useWeekdays.ts index 2faa103e..7a8da82f 100644 --- a/apps/sandbox/src/composables/useWeekdays.ts +++ b/apps/sandbox/src/composables/useWeekdays.ts @@ -1,4 +1,5 @@ import {computed, type ComputedRef} from 'vue'; +import {setDay} from 'date-fns'; import {useLanguage} from './useLanguage'; export const useWeekdays = (): ComputedRef => { @@ -6,8 +7,7 @@ export const useWeekdays = (): ComputedRef => { return computed(() => { return Array.from({length: 7}, (_, i) => { - const date = new Date(0); - date.setDate(i + 5); + const date = setDay(new Date(), i, {weekStartsOn: 0}); return date.toLocaleDateString(language.language.value.code, {weekday: 'long'}); }); From c9fbb35f3cb32e533e42384904fa227803282221 Mon Sep 17 00:00:00 2001 From: Clerise Swart Date: Wed, 9 Oct 2024 11:21:02 +0200 Subject: [PATCH 2/3] test:added unit tests --- .../src/composables/useWeekdays.spec.ts | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 apps/sandbox/src/composables/useWeekdays.spec.ts diff --git a/apps/sandbox/src/composables/useWeekdays.spec.ts b/apps/sandbox/src/composables/useWeekdays.spec.ts new file mode 100644 index 00000000..91b708b8 --- /dev/null +++ b/apps/sandbox/src/composables/useWeekdays.spec.ts @@ -0,0 +1,29 @@ +import {nextTick} from 'vue'; +import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'; +import {createPinia, setActivePinia} from 'pinia'; + +/** + * @vitest-environment happy-dom + */ + +describe('useWeekdays', () => { + beforeEach(() => { + setActivePinia(createPinia()); + }); + + afterEach(() => { + vi.resetModules(); + vi.restoreAllMocks(); + }); + + it('uses weekdays', async () => { + expect.assertions(1); + + const {useWeekdays} = await import('./useWeekdays'); + await nextTick(); + + const Weekdays = useWeekdays(); + + expect(Weekdays.value).toEqual(['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']); + }); +}); From 53f5f6965a9fec16e34b1b1b05a3df2887c7fe27 Mon Sep 17 00:00:00 2001 From: Clerise Swart Date: Wed, 9 Oct 2024 11:46:29 +0200 Subject: [PATCH 3/3] test:added unit tests --- apps/sandbox/src/composables/useWeekdays.spec.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/apps/sandbox/src/composables/useWeekdays.spec.ts b/apps/sandbox/src/composables/useWeekdays.spec.ts index 91b708b8..5cdb07cb 100644 --- a/apps/sandbox/src/composables/useWeekdays.spec.ts +++ b/apps/sandbox/src/composables/useWeekdays.spec.ts @@ -16,14 +16,18 @@ describe('useWeekdays', () => { vi.restoreAllMocks(); }); - it('uses weekdays', async () => { - expect.assertions(1); + it('returns weekdays in English for en-US', async () => { + vi.mock('./useLanguage', () => ({ + useLanguage: vi.fn(() => ({ + language: {value: {code: 'en-US'}}, + })), + })); const {useWeekdays} = await import('./useWeekdays'); await nextTick(); - const Weekdays = useWeekdays(); + const weekdays = useWeekdays(); - expect(Weekdays.value).toEqual(['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']); + expect(weekdays.value).toEqual(['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']); }); });