UNPKG

1.92 kBJavaScriptView Raw
1import expect from "expect.js";
2
3class NexmoStub {
4 static create(functions) {
5 var stub = {
6 initialize: function() {},
7 hasBeenCalled: function(name) {
8 return this[name + "_called"] === true;
9 }
10 };
11 functions.forEach(function(name) {
12 stub[name + "_called"] = false;
13 stub[name] = function() {
14 this[name + "_called"] = true;
15 };
16 });
17
18 return stub;
19 }
20
21 /**
22 * @param {Object} mappings - a mapping from legacy global function to
23 * new non-global name.
24 */
25 static checkAllFunctionsAreDefined(mappings, obj) {
26 Object.keys(mappings).forEach(function(originalName) {
27 var newName = mappings[originalName].split("|")[0];
28 expect(obj.prototype[newName]).to.be.a("function");
29 });
30 }
31
32 /**
33 * @param {Object} mappings - a mapping from legacy global function to
34 * new non-global name.
35 */
36 static checkAllFunctionsAreCalled(mappings, ObjDef) {
37 Object.keys(mappings).forEach(function(originalName) {
38 var nameAndParams = mappings[originalName].split("|");
39 var newName = nameAndParams[0];
40 var params = nameAndParams[1] ? nameAndParams[1].split(",") : [];
41 params.forEach(function(paramValue, index) {
42 try {
43 params[index] = JSON.parse(paramValue);
44 } catch (e) {
45 // couldn't be parsed, which is fine.
46 // console.log('could not parse', paramValue);
47 }
48 });
49
50 var stub = NexmoStub.create(Object.keys(mappings));
51 var obj = new ObjDef(
52 {
53 apiKey: "test",
54 apiSecret: "test"
55 },
56 {
57 nexmoOverride: stub
58 }
59 );
60
61 // console.log('calling', newName, '(' + params + ')', 'expecting', originalName);
62 obj[newName].apply(obj, params);
63
64 expect(stub.hasBeenCalled(originalName)).to.be(true);
65 });
66 }
67}
68
69export default NexmoStub;