Useful methods for date/time management.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

95 lignes
2.2 KiB

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