UNPKG

@cainiaofe/cn-ui-m

Version:
112 lines (111 loc) 5.68 kB
import dayjs from 'dayjs'; import { getValueFromDayjs } from '../get-value-from-dayjs'; describe('getValueFromDayjs', function () { test('should return the timestamp of a single dayjs object without processing time', function () { var date = dayjs('2022-01-01'); var timestamp = getValueFromDayjs(date); expect(timestamp).toBe(date.valueOf()); }); test('should return the timestamp of a single dayjs object with processing time', function () { var date = dayjs('2022-01-01'); var timestamp = getValueFromDayjs(date, true); expect(timestamp).toBe(date.startOf('date').valueOf()); }); test('should return an array of timestamps when given an array of dayjs objects without processing time', function () { var dates = [ dayjs('2022-01-01'), dayjs('2022-01-02'), dayjs('2022-01-03'), ]; var timestamps = getValueFromDayjs(dates); expect(timestamps).toEqual(dates.map(function (date) { return date.valueOf(); })); }); test('should return an array of timestamps when given an array of dayjs objects with processing time', function () { var dates = [ dayjs('2022-01-01'), dayjs('2022-01-02'), dayjs('2022-01-03'), ]; var timestamps = getValueFromDayjs(dates, true); expect(timestamps).toEqual(dates.map(function (date) { return date.startOf('date').valueOf(); })); }); test('should return the end of day timestamp when endOfDay is true', function () { var date = dayjs('2022-01-01'); var timestamp = getValueFromDayjs(date, true, true); expect(timestamp).toBe(date.endOf('date').valueOf()); }); test('should return an array of end of day timestamps when endOfDay is an array', function () { var dates = [ dayjs('2022-01-01'), dayjs('2022-01-02'), dayjs('2022-01-03'), ]; var timestamps = getValueFromDayjs(dates, true, [true, true, true]); expect(timestamps).toEqual(dates.map(function (date) { return date.endOf('date').valueOf(); })); }); test('should return the end of day timestamp when endOfDay is true', function () { var date = dayjs('2023-01-01'); var result = getValueFromDayjs(date, true, true); expect(result).toBe(date.endOf('date').valueOf()); }); test('should return the start of quarter timestamp when mode is quarter', function () { var date = dayjs('2023-01-01'); var result = getValueFromDayjs(date, true, false, false, 'quarter'); expect(result).toBe(date.startOf('date').valueOf()); }); test('should return the end of quarter timestamp when mode is quarter', function () { var date = dayjs('2023-03-31'); var result = getValueFromDayjs(date, true, true, false, 'quarter'); expect(result).toBe(date.endOf('date').valueOf()); }); test('should return an array of timestamps for a range of Dayjs objects', function () { var dates = [dayjs('2023-01-01'), dayjs('2023-01-02')]; var result = getValueFromDayjs(dates); expect(result).toEqual(dates.map(function (date) { return date.valueOf(); })); }); test('should return an array of start and end timestamps for a range of Dayjs objects when isEndOfRange is true', function () { var dates = [dayjs('2023-01-01'), dayjs('2023-01-02')]; var result = getValueFromDayjs(dates, true, false, true, 'date'); expect(result).toEqual([ dates[0].startOf('date').valueOf(), dates[1].endOf('date').valueOf(), ]); }); test('should return an array of start and end timestamps for a range of Dayjs objects when isEndOfRange is true and mode is year', function () { var dates = [dayjs('2023-01-01'), dayjs('2023-01-02')]; var result = getValueFromDayjs(dates, true, false, true, 'year'); expect(result).toEqual([ dates[0].startOf('year').valueOf(), dates[1].endOf('year').valueOf(), ]); }); test('should return an array of start and end timestamps for a range of Dayjs objects when isEndOfRange is true and mode is month', function () { var dates = [dayjs('2023-01-01'), dayjs('2023-01-02')]; var result = getValueFromDayjs(dates, true, false, true, 'month'); expect(result).toEqual([ dates[0].startOf('month').valueOf(), dates[1].endOf('month').valueOf(), ]); }); test('should return an array of start and end timestamps for a range of Dayjs objects when isEndOfRange is true and mode is week', function () { var dates = [dayjs('2023-01-01'), dayjs('2023-01-02')]; var result = getValueFromDayjs(dates, true, false, true, 'week'); expect(result).toEqual([ dates[0].startOf('week').valueOf(), dates[1].endOf('week').valueOf(), ]); }); test('should return an array of start and end timestamps for a range of Dayjs objects when isEndOfRange is true and mode is quarter', function () { var dates = [dayjs('2023-01-11'), dayjs('2023-01-02')]; var resultDates = [dayjs('2023-01-01'), dayjs('2023-03-31')]; var result = getValueFromDayjs(dates, true, false, true, 'quarter'); expect(result).toEqual([ resultDates[0].startOf('date').valueOf(), resultDates[1].endOf('date').valueOf(), ]); }); test('should return undefined for invalid Dayjs object', function () { var result = getValueFromDayjs(null); expect(result).toBeUndefined(); }); });