|
|
@@ -0,0 +1,96 @@ |
|
|
|
import * as fc from 'fast-check' |
|
|
|
import mediumDateTime from './mediumDateTime' |
|
|
|
|
|
|
|
// fast-check arbitraries |
|
|
|
const isInvalidDate = (d: Date) => isNaN(d.getTime()) |
|
|
|
const invalidDateString = () => fc.string().filter(s => isInvalidDate(new Date(s))) |
|
|
|
const validDateString = () => fc.string().filter(s => !isInvalidDate(new Date(s))) |
|
|
|
const invalidDate = () => fc.object().filter(o => !(o as unknown as Date)) |
|
|
|
|
|
|
|
it('should exist', () => { |
|
|
|
expect(mediumDateTime).toBeDefined() |
|
|
|
}) |
|
|
|
|
|
|
|
it('should be a callable', () => { |
|
|
|
expect(typeof mediumDateTime).toBe('function') |
|
|
|
}) |
|
|
|
|
|
|
|
it('should accept a minimum of 1 argument', () => { |
|
|
|
expect(mediumDateTime).toHaveLength(1) |
|
|
|
}) |
|
|
|
|
|
|
|
describe('on numeric arguments', () => { |
|
|
|
describe('on invalid values', () => { |
|
|
|
it('should throw an error on NaN', () => { |
|
|
|
expect(() => mediumDateTime(NaN)).toThrow(RangeError) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
describe('on valid values', () => { |
|
|
|
it('should return a formatted string', () => { |
|
|
|
fc.assert( |
|
|
|
fc.property( |
|
|
|
fc.integer(), |
|
|
|
v => { |
|
|
|
expect(typeof mediumDateTime(v)).toBe('string') |
|
|
|
} |
|
|
|
) |
|
|
|
) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
describe('on string arguments', () => { |
|
|
|
describe('on invalid values', () => { |
|
|
|
it('should throw an error', () => { |
|
|
|
fc.assert( |
|
|
|
fc.property( |
|
|
|
invalidDateString(), |
|
|
|
v => { |
|
|
|
expect(() => mediumDateTime(v)).toThrow(RangeError) |
|
|
|
} |
|
|
|
) |
|
|
|
) |
|
|
|
}) |
|
|
|
}) |
|
|
|
describe('on valid values', () => { |
|
|
|
it('should return a formatted string', () => { |
|
|
|
fc.assert( |
|
|
|
fc.property( |
|
|
|
validDateString(), |
|
|
|
v => { |
|
|
|
expect(typeof mediumDateTime(v)).toBe('string') |
|
|
|
} |
|
|
|
) |
|
|
|
) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
describe('on object arguments', () => { |
|
|
|
describe('on non-datelike values', () => { |
|
|
|
it('should throw an error', () => { |
|
|
|
fc.assert( |
|
|
|
fc.property( |
|
|
|
invalidDate(), |
|
|
|
v => { |
|
|
|
expect(() => mediumDateTime(v)).toThrow(RangeError) |
|
|
|
} |
|
|
|
) |
|
|
|
) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
describe('on non-datelike arguments', () => { |
|
|
|
it('should throw an error', () => { |
|
|
|
fc.assert( |
|
|
|
fc.property( |
|
|
|
fc.anything().filter(v => !['number', 'string', 'object'].includes(typeof v)), |
|
|
|
v => { |
|
|
|
expect(() => mediumDateTime(v)).toThrow(TypeError) |
|
|
|
} |
|
|
|
) |
|
|
|
) |
|
|
|
}) |
|
|
|
}) |