Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix LocaleData plugin to support instance.weekdays() API #1019

Merged
merged 1 commit into from
Aug 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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