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.

120 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(fc.anything().filter(e => typeof e !== 'string'), 1, 20),
  43. }),
  44. params => {
  45. expect(() => isValidFilename(params as unknown as IsValidFilenameConfig)).toThrow(TypeError)
  46. }
  47. )
  48. )
  49. })
  50. it('should return a main callable given strings', () => {
  51. fc.assert(
  52. fc.property(
  53. fc.record<IsValidFilenameConfig>({
  54. validExtensions: fc.string(),
  55. }),
  56. params => {
  57. expect(typeof isValidFilename(params)).toBe('function')
  58. }
  59. )
  60. )
  61. })
  62. it('should return a main callable given string arrays', () => {
  63. fc.assert(
  64. fc.property(
  65. fc.record<IsValidFilenameConfig>({
  66. validExtensions: fc.array(fc.string(), 1, 20),
  67. }),
  68. params => {
  69. expect(typeof isValidFilename(params)).toBe('function')
  70. }
  71. )
  72. )
  73. })
  74. })
  75. describe('on main callable', () => {
  76. it('should throw an error for non-string params', () => {
  77. fc.assert(
  78. fc.property(
  79. fc.tuple(
  80. fc.record<IsValidFilenameConfig>({
  81. validExtensions: fc.oneof(
  82. fc.string(),
  83. fc.array(fc.string(), 1, 20),
  84. ),
  85. }),
  86. fc.anything().filter(v => typeof v !== 'string'),
  87. ),
  88. ([params, maybeFileName]) => {
  89. expect(() => isValidFilename(params)(maybeFileName as string)).toThrow(TypeError)
  90. }
  91. )
  92. )
  93. })
  94. it('should return a boolean for string params', () => {
  95. fc.assert(
  96. fc.property(
  97. fc.tuple(
  98. fc.record<IsValidFilenameConfig>({
  99. validExtensions: fc.oneof(
  100. fc.string(),
  101. fc.array(fc.string(), 1, 20),
  102. ),
  103. }),
  104. fc.string(),
  105. ),
  106. ([params, maybeFileName]) => {
  107. expect(typeof isValidFilename(params)(maybeFileName)).toBe('boolean')
  108. }
  109. )
  110. )
  111. })
  112. })