UNPKG

3.53 kBJavaScriptView Raw
1import chai, { expect } from "chai";
2import path from "path";
3import sinon from "sinon";
4import sinonChai from "sinon-chai";
5
6import ResourceTestHelper from "./ResourceTestHelper";
7
8import CallsResource from "../lib/CallsResource";
9import StreamResource from "../lib/StreamResource";
10import TalkResource from "../lib/TalkResource";
11import DtmfResource from "../lib/DtmfResource";
12import HttpClient from "../lib/HttpClient";
13import Credentials from "../lib/Credentials";
14
15chai.use(sinonChai);
16
17var creds = Credentials.parse({
18 applicationId: "some-id",
19 privateKey: path.join(__dirname, "private-test.key")
20});
21var emptyCallback = () => {};
22
23describe("CallsResource", () => {
24 var httpClientStub = null;
25 var calls = null;
26
27 beforeEach(() => {
28 httpClientStub = sinon.createStubInstance(HttpClient);
29 var options = {
30 httpClient: httpClientStub
31 };
32 calls = new CallsResource(creds, options);
33 });
34
35 it("should allow a call to be created", () => {
36 var params = {};
37 calls.create(params, emptyCallback);
38
39 var expectedRequestArgs = ResourceTestHelper.requestArgsMatch(params);
40 expect(httpClientStub.request).to.have.been.calledWith(
41 sinon.match(expectedRequestArgs),
42 emptyCallback
43 );
44 });
45
46 it("should throw an error if no query is provided", () => {
47 var expectThrow = function() {
48 calls.get();
49 };
50
51 expect(expectThrow).to.throw(Error);
52 });
53
54 it("should get a collection of calls", () => {
55 calls.get({}, emptyCallback);
56
57 var expectedRequestArgs = ResourceTestHelper.requestArgsMatch(null, {
58 method: "GET",
59 body: undefined,
60 path: `${CallsResource.PATH}`
61 });
62
63 expect(httpClientStub.request).to.have.been.calledWith(
64 sinon.match(expectedRequestArgs),
65 emptyCallback
66 );
67 });
68
69 it("should get a single of call using a call ID", () => {
70 const callId = "2342342-lkjhlkjh-32423";
71 calls.get(callId, emptyCallback);
72
73 var expectedRequestArgs = ResourceTestHelper.requestArgsMatch(null, {
74 method: "GET",
75 body: undefined,
76 path: `${CallsResource.PATH}/${callId}`
77 });
78
79 expect(httpClientStub.request).to.have.been.calledWith(
80 sinon.match(expectedRequestArgs),
81 emptyCallback
82 );
83 });
84
85 it("should get a allow calls to be queried by filter", () => {
86 calls.get(
87 {
88 status: "answered"
89 },
90 emptyCallback
91 );
92
93 var expectedRequestArgs = ResourceTestHelper.requestArgsMatch(null, {
94 method: "GET",
95 body: undefined,
96 path: `${CallsResource.PATH}?status=answered`
97 });
98
99 expect(httpClientStub.request).to.have.been.calledWith(
100 sinon.match(expectedRequestArgs),
101 emptyCallback
102 );
103 });
104
105 it("should allow a call to be updated", () => {
106 const callId = "2342342-lkjhlkjh-32423";
107 var params = {
108 action: "hangup"
109 };
110 calls.update(callId, params, emptyCallback);
111
112 var expectedRequestArgs = ResourceTestHelper.requestArgsMatch(params, {
113 method: "PUT",
114 path: `${CallsResource.PATH}/${callId}`
115 });
116
117 expect(httpClientStub.request).to.have.been.calledWith(
118 sinon.match(expectedRequestArgs),
119 emptyCallback
120 );
121 });
122
123 it("should expose a stream property", () => {
124 expect(calls.stream).to.be.an.instanceOf(StreamResource);
125 });
126
127 it("should expose a talk property", () => {
128 expect(calls.talk).to.be.an.instanceOf(TalkResource);
129 });
130
131 it("should expose a dtmf property", () => {
132 expect(calls.dtmf).to.be.an.instanceOf(DtmfResource);
133 });
134});