Skip to content

Commit

Permalink
#89: Thumbnail tests
Browse files Browse the repository at this point in the history
  • Loading branch information
groenroos committed Mar 20, 2022
1 parent c8d251e commit 1195a8e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/Uploads.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ export default class Uploads {
}

/* Generate thumbnails, if any */
const thumbs = new Utils().coerceArray(rule && rule.thumbnails ? rule.thumbnails : this.app.config.upload.thumbnails);
const thumbDefinition = rule && rule.thumbnails ? rule.thumbnails : this.app.config.upload.thumbnails;
const thumbs = thumbDefinition ? new Utils().coerceArray(thumbDefinition) : [];

for (const [i, thumb] of thumbs.entries()) {
/* Construct path for thumbnail */
Expand Down
Binary file added test/_data/files/photo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions test/lib/Uploads.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';
import fs from 'fs';
import _ from 'underscore';
import { fileURLToPath } from 'url';
import imageSize from 'image-size';

import Response from '../../lib/Response.js';
import SaplingError from '../../lib/SaplingError.js';
Expand Down Expand Up @@ -61,6 +62,46 @@ test('uploads an image', async t => {
t.is(upload.image.height, 180);
});

test('processes thumbnails', async t => {
t.context.app.uploads = new Uploads(t.context.app);
const request = _.extend({
files: {
image: getFileObject('photo.jpg')
}
}, t.context.request);

const upload = await t.context.app.uploads.handleUpload(request, t.context.response, {
image: {
thumbnails: [
{
name: 'web',
width: 500,
},
{
name: 'thumb',
width: 128,
height: 128,
fit: 'cover',
},
]
}
});

const thumbWebPath = path.join(__dirname, 'uploads/thumbs/web/photo.jpg');
const thumbWebDims = await imageSize(thumbWebPath);

t.true(fs.existsSync(thumbWebPath));
t.is(thumbWebDims.width, 500);
t.is(thumbWebDims.height, 375);

const thumbThumbPath = path.join(__dirname, 'uploads/thumbs/thumb/photo.jpg');
const thumbThumbDims = await imageSize(thumbThumbPath);

t.true(fs.existsSync(thumbThumbPath));
t.is(thumbThumbDims.width, 128);
t.is(thumbThumbDims.height, 128);
});

test('uploads a video', async t => {
t.context.app.uploads = new Uploads(t.context.app);
const request = _.extend({
Expand Down

0 comments on commit 1195a8e

Please sign in to comment.