Skip to content
Permalink
e504f7a13e
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
173 lines (152 sloc) 4.91 KB
'use strict'
const Image = require('../modules/image')
describe('add()', () => {
test('return error if filename is empty', async done => {
expect.assertions(1)
const image = await new Image()
await expect(image.add(''))
.rejects.toEqual(Error('filename is missing'))
done()
})
test('return error if filename exists in database', async done => {
expect.assertions(1)
const image = await new Image()
await image.add('public/avatars/adrian.png')
await expect(image.add('public/avatars/adrian.png'))
.rejects.toEqual(Error('filename is already in use'))
done()
})
test('add a single item', async done => {
expect.assertions(1)
const image = await new Image()
await image.add('public/avatars/adrian.png')
const count = await image.count()
expect(count).toEqual(2)
done()
})
test('add both filename and description to the database', async done => {
expect.assertions(2)
const image = await new Image()
const described = await image.add('public/avatars/adrian.png', 'Images of Adrian')
const count = await image.count()
expect(count).toEqual(2)
expect(described).toEqual(2)
done()
})
})
describe('get()', () => {
test('return error if parameters are not stated', async done => {
expect.assertions(1)
const image = await new Image()
await expect(image.get())
.rejects.toEqual(Error('key is not defined'))
done()
})
test('return error if file does not exist', async done => {
expect.assertions(1)
const image = await new Image()
await expect(image.get(2))
.rejects.toEqual(Error('file does not exist'))
done()
})
test('return the filename and description based on input', async done => {
expect.assertions(2)
const image = await new Image()
await image.add('public/avatars/adrian.png', 'Image of Adrian')
const result = await image.get(2)
expect(result.image_path).toEqual('public/avatars/adrian.png')
expect(result.image_description).toEqual('Image of Adrian')
done()
})
})
describe('getAll()', () => {
test('return one item when no new images are added', async done => {
expect.assertions(1)
const image = await new Image()
const results = await image.getAll()
expect(results[0].path).toEqual('public/avatars/avatar.png')
done()
})
test('return all image filenames and descriptions', async done => {
expect.assertions(2)
const image = await new Image()
await image.add('public/avatars/adrian.png', 'Image of Adrian')
const results = await image.getAll()
expect(results[1].path).toEqual('public/avatars/adrian.png')
expect(results[1].description).toEqual('Image of Adrian')
done()
})
})
describe('rename()', () => {
test('return error if filename is empty', async done => {
expect.assertions(1)
const image = await new Image()
await expect(image.rename('', 'public/avatars/adrian.png'))
.rejects.toEqual(Error('file not selected'))
done()
})
test('return error if new name is empty', async done => {
expect.assertions(1)
const image = await new Image()
await expect(image.rename('public/avatars/adrian.png', ''))
.rejects.toEqual(Error('new name not stated'))
done()
})
test('return true when image_path is renamed', async done => {
expect.assertions(1)
const image = await new Image()
await image.add('public/avatars/adrian.png')
const renamed = await image.rename('public/avatars/adrian.png', 'public/avatars/adrian1.png')
expect(renamed).toEqual(true)
done()
})
test('return error when image_path is not found', async done => {
expect.assertions(1)
const image = await new Image()
await expect( image.rename('public/avatars/adrian.png', 'public/avatars/adrian1.png'))
.rejects.toEqual(Error('file is not found'))
done()
})
})
describe('delete()', () => {
test('return error when file id is not defined', async done => {
expect.assertions(1)
const image = await new Image()
await expect(image.delete())
.rejects.toEqual(Error('key is not defined'))
done()
})
test('return error when file id does not exist', async done => {
expect.assertions(1)
const image = await new Image()
await expect(image.delete(2))
.rejects.toEqual(Error('file is not found'))
done()
})
test('delete a single item from database', async done => {
expect.assertions(1)
const image = await new Image()
await image.add('public/avatars/adrian.png')
const deleted = await image.delete(2)
expect(deleted).toEqual(true)
done()
})
})
describe('count()', () => {
test('return the amount of images in database', async done => {
expect.assertions(1)
const image = await new Image()
await image.add('public/avatars/adrian.png')
await image.add('public/avatars/adrian1.png')
const amount = await image.count()
expect(amount).toEqual(3) //expect 3 due to default avatar
done()
})
test('return one if no new images are stored', async done => {
expect.assertions(1)
const image = await new Image()
const amount = await image.count()
expect(amount).toEqual(1) //expect 1 due to default avatar
done()
})
})