|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- 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)))
-
- 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', () => {
- it('should throw an error on NaN', () => {
- expect(() => mediumDateTime(NaN)).toThrow(RangeError)
- })
-
- it('should return a formatted string', () => {
- fc.assert(
- fc.property(
- fc.integer(),
- v => {
- expect(typeof mediumDateTime(v)).toBe('string')
- }
- )
- )
- })
- })
-
- describe('on string arguments', () => {
- it('should throw an error given non-parseable values', () => {
- fc.assert(
- fc.property(
- invalidDateString(),
- v => {
- expect(() => mediumDateTime(v)).toThrow(RangeError)
- }
- )
- )
- })
- it('should return a string given parseable values', () => {
- fc.assert(
- fc.property(
- validDateString(),
- v => {
- expect(typeof mediumDateTime(v)).toBe('string')
- }
- )
- )
- })
- })
-
- describe('on object arguments', () => {
- it('should throw an error given non-datelike values', () => {
- fc.assert(
- fc.property(
- fc.object(),
- v => {
- expect(() => mediumDateTime(v)).toThrow(RangeError)
- }
- )
- )
- })
-
- it('should throw an error given non-datelike arguments', () => {
- fc.assert(
- fc.property(
- fc.anything().filter(v => !['number', 'string', 'object'].includes(typeof v)),
- v => {
- expect(() => mediumDateTime(v)).toThrow(TypeError)
- }
- )
- )
- })
-
- it('should return a string given date values', () => {
- fc.assert(
- fc.property(
- fc.date(),
- d => {
- expect(typeof mediumDateTime(d)).toBe('string')
- }
- )
- )
- })
- })
|