index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* eslint no-unused-expressions: 0 */
  2. import 'core-js'
  3. import expect from 'expect'
  4. import accept from '../src/index'
  5. describe('accept', () => {
  6. it('should return true if called without acceptedFiles', () => {
  7. expect(
  8. accept(
  9. {
  10. name: 'testfile.png',
  11. type: 'some/type'
  12. },
  13. undefined
  14. )
  15. ).toBe(true)
  16. })
  17. it('should not throw and return true if file is empty or null', () => {
  18. expect(() => {
  19. accept({})
  20. accept({}, 'text/html')
  21. accept({}, '*.png')
  22. accept({}, 'image/*')
  23. accept(null)
  24. accept(null, 'text/html')
  25. accept(null, '*.png')
  26. accept(null, 'image/*')
  27. }).toNotThrow()
  28. })
  29. it('should properly validate if called with concrete mime types', () => {
  30. const acceptedMimeTypes = 'text/html,image/jpeg,application/json'
  31. expect(
  32. accept(
  33. {
  34. name: 'testfile.png',
  35. type: 'text/html'
  36. },
  37. acceptedMimeTypes
  38. )
  39. ).toBe(true)
  40. expect(
  41. accept(
  42. {
  43. name: 'testfile.png',
  44. type: 'image/jpeg'
  45. },
  46. acceptedMimeTypes
  47. )
  48. ).toBe(true)
  49. expect(
  50. accept(
  51. {
  52. name: 'testfile.png',
  53. type: 'application/json'
  54. },
  55. acceptedMimeTypes
  56. )
  57. ).toBe(true)
  58. expect(
  59. accept(
  60. {
  61. name: 'testfile.png',
  62. type: 'image/bmp'
  63. },
  64. acceptedMimeTypes
  65. )
  66. ).toBe(false)
  67. expect(
  68. accept(
  69. {
  70. type: 'image/bmp'
  71. },
  72. acceptedMimeTypes
  73. )
  74. ).toBe(false)
  75. })
  76. it('should properly validate if called with base mime types', () => {
  77. const acceptedMimeTypes = 'text/*,image/*,application/*'
  78. expect(
  79. accept(
  80. {
  81. name: 'testfile.png',
  82. type: 'text/html'
  83. },
  84. acceptedMimeTypes
  85. )
  86. ).toBe(true)
  87. expect(
  88. accept(
  89. {
  90. name: 'testfile.png',
  91. type: 'image/jpeg'
  92. },
  93. acceptedMimeTypes
  94. )
  95. ).toBe(true)
  96. expect(
  97. accept(
  98. {
  99. name: 'testfile.png',
  100. type: 'application/json'
  101. },
  102. acceptedMimeTypes
  103. )
  104. ).toBe(true)
  105. expect(
  106. accept(
  107. {
  108. name: 'testfile.png',
  109. type: 'image/bmp'
  110. },
  111. acceptedMimeTypes
  112. )
  113. ).toBe(true)
  114. expect(
  115. accept(
  116. {
  117. name: 'testfile.png',
  118. type: 'some/type'
  119. },
  120. acceptedMimeTypes
  121. )
  122. ).toBe(false)
  123. })
  124. it('should properly validate if called with mixed mime types', () => {
  125. const acceptedMimeTypes = 'text/*,image/jpeg,application/*'
  126. expect(
  127. accept(
  128. {
  129. name: 'testfile.png',
  130. type: 'text/html'
  131. },
  132. acceptedMimeTypes
  133. )
  134. ).toBe(true)
  135. expect(
  136. accept(
  137. {
  138. name: 'testfile.png',
  139. type: 'image/jpeg'
  140. },
  141. acceptedMimeTypes
  142. )
  143. ).toBe(true)
  144. expect(
  145. accept(
  146. {
  147. name: 'testfile.png',
  148. type: 'image/bmp'
  149. },
  150. acceptedMimeTypes
  151. )
  152. ).toBe(false)
  153. expect(
  154. accept(
  155. {
  156. name: 'testfile.png',
  157. type: 'application/json'
  158. },
  159. acceptedMimeTypes
  160. )
  161. ).toBe(true)
  162. expect(
  163. accept(
  164. {
  165. name: 'testfile.png',
  166. type: 'some/type'
  167. },
  168. acceptedMimeTypes
  169. )
  170. ).toBe(false)
  171. })
  172. it('should properly validate even with spaces in between', () => {
  173. const acceptedMimeTypes = 'text/html , image/jpeg, application/json'
  174. expect(
  175. accept(
  176. {
  177. name: 'testfile.png',
  178. type: 'text/html'
  179. },
  180. acceptedMimeTypes
  181. )
  182. ).toBe(true)
  183. expect(
  184. accept(
  185. {
  186. name: 'testfile.png',
  187. type: 'image/jpeg'
  188. },
  189. acceptedMimeTypes
  190. )
  191. ).toBe(true)
  192. })
  193. it('should properly validate extensions', () => {
  194. const acceptedMimeTypes = 'text/html , image/jpeg, .pdf ,.png'
  195. expect(
  196. accept(
  197. {
  198. name: 'somxsfsd',
  199. type: 'text/html'
  200. },
  201. acceptedMimeTypes
  202. )
  203. ).toBe(true)
  204. expect(
  205. accept(
  206. {
  207. name: 'somesdfsdf',
  208. type: 'image/jpeg'
  209. },
  210. acceptedMimeTypes
  211. )
  212. ).toBe(true)
  213. expect(
  214. accept(
  215. {
  216. name: 'somesdfadfadf',
  217. type: 'application/json'
  218. },
  219. acceptedMimeTypes
  220. )
  221. ).toBe(false)
  222. expect(
  223. accept(
  224. {
  225. name: 'some-file file.pdf',
  226. type: 'random/type'
  227. },
  228. acceptedMimeTypes
  229. )
  230. ).toBe(true)
  231. expect(
  232. accept(
  233. {
  234. name: 'some-file.pdf file.gif',
  235. type: 'random/type'
  236. },
  237. acceptedMimeTypes
  238. )
  239. ).toBe(false)
  240. expect(
  241. accept(
  242. {
  243. name: 'some-FILEi File.PNG',
  244. type: 'random/type'
  245. },
  246. acceptedMimeTypes
  247. )
  248. ).toBe(true)
  249. })
  250. it('should allow accepted files passed to be an array', () => {
  251. const acceptedMimeTypes = ['img/jpeg', '.pdf']
  252. expect(
  253. accept(
  254. {
  255. name: 'testfile.pdf',
  256. type: 'random/type'
  257. },
  258. acceptedMimeTypes
  259. )
  260. ).toBe(true)
  261. expect(
  262. accept(
  263. {
  264. name: 'testfile.jpg',
  265. type: 'img/jpeg'
  266. },
  267. acceptedMimeTypes
  268. )
  269. ).toBe(true)
  270. expect(
  271. accept(
  272. {
  273. name: 'testfile',
  274. type: 'application/json'
  275. },
  276. acceptedMimeTypes
  277. )
  278. ).toBe(false)
  279. })
  280. it('should check MIME types in a case insensitive way', () => {
  281. expect(
  282. accept(
  283. {
  284. name: 'testfile.xlsm',
  285. type: 'application/vnd.ms-excel.sheet.macroenabled.12'
  286. },
  287. ['application/vnd.ms-excel.sheet.macroEnabled.12']
  288. )
  289. ).toBe(true)
  290. expect(
  291. accept(
  292. {
  293. name: 'testfile.xlsm',
  294. type: 'application/vnd.ms-excel.sheet.macroEnabled.12'
  295. },
  296. ['application/vnd.ms-excel.sheet.macroenabled.12']
  297. )
  298. ).toBe(true)
  299. })
  300. })