Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unit tests for packages/web-app-files/src/mixins.js:fileTypeIcon #5253

Merged
merged 2 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/unreleased/bugfix-typeerror-fileTypeIcon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Bugfix: prevent `fileTypeIcon` to throw a TypeError

The function would die with `TypeError: file.extension.toLowerCase is not a function` if `file.extension` was set to something that is not a string.

https://github.com/owncloud/web/pull/5253
8 changes: 6 additions & 2 deletions packages/web-app-files/src/mixins.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,12 @@ export default {
if (file.type === 'folder') {
return 'folder'
}
const icon = fileTypeIconMappings[file.extension.toLowerCase()]
if (icon) return `${icon}`
if (typeof file.extension === 'string') {
const icon = fileTypeIconMappings[file.extension.toLowerCase()]
if (icon) {
return `${icon}`
}
}
}
return 'file'
},
Expand Down
52 changes: 52 additions & 0 deletions packages/web-app-files/tests/mixins.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import mixins from '../src/mixins'

describe('mixins', () => {
describe('fileTypeIcon', () => {
it.each(['', false, NaN, undefined, 1, {}])(
'should return "file" if invalid data is provided',
inputData => {
expect(mixins.methods.fileTypeIcon(inputData)).toEqual('file')
}
)
it.each([
{ type: 'file', extension: 'not existing' },
{ type: 'file', extension: 'UTF नेपालि' },
{ type: 'file', extension: '' },
{ type: 'file', extension: false },
{ type: 'file', extension: undefined },
{ type: 'file', extension: 1 },
{ type: 'file', extension: 0 },
{ type: 'file', extension: '.tar.bz2' },
{ type: '', extension: '.tar.bz2' },
{ type: 0, extension: '.tar.bz2' },
{ type: false, extension: '.tar.bz2' },
{ type: undefined, extension: '.tar.bz2' }
])('should return "file" for an unknown extension', inputData => {
expect(mixins.methods.fileTypeIcon(inputData)).toEqual('file')
})
it.each([
{ type: 'folder', extension: '' },
{ type: 'folder', extension: 'pdf' },
{ type: 'folder' },
{ type: 'folder', extension: false },
{ type: 'folder', extension: undefined },
{ type: 'folder', extension: 1 },
{ type: 'folder', extension: 0 }
])('should return "folder" if type is set to "folder"', inputData => {
expect(mixins.methods.fileTypeIcon(inputData)).toEqual('folder')
})
it.each([
{ type: 'file', extension: 'tar.bz2' },
{ type: 'file', extension: 'tAr.Bz2' },
{ type: false, extension: 'tar.bz2' },
{ type: undefined, extension: 'tar.bz2' },
{ type: 1, extension: 'tar.bz2' },
{ type: '', extension: 'tar.bz2' },
{ type: 0, extension: 'tar.bz2' },
{ type: false, extension: 'tar.bz2' },
{ type: '0', extension: 'tar.bz2' }
])('should return the icon for a known file extension, regardless of the type', inputData => {
expect(mixins.methods.fileTypeIcon(inputData)).toEqual('package-x-generic')
})
})
})