UNPKG

@cainiaofe/cn-ui-m

Version:
37 lines (36 loc) 1.46 kB
import { waitFor } from '@testing-library/react'; import { date2StringByMode } from '../date-to-string-by-mode'; describe('date2StringByMode', function () { test('should convert date to string in year mode', function () { var date = new Date('2022-01-01'); var mode = 'year'; var result = date2StringByMode(date, mode); expect(result).toBe('2022'); }); test('should convert date to string in month mode', function () { var date = new Date('2022-01-01'); var mode = 'month'; var result = date2StringByMode(date, mode); expect(result).toBe('2022-01'); }); test('should convert date to string in quarter mode', function () { var date = new Date('2022-01-01'); var mode = 'quarter'; var result = date2StringByMode(date, mode); waitFor(function () { expect(result).toBe('2022-Q1'); }); }); test('should convert date to string with custom format', function () { var date = new Date('2022-01-01'); var mode = 'year'; var format = 'YY'; var result = date2StringByMode(date, mode, format); expect(result).toBe('22'); }); test('should throw an error for invalid date', function () { var date = 'asdcd'; // Invalid date format var mode = 'year'; expect(function () { return date2StringByMode(date, mode); }).toThrowError('Invalid date'); }); });