Useful methods for file-related functions.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.9 KiB

  1. import * as fc from 'fast-check'
  2. import isValidMimeType, { IsValidMimeTypeConfig } from './isValidMimeType'
  3. it('should exist', () => {
  4. expect(isValidMimeType).toBeDefined()
  5. })
  6. it('should be a callable', () => {
  7. expect(typeof isValidMimeType).toBe('function')
  8. })
  9. it('should accept a minimum of 1 argument', () => {
  10. expect(isValidMimeType).toHaveLength(1)
  11. })
  12. it('should throw an error given invalid param shapes', () => {
  13. fc.assert(
  14. fc.property(
  15. fc.object().filter(o => !('validMimeTypes' in o)),
  16. params => {
  17. expect(() => isValidMimeType((params as unknown) as IsValidMimeTypeConfig)).toThrow(TypeError)
  18. }
  19. )
  20. )
  21. })
  22. describe('on valid MIME types', () => {
  23. it('should throw an error given non-string and non-array values', () => {
  24. fc.assert(
  25. fc.property(
  26. fc.record({
  27. validMimeTypes: fc.anything().filter(e => !(typeof e === 'string' || Array.isArray(e))),
  28. }),
  29. params => {
  30. expect(() => isValidMimeType((params as unknown) as IsValidMimeTypeConfig)).toThrow(TypeError)
  31. }
  32. )
  33. )
  34. })
  35. it('should throw an error given empty arrays', () => {
  36. expect(() => isValidMimeType({ validMimeTypes: [] })).toThrow(RangeError)
  37. })
  38. it('should throw an error given non-string arrays', () => {
  39. fc.assert(
  40. fc.property(
  41. fc.record({
  42. validMimeTypes: fc.array(
  43. fc.anything().filter(e => typeof e !== 'string'),
  44. 1,
  45. 20
  46. ),
  47. }),
  48. params => {
  49. expect(() => isValidMimeType((params as unknown) as IsValidMimeTypeConfig)).toThrow(TypeError)
  50. }
  51. )
  52. )
  53. })
  54. it('should return a main callable given strings', () => {
  55. fc.assert(
  56. fc.property(
  57. fc.record<IsValidMimeTypeConfig>({
  58. validMimeTypes: fc.string(),
  59. }),
  60. params => {
  61. expect(typeof isValidMimeType(params)).toBe('function')
  62. }
  63. )
  64. )
  65. })
  66. it('should return a main callable given string arrays', () => {
  67. fc.assert(
  68. fc.property(
  69. fc.record<IsValidMimeTypeConfig>({
  70. validMimeTypes: fc.array(fc.string(), 1, 20),
  71. }),
  72. params => {
  73. expect(typeof isValidMimeType(params)).toBe('function')
  74. }
  75. )
  76. )
  77. })
  78. })
  79. describe('on main callable', () => {
  80. it('should throw an error for non-string params', () => {
  81. fc.assert(
  82. fc.property(
  83. fc.tuple(
  84. fc.record<IsValidMimeTypeConfig>({
  85. validMimeTypes: fc.oneof(fc.string(), fc.array(fc.string(), 1, 20)),
  86. }),
  87. fc.anything().filter(v => typeof v !== 'string')
  88. ),
  89. ([params, maybeFileName]) => {
  90. expect(() => isValidMimeType(params)(maybeFileName as string)).toThrow(TypeError)
  91. }
  92. )
  93. )
  94. })
  95. it('should return a boolean for string params', () => {
  96. fc.assert(
  97. fc.property(
  98. fc.tuple(
  99. fc.record<IsValidMimeTypeConfig>({
  100. validMimeTypes: fc.oneof(fc.string(), fc.array(fc.string(), 1, 20)),
  101. }),
  102. fc.string()
  103. ),
  104. ([params, maybeFileName]) => {
  105. expect(typeof isValidMimeType(params)(maybeFileName)).toBe('boolean')
  106. }
  107. )
  108. )
  109. })
  110. it('should return true when catch-all MIME type is specified', () => {
  111. fc.assert(
  112. fc.property(fc.tuple(fc.array(fc.string(), 1, 20), fc.string()), ([validMimeTypes, maybeFileName]) => {
  113. expect(isValidMimeType({ validMimeTypes: [...validMimeTypes, '*/*'] })(maybeFileName)).toBe(true)
  114. })
  115. )
  116. })
  117. it('should return true when catch-all for specified MIME type class is specified', () => {
  118. fc.assert(
  119. fc.property(
  120. fc.tuple(
  121. fc.string().filter(s => !s.includes('/')),
  122. fc.string().filter(s => !s.includes('/'))
  123. ),
  124. ([mimeTypeClass, test]) => {
  125. expect(isValidMimeType({ validMimeTypes: [`${mimeTypeClass}/*`] })(`${mimeTypeClass}/${test}`)).toBe(true)
  126. }
  127. )
  128. )
  129. })
  130. })