UNPKG

3.65 kBJavaScriptView Raw
1/* global describe, it */
2const API_TOKEN = process.env.MJ_API_TOKEN
3
4var Mailjet = require('../mailjet-client')
5var chai = require('chai')
6var expect = chai.expect
7var should = chai.should() // eslint-disable-line no-unused-vars
8var Promise = require('bluebird')
9
10if (typeof API_TOKEN === 'undefined') {
11 throw new Error(
12 'Mailjet API_TOKEN is required, respectively ' + API_TOKEN + ' given'
13 )
14}
15
16describe('Basic Usage', function() {
17 var client = Mailjet.connect(API_TOKEN)
18
19 describe('connection', function() {
20 it('creates instance of the client', function() {
21 var connectionType1 = new Mailjet(API_TOKEN)
22 var connectionType2 = new Mailjet().connect(API_TOKEN)
23 var connectionType3 = Mailjet.connect(API_TOKEN)
24
25 expect(connectionType1.apiToken).to.equal(API_TOKEN)
26 expect(connectionType2.apiToken).to.equal(API_TOKEN)
27 expect(connectionType3.apiToken).to.equal(API_TOKEN)
28 })
29
30 it('creates an instance of the client wiht options', function() {
31 var smsOptions = {
32 version: 'v4'
33 }
34
35 var connectionType1 = new Mailjet(API_TOKEN, smsOptions)
36 var connectionType2 = new Mailjet().connect(API_TOKEN, smsOptions)
37 var connectionType3 = Mailjet.connect(API_TOKEN, smsOptions)
38
39 var connections = [connectionType1, connectionType2, connectionType3]
40
41 connections.forEach(function(connection) {
42 expect(connection).to.have.property('apiToken', API_TOKEN)
43 expect(connection.options).to.have.property(
44 'version',
45 smsOptions.version
46 )
47 })
48 })
49 })
50
51 describe('method call', function() {
52 describe('get', function() {
53 var smsGet = client.get('sms')
54 it('calls retrieve sms action count with parameters', function(done) {
55 var countRequest = smsGet.action('count')
56
57 var promise = countRequest
58 .request({ FromTS: +new Date, ToTS: +new Date })
59 .then(function(result) {
60 expect(result.body).should.be.a('object')
61 expect(result.body.count).to.equal(0)
62 done()
63 })
64 .catch(function(reason) {
65 expect(reason.ErrorMessage).to.equal(undefined)
66 done()
67 })
68 expect(Promise.prototype.isPrototypeOf(promise)).to.equal(true)
69 })
70
71 it('retirieve list of messages', function(done) {
72 var promise = smsGet
73 .request({ FromTS: +new Date, ToTS: +new Date })
74 .then(function(response) {
75 expect(response.body).to.be.a('object')
76 expect(response.body.Data.length).to.equal(0)
77 done()
78 })
79 .catch(function(reason) {
80 expect(reason).to.equal(undefined)
81 done()
82 })
83 expect(Promise.prototype.isPrototypeOf(promise)).to.equal(true)
84 })
85 })
86
87 describe('post', function() {
88 it('export sms statisitcs action with timestamp bigger than one year', function(done) {
89 var promise = client
90 .post('sms')
91 .action('export')
92 .request({
93 FromTS: 1033552800,
94 ToTS: 1033574400
95 })
96 .then(function(response) {
97 expect(response.body).to.be.a('object')
98 done()
99 })
100 .catch(function(reason) {
101 expect(reason.statusCode).to.equal(400)
102 expect(reason.ErrorMessage).to.equal(
103 'FromTS must not be older than one year.'
104 )
105 expect(reason.message).to.include('Unsuccessful')
106 done()
107 })
108 expect(Promise.prototype.isPrototypeOf(promise)).to.equal(true)
109 })
110 })
111 })
112})