123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- /* eslint no-unused-expressions: 0 */
- import 'core-js'
- import expect from 'expect'
- import accept from '../src/index'
- describe('accept', () => {
- it('should return true if called without acceptedFiles', () => {
- expect(
- accept(
- {
- name: 'testfile.png',
- type: 'some/type'
- },
- undefined
- )
- ).toBe(true)
- })
- it('should not throw and return true if file is empty or null', () => {
- expect(() => {
- accept({})
- accept({}, 'text/html')
- accept({}, '*.png')
- accept({}, 'image/*')
- accept(null)
- accept(null, 'text/html')
- accept(null, '*.png')
- accept(null, 'image/*')
- }).toNotThrow()
- })
- it('should properly validate if called with concrete mime types', () => {
- const acceptedMimeTypes = 'text/html,image/jpeg,application/json'
- expect(
- accept(
- {
- name: 'testfile.png',
- type: 'text/html'
- },
- acceptedMimeTypes
- )
- ).toBe(true)
- expect(
- accept(
- {
- name: 'testfile.png',
- type: 'image/jpeg'
- },
- acceptedMimeTypes
- )
- ).toBe(true)
- expect(
- accept(
- {
- name: 'testfile.png',
- type: 'application/json'
- },
- acceptedMimeTypes
- )
- ).toBe(true)
- expect(
- accept(
- {
- name: 'testfile.png',
- type: 'image/bmp'
- },
- acceptedMimeTypes
- )
- ).toBe(false)
- expect(
- accept(
- {
- type: 'image/bmp'
- },
- acceptedMimeTypes
- )
- ).toBe(false)
- })
- it('should properly validate if called with base mime types', () => {
- const acceptedMimeTypes = 'text/*,image/*,application/*'
- expect(
- accept(
- {
- name: 'testfile.png',
- type: 'text/html'
- },
- acceptedMimeTypes
- )
- ).toBe(true)
- expect(
- accept(
- {
- name: 'testfile.png',
- type: 'image/jpeg'
- },
- acceptedMimeTypes
- )
- ).toBe(true)
- expect(
- accept(
- {
- name: 'testfile.png',
- type: 'application/json'
- },
- acceptedMimeTypes
- )
- ).toBe(true)
- expect(
- accept(
- {
- name: 'testfile.png',
- type: 'image/bmp'
- },
- acceptedMimeTypes
- )
- ).toBe(true)
- expect(
- accept(
- {
- name: 'testfile.png',
- type: 'some/type'
- },
- acceptedMimeTypes
- )
- ).toBe(false)
- })
- it('should properly validate if called with mixed mime types', () => {
- const acceptedMimeTypes = 'text/*,image/jpeg,application/*'
- expect(
- accept(
- {
- name: 'testfile.png',
- type: 'text/html'
- },
- acceptedMimeTypes
- )
- ).toBe(true)
- expect(
- accept(
- {
- name: 'testfile.png',
- type: 'image/jpeg'
- },
- acceptedMimeTypes
- )
- ).toBe(true)
- expect(
- accept(
- {
- name: 'testfile.png',
- type: 'image/bmp'
- },
- acceptedMimeTypes
- )
- ).toBe(false)
- expect(
- accept(
- {
- name: 'testfile.png',
- type: 'application/json'
- },
- acceptedMimeTypes
- )
- ).toBe(true)
- expect(
- accept(
- {
- name: 'testfile.png',
- type: 'some/type'
- },
- acceptedMimeTypes
- )
- ).toBe(false)
- })
- it('should properly validate even with spaces in between', () => {
- const acceptedMimeTypes = 'text/html , image/jpeg, application/json'
- expect(
- accept(
- {
- name: 'testfile.png',
- type: 'text/html'
- },
- acceptedMimeTypes
- )
- ).toBe(true)
- expect(
- accept(
- {
- name: 'testfile.png',
- type: 'image/jpeg'
- },
- acceptedMimeTypes
- )
- ).toBe(true)
- })
- it('should properly validate extensions', () => {
- const acceptedMimeTypes = 'text/html , image/jpeg, .pdf ,.png'
- expect(
- accept(
- {
- name: 'somxsfsd',
- type: 'text/html'
- },
- acceptedMimeTypes
- )
- ).toBe(true)
- expect(
- accept(
- {
- name: 'somesdfsdf',
- type: 'image/jpeg'
- },
- acceptedMimeTypes
- )
- ).toBe(true)
- expect(
- accept(
- {
- name: 'somesdfadfadf',
- type: 'application/json'
- },
- acceptedMimeTypes
- )
- ).toBe(false)
- expect(
- accept(
- {
- name: 'some-file file.pdf',
- type: 'random/type'
- },
- acceptedMimeTypes
- )
- ).toBe(true)
- expect(
- accept(
- {
- name: 'some-file.pdf file.gif',
- type: 'random/type'
- },
- acceptedMimeTypes
- )
- ).toBe(false)
- expect(
- accept(
- {
- name: 'some-FILEi File.PNG',
- type: 'random/type'
- },
- acceptedMimeTypes
- )
- ).toBe(true)
- })
- it('should allow accepted files passed to be an array', () => {
- const acceptedMimeTypes = ['img/jpeg', '.pdf']
- expect(
- accept(
- {
- name: 'testfile.pdf',
- type: 'random/type'
- },
- acceptedMimeTypes
- )
- ).toBe(true)
- expect(
- accept(
- {
- name: 'testfile.jpg',
- type: 'img/jpeg'
- },
- acceptedMimeTypes
- )
- ).toBe(true)
- expect(
- accept(
- {
- name: 'testfile',
- type: 'application/json'
- },
- acceptedMimeTypes
- )
- ).toBe(false)
- })
- it('should check MIME types in a case insensitive way', () => {
- expect(
- accept(
- {
- name: 'testfile.xlsm',
- type: 'application/vnd.ms-excel.sheet.macroenabled.12'
- },
- ['application/vnd.ms-excel.sheet.macroEnabled.12']
- )
- ).toBe(true)
- expect(
- accept(
- {
- name: 'testfile.xlsm',
- type: 'application/vnd.ms-excel.sheet.macroEnabled.12'
- },
- ['application/vnd.ms-excel.sheet.macroenabled.12']
- )
- ).toBe(true)
- })
- })
|