1 | const before = require('mocha').before
|
2 | const {checkTransformation, transformWithCli} = require('./helpers.js')
|
3 | const describe = require('mocha').describe
|
4 | const expect = require('chai').expect
|
5 | const fs = require('fs-extra')
|
6 | const it = require('mocha').it
|
7 | const jo = require('../src/main.js')
|
8 | const path = require('path')
|
9 |
|
10 | require('chai').should()
|
11 |
|
12 | describe('transformations', function() {
|
13 | before(function() {
|
14 | return fs.emptyDir(path.join(__dirname, '.tmp'))
|
15 | })
|
16 | const modes = ['promise', 'callback', 'cli']
|
17 | modes.forEach((mode) => {
|
18 | itShouldTransform(path.join(__dirname, '/samples/image_2.jpg'), 'image_2.jpg', mode)
|
19 | itShouldTransform(path.join(__dirname, '/samples/image_3.jpg'), 'image_3.jpg', mode)
|
20 | itShouldTransform(path.join(__dirname, '/samples/image_4.jpg'), 'image_4.jpg', mode)
|
21 | itShouldTransform(path.join(__dirname, '/samples/image_5.jpg'), 'image_5.jpg', mode)
|
22 | itShouldTransform(path.join(__dirname, '/samples/image_6.jpg'), 'image_6.jpg', mode)
|
23 | itShouldTransform(path.join(__dirname, '/samples/image_7.jpg'), 'image_7.jpg', mode)
|
24 | itShouldTransform(path.join(__dirname, '/samples/image_8.jpg'), 'image_8.jpg', mode)
|
25 | itShouldTransform(path.join(__dirname, '/samples/image_exif.jpg'), 'image_exif.jpg', mode)
|
26 | if (mode !== 'cli') {
|
27 | itShouldTransform(fs.readFileSync(path.join(__dirname, '/samples/image_8.jpg')), 'From a buffer', mode)
|
28 | }
|
29 | })
|
30 | })
|
31 |
|
32 | function itShouldTransform(originPathOrBuffer, label, mode) {
|
33 | it('Should rotate image (' + label + ') (' + mode + ')', function(done) {
|
34 | this.timeout(20000)
|
35 | const options = {quality: 82}
|
36 | if (mode === 'promise') {
|
37 | testPromise(originPathOrBuffer, options, done)
|
38 | }
|
39 | if (mode === 'callback') {
|
40 | testCallback(originPathOrBuffer, options, done)
|
41 | }
|
42 | if (mode === 'cli') {
|
43 | testCli(originPathOrBuffer, options, done)
|
44 | }
|
45 | })
|
46 | }
|
47 |
|
48 | function testPromise(originPathOrBuffer, options, done) {
|
49 | jo.rotate(originPathOrBuffer, options).then(({buffer, orientation, dimensions, quality}) => {
|
50 | testTransformation(originPathOrBuffer, buffer, null, orientation, dimensions, quality)
|
51 | done()
|
52 | })
|
53 | }
|
54 |
|
55 | function testCallback(originPathOrBuffer, options, done) {
|
56 | jo.rotate(originPathOrBuffer, options, function(error, buffer, orientation, dimensions, quality) {
|
57 | testTransformation(originPathOrBuffer, buffer, error, orientation, dimensions, quality)
|
58 | done()
|
59 | })
|
60 | }
|
61 |
|
62 | function testCli(originPathOrBuffer, options, done) {
|
63 | transformWithCli(originPathOrBuffer, options.quality).then(({buffer, orientation, dimensions, quality}) => {
|
64 | testTransformation(originPathOrBuffer, buffer, null, orientation, dimensions, quality)
|
65 | done()
|
66 | })
|
67 | }
|
68 |
|
69 | function testTransformation(originPathOrBuffer, buffer, error, orientation, dimensions, quality) {
|
70 | const data = checkTransformation(originPathOrBuffer, buffer, orientation, dimensions)
|
71 | expect(error).to.equal(null)
|
72 | expect(quality).to.equal(82)
|
73 | expect(data.dimensionsMatch).to.equal(true)
|
74 | expect(data.exifMatch).to.equal(true)
|
75 | expect(data.pixelsMatch).to.equal(true)
|
76 | }
|