@bigfishtv/cockpit
Version:
112 lines (95 loc) • 3.68 kB
JavaScript
import { getExtension, getImageUrl, getAssetUrl } from './fileUtils.js'
describe('returns extension from filename string', () => {
it('should return valid extension if filename is string and contains a `.`', () => {
const input = 'my.amazing.image.jpg'
const expected = 'jpg'
expect(getExtension(input)).toEqual(expected)
})
it('should return false if no `.` is present in filename string', () => {
const input = 'imagejpg'
const expected = false
expect(getExtension(input)).toEqual(expected)
})
it('should return false if not provided a string, or if provided nothing', () => {
expect(getExtension()).toEqual(false)
expect(getExtension(null)).toEqual(false)
expect(getExtension(true)).toEqual(false)
expect(getExtension(1234)).toEqual(false)
expect(getExtension({ foo: 'bar' })).toEqual(false)
expect(getExtension([1, 2, 3, 4])).toEqual(false)
expect(getExtension(() => {})).toEqual(false)
})
})
describe('generate resized image url from file object', () => {
it('should provide valid url when only given an object with a filename', () => {
const input = { filename: 'image.png' }
const expected = '/generated/cockpit-small/image.png'
expect(getImageUrl(input)).toEqual(expected)
})
it('should provide valid url when given an object with a filename, and size', () => {
const input1 = { filename: 'image.png' }
const input2 = 'cockpit-small'
const expected = '/generated/' + input2 + '/image.png'
expect(getImageUrl(input1, input2)).toEqual(expected)
})
it('should provide valid url when given an object with a filename, size and extension', () => {
const input1 = { filename: 'image.jpg' }
const input2 = 'cockpit-small'
const input3 = 'jpg'
const expected = '/generated/' + input2 + '/image.' + input3
expect(getImageUrl(input1, input2, input3)).toEqual(expected)
})
it('should return false if given a string', () => {
const input = 'string'
const expected = false
expect(getImageUrl(input)).toEqual(expected)
})
it('should return false if given a number', () => {
const input = 1234
const expected = false
expect(getImageUrl(input)).toEqual(expected)
})
it('should return false if given a boolean', () => {
const input = true
const expected = false
expect(getImageUrl(input)).toEqual(expected)
})
it('should return false if given an array', () => {
const input = [{ filename: 'file1.jpg' }, { filename: 'file2.jpg' }]
const expected = false
expect(getImageUrl(input)).toEqual(expected)
})
it('should return false if image size is not a string', () => {
const input1 = { filename: 'file1.jpg' }
const input2 = 300
const expected = false
expect(getImageUrl(input1, input2)).toEqual(expected)
})
})
describe('generate asset url from file object', () => {
it('should provide valid url if given an object with a filename attribute', () => {
const input = { filename: 'image.png' }
const expected = '/uploads/' + input.filename
expect(getAssetUrl(input)).toEqual(expected)
})
it('should return valid url if given a filename', () => {
const input = 'file.exe'
const expected = '/uploads/' + input
expect(getAssetUrl(input)).toEqual(expected)
})
it('should return false if given a number', () => {
const input = 1234
const expected = false
expect(getAssetUrl(input)).toEqual(expected)
})
it('should return false if given a boolean', () => {
const input = true
const expected = false
expect(getAssetUrl(input)).toEqual(expected)
})
it('should return false if given an array', () => {
const input = [{ filename: 'file1.jpg' }, { filename: 'file2.jpg' }]
const expected = false
expect(getAssetUrl(input)).toEqual(expected)
})
})