jalaliday
Version:
Persian (Jalali, Khorshidi) Plugin for Day.js
96 lines (79 loc) • 2.69 kB
text/typescript
import dayjs from 'dayjs'
import MockDate from 'mockdate'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import jalali from '../src/dayjs/plugin'
dayjs.extend(jalali)
dayjs.calendar('jalali')
type Dayjs = dayjs.Dayjs
describe('plugin.add', () => {
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
it('add 1 day in the middle of the month', () => {
const date = dayjs('1397/06/13', { jalali: true })
const date2 = date.add(1, 'day')
expect(date2.year()).toEqual(date.year())
expect(date2.month()).toEqual(date.month())
expect(date2.date()).toEqual(date.date() + 1)
})
it('add 1 day in the end of the month', () => {
const date = dayjs('1397/06/31', { jalali: true })
const date2 = date.add(1, 'day')
expect(date2.year()).toEqual(date.year())
expect(date2.month()).toEqual(date.month() + 1)
expect(date2.date()).toEqual(1)
})
it('add 1 month in the middle of the year', () => {
const date = dayjs('1397/06/13', { jalali: true })
const date2 = date.add(1, 'month')
expect(date2.year()).toEqual(date.year())
expect(date2.month()).toEqual(date.month() + 1)
expect(date2.date()).toEqual(date.date())
})
it('add 1 month in the end of the year', () => {
const date = dayjs('1397/12/13', { jalali: true })
const date2 = date.add(1, 'month')
expect(date2.year()).toEqual(date.year() + 1)
expect(date2.month()).toEqual(0)
expect(date2.date()).toEqual(date.date())
})
it('add 1 year', () => {
const date = dayjs('1397/12/13', { jalali: true })
const date2 = date.add(1, 'year')
expect(date2.year()).toEqual(date.year() + 1)
expect(date2.month()).toEqual(date.month())
expect(date2.date()).toEqual(date.date())
})
it('add 11 months in the middle of the year', () => {
const date = dayjs('1396/06/13', { jalali: true })
const date2 = date.add(11, 'month')
expect(date2.year()).toEqual(date.year() + 1)
expect(date2.month()).toEqual(date.month() - 1)
expect(date2.date()).toEqual(date.date())
})
describe('add 100 days', () => {
let a: Dayjs
let b: Dayjs
beforeEach(() => {
a = dayjs('1397/06/01', { jalali: true })
b = dayjs('1397/09/10', { jalali: true })
})
it('add date', () => {
expect(a.add(100, 'day')).toEqual(b)
})
})
describe('add 1 month in shahrivar 31th', () => {
let a: Dayjs
let b: Dayjs
beforeEach(() => {
a = dayjs('1397/06/31', { jalali: true })
b = dayjs('1397/07/30', { jalali: true })
})
it('add date', () => {
expect(a.add(1, 'month')).toEqual(b)
})
})
})