Useful methods for date/time management.
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mediumDateTime.test.ts 2.2 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import * as fc from 'fast-check'
  2. import mediumDateTime from './mediumDateTime'
  3. // fast-check arbitraries
  4. const isInvalidDate = (d: Date) => isNaN(d.getTime())
  5. const invalidDateString = () => fc.string().filter(s => isInvalidDate(new Date(s)))
  6. const validDateString = () => fc.string().filter(s => !isInvalidDate(new Date(s)))
  7. it('should exist', () => {
  8. expect(mediumDateTime).toBeDefined()
  9. })
  10. it('should be a callable', () => {
  11. expect(typeof mediumDateTime).toBe('function')
  12. })
  13. it('should accept a minimum of 1 argument', () => {
  14. expect(mediumDateTime).toHaveLength(1)
  15. })
  16. describe('on numeric arguments', () => {
  17. it('should throw an error on NaN', () => {
  18. expect(() => mediumDateTime(NaN)).toThrow(RangeError)
  19. })
  20. it('should return a formatted string', () => {
  21. fc.assert(
  22. fc.property(
  23. fc.integer(),
  24. v => {
  25. expect(typeof mediumDateTime(v)).toBe('string')
  26. }
  27. )
  28. )
  29. })
  30. })
  31. describe('on string arguments', () => {
  32. it('should throw an error given non-parseable values', () => {
  33. fc.assert(
  34. fc.property(
  35. invalidDateString(),
  36. v => {
  37. expect(() => mediumDateTime(v)).toThrow(RangeError)
  38. }
  39. )
  40. )
  41. })
  42. it('should return a string given parseable values', () => {
  43. fc.assert(
  44. fc.property(
  45. validDateString(),
  46. v => {
  47. expect(typeof mediumDateTime(v)).toBe('string')
  48. }
  49. )
  50. )
  51. })
  52. })
  53. describe('on object arguments', () => {
  54. it('should throw an error given non-datelike values', () => {
  55. fc.assert(
  56. fc.property(
  57. fc.object(),
  58. v => {
  59. expect(() => mediumDateTime(v)).toThrow(RangeError)
  60. }
  61. )
  62. )
  63. })
  64. it('should throw an error given non-datelike arguments', () => {
  65. fc.assert(
  66. fc.property(
  67. fc.anything().filter(v => !['number', 'string', 'object'].includes(typeof v)),
  68. v => {
  69. expect(() => mediumDateTime(v)).toThrow(TypeError)
  70. }
  71. )
  72. )
  73. })
  74. it('should return a string given date values', () => {
  75. fc.assert(
  76. fc.property(
  77. fc.date(),
  78. d => {
  79. expect(typeof mediumDateTime(d)).toBe('string')
  80. }
  81. )
  82. )
  83. })
  84. })