|
| 1 | +// Composables |
| 2 | +import { useFileFilter } from '../fileFilter' |
| 3 | + |
| 4 | +describe('fileFilter', () => { |
| 5 | + it('by extension', () => { |
| 6 | + const { filterAccepted } = useFileFilter({ filterByType: '.pdf' }) |
| 7 | + const { accepted, rejected } = filterAccepted([ |
| 8 | + new File([], 'file_1.pdf', { type: 'application/pdf' }), |
| 9 | + new File([], 'file_2.txt', { type: 'text/plain' }), |
| 10 | + new File([], 'file_3.pdf', { type: 'application/pdf' }), |
| 11 | + new File([], 'file_4.png', { type: 'image/png' }), |
| 12 | + ]) |
| 13 | + expect(accepted.map(x => x.name)).toEqual(['file_1.pdf', 'file_3.pdf']) |
| 14 | + expect(rejected.map(x => x.name)).toEqual(['file_2.txt', 'file_4.png']) |
| 15 | + }) |
| 16 | + |
| 17 | + it('by type', () => { |
| 18 | + const { filterAccepted } = useFileFilter({ filterByType: 'image/png, image/jpeg' }) |
| 19 | + const { accepted, rejected } = filterAccepted([ |
| 20 | + new File([], 'file_1.pdf', { type: 'application/pdf' }), |
| 21 | + new File([], 'file_2.png', { type: 'image/png' }), |
| 22 | + new File([], 'file_3.jpeg', { type: 'image/jpeg' }), |
| 23 | + new File([], 'file_4.jpg', { type: 'image/jpeg' }), |
| 24 | + new File([], 'file_5.htm', { type: 'text/html' }), |
| 25 | + ]) |
| 26 | + expect(accepted.map(x => x.name)).toEqual(['file_2.png', 'file_3.jpeg', 'file_4.jpg']) |
| 27 | + expect(rejected.map(x => x.name)).toEqual(['file_1.pdf', 'file_5.htm']) |
| 28 | + }) |
| 29 | + |
| 30 | + it('by type with wildcard', () => { |
| 31 | + const { filterAccepted } = useFileFilter({ filterByType: 'video/*' }) |
| 32 | + const { accepted, rejected } = filterAccepted([ |
| 33 | + new File([], 'file_1.mp4', { type: 'video/mp4' }), |
| 34 | + new File([], 'file_2.mpeg', { type: 'video/mpeg' }), |
| 35 | + new File([], 'file_3.gif', { type: 'image/gif' }), |
| 36 | + new File([], 'file_4.wav', { type: 'audio/wav' }), |
| 37 | + ]) |
| 38 | + expect(accepted.map(x => x.name)).toEqual(['file_1.mp4', 'file_2.mpeg']) |
| 39 | + expect(rejected.map(x => x.name)).toEqual(['file_3.gif', 'file_4.wav']) |
| 40 | + }) |
| 41 | + |
| 42 | + it('by mixed rules', () => { |
| 43 | + const { filterAccepted } = useFileFilter({ filterByType: 'font/*,application/manifest+json,.zip' }) |
| 44 | + const { accepted, rejected } = filterAccepted([ |
| 45 | + new File([], 'file_1.css', { type: 'text/css' }), |
| 46 | + new File([], 'file_2.jpg', { type: 'image/jpeg' }), |
| 47 | + new File([], 'file_3.rar', { type: 'application/vnd.rar' }), |
| 48 | + new File([], 'file_4.zip', { type: 'application/x-zip-compressed' }), |
| 49 | + new File([], 'file_5.js', { type: 'text/javascript' }), |
| 50 | + new File([], 'file_6.woff2', { type: 'font/woff2' }), |
| 51 | + new File([], 'file_7.woff', { type: 'font/woff' }), |
| 52 | + new File([], 'file_8.ico', { type: 'image/vnd.microsoft.icon' }), |
| 53 | + new File([], 'file_9.webmanifest', { type: 'application/manifest+json' }), |
| 54 | + ]) |
| 55 | + expect(accepted.map(x => x.name)).toEqual(['file_4.zip', 'file_6.woff2', 'file_7.woff', 'file_9.webmanifest']) |
| 56 | + expect(rejected.map(x => x.name)).toEqual(['file_1.css', 'file_2.jpg', 'file_3.rar', 'file_5.js', 'file_8.ico']) |
| 57 | + }) |
| 58 | +}) |
0 commit comments