From 475d10e918e831721c774bb87765469a84297036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=BF=BC?= Date: Wed, 29 Dec 2021 14:23:35 +0800 Subject: [PATCH] fix(antd/next): fix tool methods and provide simple unit tests (#2694) --- packages/antd/__tests__/moment.spec.ts | 48 ++++++++++++++++++++++++ packages/antd/src/__builtins__/moment.ts | 12 ++++-- packages/next/__tests__/moment.spec.ts | 48 ++++++++++++++++++++++++ packages/next/src/__builtins__/moment.ts | 12 ++++-- 4 files changed, 114 insertions(+), 6 deletions(-) create mode 100644 packages/antd/__tests__/moment.spec.ts create mode 100644 packages/next/__tests__/moment.spec.ts diff --git a/packages/antd/__tests__/moment.spec.ts b/packages/antd/__tests__/moment.spec.ts new file mode 100644 index 00000000000..946fa925860 --- /dev/null +++ b/packages/antd/__tests__/moment.spec.ts @@ -0,0 +1,48 @@ +import moment from 'moment' +import { formatMomentValue, momentable } from '../src/__builtins__/moment' + +test('momentable is usable', () => { + expect(moment.isMoment(momentable('2021-09-08'))).toBe(true) + expect( + momentable(['2021-09-08', '2021-12-29']).every((item) => + moment.isMoment(item) + ) + ).toBe(true) +}) + +test('formatMomentValue is usable', () => { + expect(formatMomentValue('', 'YYYY-MM-DD', '~')).toBe('~') + expect(formatMomentValue('2021-12-21 15:47:00', 'YYYY-MM-DD')).toBe( + '2021-12-21' + ) + expect(formatMomentValue('2021-12-21 15:47:00', undefined)).toBe( + '2021-12-21 15:47:00' + ) + expect(formatMomentValue('2021-12-21 15:47:00', (date: string) => date)).toBe( + '2021-12-21 15:47:00' + ) + expect( + formatMomentValue( + ['2021-12-21 15:47:00', '2021-12-29 15:47:00'], + 'YYYY-MM-DD' + ) + ).toEqual(['2021-12-21', '2021-12-29']) + expect( + formatMomentValue( + ['2021-12-21 16:47:00', '2021-12-29 18:47:00'], + (date: string) => date + ) + ).toEqual(['2021-12-21 16:47:00', '2021-12-29 18:47:00']) + expect( + formatMomentValue( + ['2021-12-21 16:47:00', '2021-12-29 18:47:00'], + ['YYYY-MM-DD', (date: string) => date] + ) + ).toEqual(['2021-12-21', '2021-12-29 18:47:00']) + expect( + formatMomentValue( + ['2021-12-21 16:47:00', '2021-12-29 18:47:00'], + ['YYYY-MM-DD', undefined] + ) + ).toEqual(['2021-12-21', '2021-12-29 18:47:00']) +}) diff --git a/packages/antd/src/__builtins__/moment.ts b/packages/antd/src/__builtins__/moment.ts index 3eb6c0edaed..f94c96326f3 100644 --- a/packages/antd/src/__builtins__/moment.ts +++ b/packages/antd/src/__builtins__/moment.ts @@ -1,4 +1,4 @@ -import { isArr, isFn } from '@formily/shared' +import { isArr, isFn, isEmpty } from '@formily/shared' import moment from 'moment' export const momentable = (value: any, format?: string) => { @@ -21,12 +21,18 @@ export const formatMomentValue = ( if (isFn(_format)) { return _format(date) } - return date?.format ? date.format(_format) : date + if (isEmpty(_format)) { + return date + } + return moment(date).format(_format) } else { if (isFn(format)) { return format(date) } - return date?.format ? date.format(format) : date + if (isEmpty(format)) { + return date + } + return moment(date).format(format) } } if (isArr(value)) { diff --git a/packages/next/__tests__/moment.spec.ts b/packages/next/__tests__/moment.spec.ts new file mode 100644 index 00000000000..c352f2dcc0d --- /dev/null +++ b/packages/next/__tests__/moment.spec.ts @@ -0,0 +1,48 @@ +import { momentable, formatMomentValue } from '../src/__builtins__/moment' +import moment from 'moment' + +test('momentable is usable', () => { + expect(moment.isMoment(momentable('2021-09-08'))).toBe(true) + expect( + momentable(['2021-09-08', '2021-12-29']).every((item) => + moment.isMoment(item) + ) + ).toBe(true) +}) + +test('formatMomentValue is usable', () => { + expect(formatMomentValue('', 'YYYY-MM-DD', '~')).toBe('~') + expect(formatMomentValue('2021-12-22 15:47:00', 'YYYY-MM-DD')).toBe( + '2021-12-22' + ) + expect(formatMomentValue('2021-12-23 15:47:00', undefined)).toBe( + '2021-12-23 15:47:00' + ) + expect(formatMomentValue('2021-12-21 15:47:00', (date: string) => date)).toBe( + '2021-12-21 15:47:00' + ) + expect( + formatMomentValue( + ['2021-12-21 15:47:00', '2021-12-29 15:47:00'], + 'YYYY-MM-DD' + ) + ).toEqual(['2021-12-21', '2021-12-29']) + expect( + formatMomentValue( + ['2021-12-21 16:47:00', '2021-12-29 18:47:00'], + (date: string) => date + ) + ).toEqual(['2021-12-21 16:47:00', '2021-12-29 18:47:00']) + expect( + formatMomentValue( + ['2021-12-21 16:47:00', '2021-12-29 18:47:00'], + ['YYYY-MM-DD', (date: string) => date] + ) + ).toEqual(['2021-12-21', '2021-12-29 18:47:00']) + expect( + formatMomentValue( + ['2021-12-21 16:47:00', '2021-12-29 18:47:00'], + ['YYYY-MM-DD', undefined] + ) + ).toEqual(['2021-12-21', '2021-12-29 18:47:00']) +}) diff --git a/packages/next/src/__builtins__/moment.ts b/packages/next/src/__builtins__/moment.ts index 3eb6c0edaed..be0ec9a6406 100644 --- a/packages/next/src/__builtins__/moment.ts +++ b/packages/next/src/__builtins__/moment.ts @@ -1,4 +1,4 @@ -import { isArr, isFn } from '@formily/shared' +import { isArr, isEmpty, isFn } from '@formily/shared' import moment from 'moment' export const momentable = (value: any, format?: string) => { @@ -21,12 +21,18 @@ export const formatMomentValue = ( if (isFn(_format)) { return _format(date) } - return date?.format ? date.format(_format) : date + if (isEmpty(_format)) { + return date + } + return moment(date).format(_format) } else { if (isFn(format)) { return format(date) } - return date?.format ? date.format(format) : date + if (isEmpty(format)) { + return date + } + return moment(date).format(format) } } if (isArr(value)) {