From da518654174c8d68f1b817c4a0f7f4a462bd6482 Mon Sep 17 00:00:00 2001 From: iamkun Date: Fri, 21 Aug 2020 10:49:45 +0800 Subject: [PATCH] fix: fix LocaleData plugin to support instance.weekdays() API fix #1017 --- src/plugin/localeData/index.js | 1 + test/plugin/localeData.test.js | 50 ++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/plugin/localeData/index.js b/src/plugin/localeData/index.js index 40032e0b0..71ded86de 100644 --- a/src/plugin/localeData/index.js +++ b/src/plugin/localeData/index.js @@ -18,6 +18,7 @@ export default (o, c, dayjs) => { // locale needed later monthsShort: instance => (instance ? instance.format('MMM') : getShort(this, 'monthsShort', 'months', 3)), firstDayOfWeek: () => this.$locale().weekStart || 0, + weekdays: instance => (instance ? instance.format('dddd') : getShort(this, 'weekdays')), weekdaysMin: instance => (instance ? instance.format('dd') : getShort(this, 'weekdaysMin', 'weekdays', 2)), weekdaysShort: instance => diff --git a/test/plugin/localeData.test.js b/test/plugin/localeData.test.js index 4c46a80a7..109d4780d 100644 --- a/test/plugin/localeData.test.js +++ b/test/plugin/localeData.test.js @@ -1,11 +1,11 @@ import MockDate from 'mockdate' import moment from 'moment' import dayjs from '../../src' -import localeData from '../../src/plugin/localeData' -import localizedFormat from '../../src/plugin/localizedFormat' import '../../src/locale/fr' -import '../../src/locale/zh-cn' import '../../src/locale/ru' +import '../../src/locale/zh-cn' +import localeData from '../../src/plugin/localeData' +import localizedFormat from '../../src/plugin/localizedFormat' dayjs.extend(localizedFormat) dayjs.extend(localeData) @@ -18,24 +18,34 @@ afterEach(() => { MockDate.reset() }) -it('Instance localeData', () => { - const d = dayjs() - const m = moment() - const dayjsLocaleData = dayjs().localeData() - const momentLocaleData = moment().localeData() - expect(dayjsLocaleData.firstDayOfWeek()).toBe(momentLocaleData.firstDayOfWeek()) - expect(dayjsLocaleData.months(d)).toBe(momentLocaleData.months(m)) - expect(dayjsLocaleData.months()).toEqual(momentLocaleData.months()) - expect(dayjsLocaleData.monthsShort(d)).toBe(momentLocaleData.monthsShort(m)) - expect(dayjsLocaleData.monthsShort()).toEqual(momentLocaleData.monthsShort()) - expect(dayjsLocaleData.weekdaysMin(d)).toBe(momentLocaleData.weekdaysMin(m)) - expect(dayjsLocaleData.weekdaysMin()).toEqual(momentLocaleData.weekdaysMin()) - expect(dayjsLocaleData.weekdaysShort(d)).toBe(momentLocaleData.weekdaysShort(m)) - expect(dayjsLocaleData.weekdaysShort()).toEqual(momentLocaleData.weekdaysShort()) - const longDateFormats = ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'] - longDateFormats.forEach((f) => { - expect(dayjsLocaleData.longDateFormat(f)).toEqual(momentLocaleData.longDateFormat(f)) +describe('Instance localeData', () => { + ['zh-cn', 'en', 'fr'].forEach((lo) => { + it(`Locale: ${lo}`, () => { + dayjs.locale(lo) + moment.locale(lo) + const d = dayjs() + const m = moment() + const dayjsLocaleData = dayjs().localeData() + const momentLocaleData = moment().localeData() + expect(dayjsLocaleData.firstDayOfWeek()).toBe(momentLocaleData.firstDayOfWeek()) + expect(dayjsLocaleData.months(d)).toBe(momentLocaleData.months(m)) + expect(dayjsLocaleData.months()).toEqual(momentLocaleData.months()) + expect(dayjsLocaleData.monthsShort(d)).toBe(momentLocaleData.monthsShort(m)) + expect(dayjsLocaleData.monthsShort()).toEqual(momentLocaleData.monthsShort()) + expect(dayjsLocaleData.weekdays(d)).toBe(momentLocaleData.weekdays(m)) + expect(dayjsLocaleData.weekdays()).toEqual(momentLocaleData.weekdays()) + expect(dayjsLocaleData.weekdaysMin(d)).toBe(momentLocaleData.weekdaysMin(m)) + expect(dayjsLocaleData.weekdaysMin()).toEqual(momentLocaleData.weekdaysMin()) + expect(dayjsLocaleData.weekdaysShort(d)).toBe(momentLocaleData.weekdaysShort(m)) + expect(dayjsLocaleData.weekdaysShort()).toEqual(momentLocaleData.weekdaysShort()) + const longDateFormats = ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'] + longDateFormats.forEach((f) => { + expect(dayjsLocaleData.longDateFormat(f)).toEqual(momentLocaleData.longDateFormat(f)) + }) + }) }) + dayjs.locale('en') + moment.locale('en') })