From 5485ca3745ba6cc9a919a86bd5f1b4f778acdb4a Mon Sep 17 00:00:00 2001 From: Basil Goryachev Date: Fri, 14 Jun 2024 03:02:38 -0300 Subject: [PATCH] fix: Wrong weekday names when using `format-locale` in overridden timezone --- src/VueDatePicker/utils/util.ts | 2 +- tests/unit/utils.spec.ts | 40 ++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/VueDatePicker/utils/util.ts b/src/VueDatePicker/utils/util.ts index ec6ca0f5..d9910f74 100644 --- a/src/VueDatePicker/utils/util.ts +++ b/src/VueDatePicker/utils/util.ts @@ -35,7 +35,7 @@ function dayNameIntlMapper(locale: string) { function dayNameDateFnsMapper(formatLocale: Locale) { return (day: number) => { - return format(new Date(`2017-01-0${day}T00:00:00+00:00`), 'EEEEEE', { locale: formatLocale }); + return format(localToTz(new Date(`2017-01-0${day}T00:00:00+00:00`), 'UTC'), 'EEEEEE', { locale: formatLocale }); }; } diff --git a/tests/unit/utils.spec.ts b/tests/unit/utils.spec.ts index 19d89aac..aa8996c4 100644 --- a/tests/unit/utils.spec.ts +++ b/tests/unit/utils.spec.ts @@ -122,26 +122,60 @@ describe('Utils and date utils formatting', () => { expect(years[1].value).toEqual(2022); }); - it('Should get month values according to locale', () => { + it('Should get long month values according to locale', () => { const months = getMonths(null, 'en', 'long'); expect(months).toHaveLength(12); expect(months[0].text).toEqual('January'); + expect(months[1].text).toEqual('February'); + expect(months[2].text).toEqual('March'); }); - it('Should get month values according to formatLocale', () => { + it('Should get short month values according to locale', () => { + const months = getMonths(null, 'en', 'short'); + + expect(months).toHaveLength(12); + expect(months[0].text).toEqual('Jan'); + expect(months[1].text).toEqual('Feb'); + expect(months[2].text).toEqual('Mar'); + }); + + it('Should get long month values according to formatLocale', () => { const months = getMonths(de, 'en', 'long'); expect(months).toHaveLength(12); expect(months[0].text).toEqual('Januar'); + expect(months[1].text).toEqual('Februar'); + expect(months[2].text).toEqual('März'); }); - it('Should get month values by fallback to locale', () => { + it('Should get long month values by fallback to locale', () => { // Pass incorrect formatLocale const months = getMonths(null, 'de', 'long'); expect(months).toHaveLength(12); expect(months[0].text).toEqual('Januar'); + expect(months[1].text).toEqual('Februar'); + expect(months[2].text).toEqual('März'); + }); + + it('Should get short month values according to formatLocale', () => { + const months = getMonths(de, 'en', 'short'); + + expect(months).toHaveLength(12); + expect(months[0].text).toEqual('Jan'); + expect(months[1].text).toEqual('Feb'); + expect(months[2].text).toEqual('Mär'); + }); + + it('Should get short month values by fallback to locale', () => { + // Pass incorrect formatLocale + const months = getMonths({}, 'de', 'short'); + + expect(months).toHaveLength(12); + expect(months[0].text).toEqual('Jan'); + expect(months[1].text).toEqual('Feb'); + expect(months[2].text).toEqual('Mär'); }); it('Should get default pattern', () => {