|
| 1 | +import moment from 'moment' |
| 2 | +import MockDate from 'mockdate' |
| 3 | +import dayjs from '../../src' |
| 4 | +import relativeTime from '../../src/plugin/relativeTime' |
| 5 | +import advancedFormat from '../../src/plugin/advancedFormat' |
| 6 | +import '../../src/locale/bg' |
| 7 | + |
| 8 | +dayjs.extend(relativeTime) |
| 9 | +dayjs.extend(advancedFormat) |
| 10 | + |
| 11 | +beforeEach(() => { |
| 12 | + MockDate.set(new Date()) |
| 13 | +}) |
| 14 | + |
| 15 | +afterEach(() => { |
| 16 | + MockDate.reset() |
| 17 | +}) |
| 18 | + |
| 19 | +it('Format Month with locale function', () => { |
| 20 | + for (let i = 0; i <= 7; i += 1) { |
| 21 | + const dayjsBG = dayjs().locale('bg').add(i, 'day') |
| 22 | + const momentBG = moment().locale('bg').add(i, 'day') |
| 23 | + const testFormat1 = 'DD MMMM YYYY MMM' |
| 24 | + const testFormat2 = 'MMMM' |
| 25 | + const testFormat3 = 'MMM' |
| 26 | + expect(dayjsBG.format(testFormat1)).toEqual(momentBG.format(testFormat1)) |
| 27 | + expect(dayjsBG.format(testFormat2)).toEqual(momentBG.format(testFormat2)) |
| 28 | + expect(dayjsBG.format(testFormat3)).toEqual(momentBG.format(testFormat3)) |
| 29 | + } |
| 30 | +}) |
| 31 | + |
| 32 | +it('RelativeTime: Time from X', () => { |
| 33 | + const T = [ |
| 34 | + [44.4, 'second'], // a few seconds |
| 35 | + [89.5, 'second'], // a minute |
| 36 | + [130, 'second'], // two minutes |
| 37 | + [43, 'minute'], // 44 minutes |
| 38 | + [1, 'hour'], // 1 hour |
| 39 | + [21, 'hour'], // 21 hours |
| 40 | + [2, 'day'], // 2 days |
| 41 | + [25, 'day'], // 25 days |
| 42 | + [2, 'month'], // 2 months |
| 43 | + [10, 'month'], // 10 months |
| 44 | + [18, 'month'], // 2 years |
| 45 | + [15, 'year'] // 15 years |
| 46 | + ] |
| 47 | + |
| 48 | + T.forEach((t) => { |
| 49 | + dayjs.locale('bg') |
| 50 | + moment.locale('bg') |
| 51 | + expect(dayjs().from(dayjs().add(t[0], t[1]))) |
| 52 | + .toBe(moment().from(moment().add(t[0], t[1]))) |
| 53 | + expect(dayjs().from(dayjs().add(t[0], t[1]), true)) |
| 54 | + .toBe(moment().from(moment().add(t[0], t[1]), true)) |
| 55 | + }) |
| 56 | +}) |
| 57 | + |
| 58 | +it('Ordinal', () => { |
| 59 | + dayjs.locale('bg') |
| 60 | + moment.locale('bg') |
| 61 | + |
| 62 | + for (let d = 1; d <= 31; d += 1) { |
| 63 | + const day = d < 10 ? `0${d}` : d |
| 64 | + const date = `2021-01-${day}` |
| 65 | + expect(dayjs(date).format('Do')).toBe(moment(date).format('Do')) |
| 66 | + } |
| 67 | +}) |
0 commit comments