UNPKG

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