UNPKG

2.63 kBJavaScriptView Raw
1import Conversion from "../lib/Conversion";
2import Credentials from "../lib/Credentials";
3import HttpClient from "../lib/HttpClient";
4import NullLogger from "../lib/ConsoleLogger";
5
6import ResourceTestHelper from "./ResourceTestHelper";
7import sinon from "sinon";
8import chai, { expect } from "chai";
9import sinonChai from "sinon-chai";
10chai.use(sinonChai);
11
12describe("Conversion", function() {
13 beforeEach(function() {
14 var creds = Credentials.parse({
15 apiKey: "myKey",
16 apiSecret: "mySecret"
17 });
18
19 this.httpClientStub = new HttpClient(
20 {
21 logger: new NullLogger()
22 },
23 creds
24 );
25
26 sinon.stub(this.httpClientStub, "request");
27
28 var options = {
29 api: this.httpClientStub
30 };
31
32 this.conversion = new Conversion(creds, options);
33 });
34
35 describe("#submit", function() {
36 it("should call the correct endpoint", function(done) {
37 this.httpClientStub.request.yields(null, {});
38
39 var expectedRequestArgs = ResourceTestHelper.requestArgsMatch({
40 path:
41 "/conversions/foo?message-id=1234&delivered=1&timestamp=1513254618"
42 });
43
44 this.conversion.submit(
45 "foo",
46 "1234",
47 1,
48 1513254618,
49 function(err, data) {
50 expect(this.httpClientStub.request).to.have.been.calledWith(
51 sinon.match(expectedRequestArgs)
52 );
53
54 done();
55 }.bind(this)
56 );
57 });
58
59 it("returns a friendly error when not enabled", function(done) {
60 const mockError = {
61 status: 402
62 };
63
64 this.httpClientStub.request.yields(mockError, null);
65 this.conversion.sms("1234", 1, 1234567890, function(err, data) {
66 expect(err._INFO_).to.eql(
67 "This endpoint may need activating on your account. Please email support@nexmo.com for more information"
68 );
69 expect(data).to.eql(null);
70 done();
71 });
72 });
73 });
74
75 describe("#voice", function() {
76 it("calls the correct endpoint for voice", function() {
77 const submitStub = sinon.stub(this.conversion, "submit");
78 this.conversion.voice("1234", 1, 1513254618);
79 expect(submitStub).to.have.been.calledWith(
80 "voice",
81 "1234",
82 1,
83 1513254618
84 );
85 submitStub.restore();
86 });
87 });
88
89 describe("#sms", function() {
90 it("calls the correct endpoint for sms", function() {
91 const submitStub = sinon.stub(this.conversion, "submit");
92 this.conversion.sms("1234", 1, 1513254618);
93 expect(submitStub).to.have.been.calledWith("sms", "1234", 1, 1513254618);
94 submitStub.restore();
95 });
96 });
97});