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') } ) ) }) })