Skip to content

Commit

Permalink
fix(antd/next): fix tool methods and provide simple unit tests (#2694)
Browse files Browse the repository at this point in the history
  • Loading branch information
henryzp authored Dec 29, 2021
1 parent 9ebdd9c commit 475d10e
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 6 deletions.
48 changes: 48 additions & 0 deletions packages/antd/__tests__/moment.spec.ts
Original file line number Diff line number Diff line change
@@ -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'])
})
12 changes: 9 additions & 3 deletions packages/antd/src/__builtins__/moment.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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)) {
Expand Down
48 changes: 48 additions & 0 deletions packages/next/__tests__/moment.spec.ts
Original file line number Diff line number Diff line change
@@ -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'])
})
12 changes: 9 additions & 3 deletions packages/next/src/__builtins__/moment.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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)) {
Expand Down

0 comments on commit 475d10e

Please sign in to comment.