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.

118 lines
3.2 KiB

  1. import * as fc from 'fast-check'
  2. import isValidFilename, { IsValidFilenameConfig } from './isValidFileName'
  3. it('should exist', () => {
  4. expect(isValidFilename).toBeDefined()
  5. })
  6. it('should be a callable', () => {
  7. expect(typeof isValidFilename).toBe('function')
  8. })
  9. it('should accept a minimum of 1 argument', () => {
  10. expect(isValidFilename).toHaveLength(1)
  11. })
  12. it('should throw an error given invalid param shapes', () => {
  13. fc.assert(
  14. fc.property(
  15. fc.object().filter(o => !('validExtensions' in o)),
  16. params => {
  17. expect(() => isValidFilename((params as unknown) as IsValidFilenameConfig)).toThrow(TypeError)
  18. }
  19. )
  20. )
  21. })
  22. describe('on valid extensions', () => {
  23. it('should throw an error given non-string and non-array values', () => {
  24. fc.assert(
  25. fc.property(
  26. fc.record({
  27. validExtensions: fc.anything().filter(e => !(typeof e === 'string' || Array.isArray(e))),
  28. }),
  29. params => {
  30. expect(() => isValidFilename((params as unknown) as IsValidFilenameConfig)).toThrow(TypeError)
  31. }
  32. )
  33. )
  34. })
  35. it('should throw an error given empty arrays', () => {
  36. expect(() => isValidFilename({ validExtensions: [] })).toThrow(RangeError)
  37. })
  38. it('should throw an error given non-string arrays', () => {
  39. fc.assert(
  40. fc.property(
  41. fc.record({
  42. validExtensions: fc.array(
  43. fc.anything().filter(e => typeof e !== 'string'),
  44. 1,
  45. 20
  46. ),
  47. }),
  48. params => {
  49. expect(() => isValidFilename((params as unknown) as IsValidFilenameConfig)).toThrow(TypeError)
  50. }
  51. )
  52. )
  53. })
  54. it('should return a main callable given strings', () => {
  55. fc.assert(
  56. fc.property(
  57. fc.record<IsValidFilenameConfig>({
  58. validExtensions: fc.string(),
  59. }),
  60. params => {
  61. expect(typeof isValidFilename(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<IsValidFilenameConfig>({
  70. validExtensions: fc.array(fc.string(), 1, 20),
  71. }),
  72. params => {
  73. expect(typeof isValidFilename(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<IsValidFilenameConfig>({
  85. validExtensions: 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(() => isValidFilename(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<IsValidFilenameConfig>({
  100. validExtensions: fc.oneof(fc.string(), fc.array(fc.string(), 1, 20)),
  101. }),
  102. fc.string()
  103. ),
  104. ([params, maybeFileName]) => {
  105. expect(typeof isValidFilename(params)(maybeFileName)).toBe('boolean')
  106. }
  107. )
  108. )
  109. })
  110. })