Skip to content

Commit

Permalink
fix: fix LocaleData plugin to support instance.weekdays() API (#1019)
Browse files Browse the repository at this point in the history
fix #1017
  • Loading branch information
iamkun authored Aug 22, 2020
1 parent e1890c9 commit a09d259
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/plugin/localeData/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down
50 changes: 30 additions & 20 deletions test/plugin/localeData.test.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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')
})


Expand Down

0 comments on commit a09d259

Please sign in to comment.