-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
49 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 37 additions & 94 deletions
131
packages/core/src/fields/types/timestamp/views/__tests__/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,119 +1,62 @@ | ||
import { formatISO } from 'date-fns' | ||
import { type FieldControllerConfig } from '../../../../../types' | ||
import { controller, type TimestampFieldMeta } from '../index' | ||
const STUBCONFIG: FieldControllerConfig<TimestampFieldMeta> = { | ||
import { controller, } from '../index' | ||
|
||
const STUBCONFIG = { | ||
listKey: 'timestamp', | ||
path: './timestamp', | ||
label: 'timestmap', | ||
label: 'foo', | ||
customViews: {}, | ||
fieldMeta: { defaultValue: null, isRequired: false, updatedAt: false }, | ||
description: null, | ||
} | ||
|
||
describe('controller', () => { | ||
describe('validate', () => { | ||
it('should return true if neither date nor time value are specified', () => { | ||
const { validate } = controller(STUBCONFIG) | ||
it('null is OK if not required', () => { | ||
const { validate } = controller({ | ||
...STUBCONFIG, | ||
fieldMeta: { | ||
defaultValue: null, | ||
isRequired: false, | ||
updatedAt: false | ||
} | ||
}) | ||
expect( | ||
validate!({ | ||
kind: 'create', | ||
value: { dateValue: null, timeValue: { kind: 'parsed', value: null } }, | ||
value: null | ||
}) | ||
).toBe(true) | ||
}) | ||
it('should return true if both date and time values are valid', () => { | ||
const { validate } = controller(STUBCONFIG) | ||
const value = { | ||
kind: 'create', | ||
value: { | ||
dateValue: formatISO(new Date(), { representation: 'date' }), | ||
timeValue: { | ||
kind: 'parsed', | ||
value: '10:00:00.000', | ||
}, | ||
}, | ||
} as const | ||
expect(validate!(value)).toBe(true) | ||
}) | ||
it('should return false if only the date value is missing', () => { | ||
const { validate } = controller(STUBCONFIG) | ||
expect( | ||
validate!({ | ||
kind: 'create', | ||
value: { | ||
dateValue: null, | ||
timeValue: { | ||
kind: 'parsed', | ||
value: '10:00:00.000', | ||
}, | ||
}, | ||
}) | ||
).toBe(false) | ||
}) | ||
it('should return false if only the time value is missing', () => { | ||
const { validate } = controller(STUBCONFIG) | ||
expect( | ||
validate!({ | ||
kind: 'create', | ||
value: { | ||
dateValue: formatISO(new Date(), { representation: 'date' }), | ||
timeValue: { kind: 'parsed', value: null }, | ||
}, | ||
}) | ||
).toBe(false) | ||
}) | ||
}) | ||
describe('serialize', () => { | ||
it('should return null if neither date nor time value is specified', () => { | ||
const { serialize } = controller(STUBCONFIG) | ||
expect( | ||
serialize({ | ||
kind: 'create', | ||
value: { | ||
dateValue: null, | ||
timeValue: { kind: 'parsed', value: null }, | ||
}, | ||
}) | ||
).toStrictEqual({ | ||
[STUBCONFIG.path]: null, | ||
it('isRequired enforces required (null)', () => { | ||
const { validate } = controller({ | ||
...STUBCONFIG, | ||
fieldMeta: { | ||
defaultValue: null, | ||
isRequired: true, | ||
updatedAt: false | ||
} | ||
}) | ||
}) | ||
it('should return null if an invalid time value is specified', () => { | ||
const { serialize } = controller(STUBCONFIG) | ||
expect( | ||
serialize({ | ||
validate!({ | ||
kind: 'create', | ||
value: { | ||
dateValue: '2020-10-20', | ||
timeValue: 'hello', | ||
}, | ||
value: null | ||
}) | ||
).toStrictEqual({ | ||
[STUBCONFIG.path]: null, | ||
).toBe('foo is required') | ||
}) | ||
it('isRequired enforces required (value)', () => { | ||
const { validate } = controller({ | ||
...STUBCONFIG, | ||
fieldMeta: { | ||
defaultValue: null, | ||
isRequired: true, | ||
updatedAt: false | ||
} | ||
}) | ||
}) | ||
it('should return null if no dateValue is specified', () => { | ||
const { serialize } = controller(STUBCONFIG) | ||
expect( | ||
serialize({ | ||
validate!({ | ||
kind: 'create', | ||
value: { | ||
dateValue: null, | ||
timeValue: { kind: 'parsed', value: '10:00:00.000' }, | ||
}, | ||
value: new Date().toJSON() | ||
}) | ||
).toStrictEqual({ | ||
[STUBCONFIG.path]: null, | ||
}) | ||
}) | ||
it('should return a valid ISO8601 string if a valid time and date value are specified', () => { | ||
const { serialize } = controller(STUBCONFIG) | ||
expect( | ||
serialize({ | ||
kind: 'create', | ||
value: { dateValue: '2020-10-20', timeValue: { kind: 'parsed', value: '10:00:00.000' } }, | ||
})[STUBCONFIG.path] | ||
).toEqual(new Date('2020-10-20T10:00:00.000').toISOString()) | ||
).toBe(true) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters