UNPKG

3.76 kBJavaScriptView Raw
1const describe = require('mocha').describe
2const exec = require('child_process').exec
3const expect = require('chai').expect
4const it = require('mocha').it
5const jo = require('../src/main.js')
6const path = require('path')
7
8require('chai').should()
9
10describe('errors', function() {
11 it('Should expose error types', function() {
12 function getTypeof(thing) {
13 return typeof thing
14 }
15 getTypeof(jo.errors).should.equals('object')
16 getTypeof(jo.errors.read_file).should.equals('string')
17 getTypeof(jo.errors.read_exif).should.equals('string')
18 getTypeof(jo.errors.no_orientation).should.equals('string')
19 getTypeof(jo.errors.unknown_orientation).should.equals('string')
20 getTypeof(jo.errors.correct_orientation).should.equals('string')
21 getTypeof(jo.errors.rotate_file).should.equals('string')
22 })
23
24 it('Should return an error if the orientation is 1 (callback)', function(done) {
25 jo.rotate(path.join(__dirname, '/samples/image_1.jpg'), {}, function(error, buffer) {
26 error.should.have.property('code').equal(jo.errors.correct_orientation)
27 Buffer.isBuffer(buffer).should.be.ok
28 done()
29 })
30 })
31
32 it('Should return an error if the orientation is 1 (promise)', function(done) {
33 jo.rotate(path.join(__dirname, '/samples/image_1.jpg'), {}).catch((error) => {
34 error.should.have.property('code').equal(jo.errors.correct_orientation)
35 done()
36 })
37 })
38
39 it('Should return an error if the orientation is 1 (cli)', function(done) {
40 exec('./src/cli.js ' + path.join(__dirname, '/samples/image_1.jpg'), (error, stdout) => {
41 stdout.should.contain('Orientation already correct')
42 done()
43 })
44 })
45
46 it('Should return an error if the image does not exist', function(done) {
47 jo.rotate('foo.jpg', {}, function(error, buffer, orientation) {
48 error.should.have.property('code').equal(jo.errors.read_file)
49 expect(buffer).to.equal(null)
50 expect(orientation).to.equal(null)
51 done()
52 })
53 })
54
55 it('Should return an error if the file is not an image', function(done) {
56 jo.rotate(path.join(__dirname, '/samples/textfile.md'), {}, function(error, buffer, orientation) {
57 error.should.have.property('code').equal(jo.errors.read_exif)
58 expect(buffer).to.equal(null)
59 expect(orientation).to.equal(null)
60 done()
61 })
62 })
63
64 it('Should return an error if the path is not a string/buffer', function(done) {
65 jo.rotate(['foo'], {}, function(error, buffer, orientation) {
66 error.should.have.property('code').equal(jo.errors.read_file)
67 expect(buffer).to.equal(null)
68 expect(orientation).to.equal(null)
69 done()
70 })
71 })
72
73 it('Should work if `options` is not an object', function(done) {
74 jo.rotate(path.join(__dirname, '/samples/image_2.jpg'), 'options', function(error, buffer, orientation) {
75 expect(error).to.equal(null)
76 Buffer.isBuffer(buffer).should.be.ok
77 expect(orientation).to.equal(2)
78 done()
79 })
80 })
81
82 it('Should return an error if the image has no orientation tag', function(done) {
83 jo.rotate(path.join(__dirname, '/samples/image_no_orientation.jpg'), {}, function(error, buffer, orientation) {
84 error.should.have.property('code').equal(jo.errors.no_orientation)
85 Buffer.isBuffer(buffer).should.be.ok
86 expect(orientation).to.equal(null)
87 done()
88 })
89 })
90
91 it('Should return an error if the image has an unknown orientation tag', function(done) {
92 jo.rotate(path.join(__dirname, '/samples/image_unknown_orientation.jpg'), {}, function(error, buffer, orientation) {
93 error.should.have.property('code').equal(jo.errors.unknown_orientation)
94 Buffer.isBuffer(buffer).should.be.ok
95 expect(orientation).to.equal(null)
96 done()
97 })
98 })
99})