|
| 1 | +import MockDate from 'mockdate' |
| 2 | +import dayjs from 'dayjs' |
| 3 | +import negativeYear from '../../src/plugin/negativeYear' |
| 4 | +import utc from '../../src/plugin/utc' |
| 5 | +import { REGEX_PARSE } from '../../src/constant' |
| 6 | + |
| 7 | + |
| 8 | +dayjs.extend(negativeYear) |
| 9 | +dayjs.extend(utc) |
| 10 | + |
| 11 | +beforeEach(() => { |
| 12 | + MockDate.set(new Date()) |
| 13 | +}) |
| 14 | + |
| 15 | +afterEach(() => { |
| 16 | + MockDate.reset() |
| 17 | +}) |
| 18 | + |
| 19 | +describe('negativeYear', () => { |
| 20 | + it('parses negative years', () => { |
| 21 | + expect(dayjs('-2020-01-01').year()).toBe(-2020) |
| 22 | + const date = '-2021/01/03' |
| 23 | + const date2 = '01/03/-2021' |
| 24 | + const date3 = '01-03--2021' |
| 25 | + const d = date.match(REGEX_PARSE) |
| 26 | + expect(dayjs(date).format('YYYY-MM-DD')).toBe('-2021-01-03') |
| 27 | + expect(dayjs(date2).format('YYYY-MM-DD')).toBe('Invalid Date') |
| 28 | + expect(dayjs(date3).format()).toBe('Invalid Date') |
| 29 | + expect(d).toBe(null) |
| 30 | + }) |
| 31 | + |
| 32 | + it('does not parse non-negative years', () => { |
| 33 | + expect(dayjs('2020-01-01').year()).toBe(2020) |
| 34 | + }) |
| 35 | + |
| 36 | + it('works with other plugins', () => { |
| 37 | + expect(dayjs.utc('-2020-01-01').year()).toBe(-2020) |
| 38 | + }) |
| 39 | + |
| 40 | + it('Add and subtract with negative years', () => { |
| 41 | + expect(dayjs('-2006').add(1, 'y')).toEqual(dayjs('-2005')) |
| 42 | + expect(dayjs('-2006').subtract(1, 'y')).toEqual(dayjs('-2007')) |
| 43 | + expect(dayjs('-2006').add(1, 'y').format('YYYY')).toBe(dayjs('-2005').format('YYYY')) |
| 44 | + expect(dayjs('-2006').subtract(1, 'y').format('YYYY')).toBe(dayjs('-2007').format('YYYY')) |
| 45 | + }) |
| 46 | + |
| 47 | + it('Compare date with negative years', () => { |
| 48 | + expect(dayjs('-2006').isAfter(dayjs('-2007'))).toBeTruthy() |
| 49 | + expect(dayjs('-2006').isBefore(dayjs('-2005'))).toBeTruthy() |
| 50 | + expect(dayjs('-2006').isSame('-2006')).toBeTruthy() |
| 51 | + }) |
| 52 | +}) |
0 commit comments