|
| 1 | +const chai = require("chai"); |
| 2 | +const sinon = require("sinon"); |
| 3 | + |
| 4 | +const expect = chai.expect; |
| 5 | + |
| 6 | +opts = { |
| 7 | + publicKey: "test_public_key", |
| 8 | + urlEndpoint: "https://ik.imagekit.io/test_url_endpoint", |
| 9 | + authenticationEndpoint: "http://test/auth" |
| 10 | +} |
| 11 | + |
| 12 | +var ImageKit = require("../"); |
| 13 | + |
| 14 | + |
| 15 | +describe("Testing SDK", function () { |
| 16 | + |
| 17 | + describe("Imagekit Class", function () { |
| 18 | + |
| 19 | + var imagekit = new ImageKit(opts); |
| 20 | + |
| 21 | + describe("imagekit instance", function () { |
| 22 | + it('should have options object', function () { |
| 23 | + expect(imagekit.options).to.be.an('object'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should have correctly initialized options object.', function() { |
| 27 | + expect(imagekit.options).to.have.property('publicKey').to.be.equal(opts.publicKey); |
| 28 | + expect(imagekit.options).to.have.property('urlEndpoint').to.be.equal(opts.urlEndpoint); |
| 29 | + expect(imagekit.options).to.have.property('authenticationEndpoint').to.be.equal(opts.authenticationEndpoint); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should have callable functions 'url' and 'upload'", function() { |
| 33 | + expect(imagekit.url).to.exist.and.to.be.a('function'); |
| 34 | + expect(imagekit.upload).to.exist.and.to.be.a('function'); |
| 35 | + }); |
| 36 | + |
| 37 | + describe(".url method", function() { |
| 38 | + it('should generate the correct url with path param', function() { |
| 39 | + const url = imagekit.url({ |
| 40 | + path: "/test_path.jpg", |
| 41 | + transformation : [{ |
| 42 | + "height" : "300", |
| 43 | + "width" : "400" |
| 44 | + }] |
| 45 | + }) |
| 46 | + expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test\_url\_endpoint/); |
| 47 | + expect(url).to.match(new RegExp("\/tr:")); |
| 48 | + expect(url).to.match(new RegExp("w-400")); |
| 49 | + expect(url).to.match(new RegExp("h-300")); |
| 50 | + expect(url).to.match(/\/test_path\.jpg[^\/][\?]?/); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should generate the correct url with src param', function() { |
| 54 | + const url = imagekit.url({ |
| 55 | + src : "https://ik.imagekit.io/test_url_endpoint/endpoint/test_path.jpg", |
| 56 | + transformation : [{ |
| 57 | + "height" : "300", |
| 58 | + "width" : "400" |
| 59 | + }] |
| 60 | + }); |
| 61 | + expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test_url_endpoint\/endpoint\/test\_path\.jpg/); |
| 62 | + expect(url).to.match(/\&tr=/); |
| 63 | + expect(url).to.match(new RegExp("w-400")); |
| 64 | + expect(url).to.match(new RegExp("h-300")); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe(".upload method", function() { |
| 69 | + const requestUtils = require('../utils/request.js'); |
| 70 | + sinon.stub(requestUtils, 'generateSignatureToken').callsFake(function (options, callback) { |
| 71 | + callback(null, { |
| 72 | + signature: "test_signature", |
| 73 | + expire: 123, |
| 74 | + token: "test_token" |
| 75 | + }) |
| 76 | + }); |
| 77 | + |
| 78 | + sinon.stub(requestUtils, 'uploadFile').callsFake(function (options, callback) { |
| 79 | + callback(null, "success") |
| 80 | + }); |
| 81 | + |
| 82 | + global.FormData = require('formdata-node'); |
| 83 | + |
| 84 | + |
| 85 | + const fileOptions = { |
| 86 | + fileName: "test_file_name", |
| 87 | + file: "test_file", |
| 88 | + }; |
| 89 | + |
| 90 | + it('calls relavant functions in order', function() { |
| 91 | + imagekit.upload(fileOptions, function(){}); |
| 92 | + expect(requestUtils.generateSignatureToken.calledOnce).to.be.true; |
| 93 | + expect(requestUtils.uploadFile.calledOnce).to.be.true; |
| 94 | + }); |
| 95 | + |
| 96 | + it('calls functions with correct arguments', function() { |
| 97 | + const arg = requestUtils.uploadFile.getCall(0).args[0]; |
| 98 | + expect(arg.get('file')).to.be.equal("test_file"); |
| 99 | + expect(arg.get('fileName')).to.be.equal("test_file_name"); |
| 100 | + expect(arg.get('token')).to.be.equal("test_token"); |
| 101 | + expect(arg.get('expire')).to.be.equal("123"); |
| 102 | + expect(arg.get('signature')).to.be.equal("test_signature"); |
| 103 | + }); |
| 104 | + |
| 105 | + }); |
| 106 | + }); |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
0 commit comments