Useful methods for date/time management.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

97 linhas
2.3 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. const invalidDate = () => fc.object().filter(o => !(o as unknown as Date))
  8. it('should exist', () => {
  9. expect(mediumDateTime).toBeDefined()
  10. })
  11. it('should be a callable', () => {
  12. expect(typeof mediumDateTime).toBe('function')
  13. })
  14. it('should accept a minimum of 1 argument', () => {
  15. expect(mediumDateTime).toHaveLength(1)
  16. })
  17. describe('on numeric arguments', () => {
  18. describe('on invalid values', () => {
  19. it('should throw an error on NaN', () => {
  20. expect(() => mediumDateTime(NaN)).toThrow(RangeError)
  21. })
  22. })
  23. describe('on valid values', () => {
  24. it('should return a formatted string', () => {
  25. fc.assert(
  26. fc.property(
  27. fc.integer(),
  28. v => {
  29. expect(typeof mediumDateTime(v)).toBe('string')
  30. }
  31. )
  32. )
  33. })
  34. })
  35. })
  36. describe('on string arguments', () => {
  37. describe('on invalid values', () => {
  38. it('should throw an error', () => {
  39. fc.assert(
  40. fc.property(
  41. invalidDateString(),
  42. v => {
  43. expect(() => mediumDateTime(v)).toThrow(RangeError)
  44. }
  45. )
  46. )
  47. })
  48. })
  49. describe('on valid values', () => {
  50. it('should return a formatted string', () => {
  51. fc.assert(
  52. fc.property(
  53. validDateString(),
  54. v => {
  55. expect(typeof mediumDateTime(v)).toBe('string')
  56. }
  57. )
  58. )
  59. })
  60. })
  61. })
  62. describe('on object arguments', () => {
  63. describe('on non-datelike values', () => {
  64. it('should throw an error', () => {
  65. fc.assert(
  66. fc.property(
  67. invalidDate(),
  68. v => {
  69. expect(() => mediumDateTime(v)).toThrow(RangeError)
  70. }
  71. )
  72. )
  73. })
  74. })
  75. })
  76. describe('on non-datelike arguments', () => {
  77. it('should throw an error', () => {
  78. fc.assert(
  79. fc.property(
  80. fc.anything().filter(v => !['number', 'string', 'object'].includes(typeof v)),
  81. v => {
  82. expect(() => mediumDateTime(v)).toThrow(TypeError)
  83. }
  84. )
  85. )
  86. })
  87. })