Skip to content

Commit ba8a04c

Browse files
committed
add Parse tests
1 parent 2c38786 commit ba8a04c

File tree

13 files changed

+432
-0
lines changed

13 files changed

+432
-0
lines changed

parse/testApp/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.idea

parse/testApp/.jshint

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"node": true,
3+
"indent": 4
4+
}

parse/testApp/cloud/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pubnub.js

parse/testApp/cloud/main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var errorsTest = require('cloud/tests/errorsTest');
2+
3+
Parse.Cloud.define("testErrors", function (request, response) {
4+
errorsTest.run(response.success, response.error);
5+
});
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
var Pubnub = require('cloud/pubnub'),
2+
assert = require('assert'),
3+
Sandbox = require('cloud/utils/sandbox'),
4+
TestSuite = require('cloud/utils/testSuite');
5+
6+
var test = new TestSuite("Error callback"),
7+
pubnub,
8+
sandbox;
9+
10+
test.before(function () {
11+
pubnub = Pubnub.init({
12+
pubilsh_key: 'pub-c-a82961b5-d8ee-462c-bd16-0cd53edbbe09',
13+
subscribe_key: 'sub-c-f5674c7e-7d52-11e3-a993-02ee2ddab7fe',
14+
heartbeat_interval: false
15+
});
16+
});
17+
18+
test.beforeEach(function () {
19+
sandbox = Sandbox.create();
20+
});
21+
22+
test.it("should be invoked on JSON response without error description", function (done) {
23+
sandbox.stub(Parse.Cloud, 'httpRequest', function () {
24+
var deferred = new Parse.Promise();
25+
deferred.resolve("{}");
26+
return deferred;
27+
}, "request");
28+
29+
pubnub.publish({
30+
channel: "blah",
31+
message: "hey",
32+
callback: function () {
33+
done(new Error("Success callback invoked while shouldn't"))
34+
},
35+
error: function (error) {
36+
assert.equal(error.message, "Bad JSON response");
37+
done();
38+
}
39+
});
40+
});
41+
42+
test.it("should be invoked on JSON error without error description", function (done) {
43+
sandbox.stub(Parse.Cloud, 'httpRequest', function () {
44+
var deferred = new Parse.Promise();
45+
deferred.reject("{}");
46+
return deferred;
47+
}, "request");
48+
49+
pubnub.publish({
50+
channel: "blah",
51+
message: "hey",
52+
callback: function () {
53+
done(new Error("Success callback invoked while shouldn't"))
54+
},
55+
error: function (error) {
56+
assert.equal(error.message, "Network error");
57+
done();
58+
}
59+
});
60+
});
61+
62+
test.it("should be invoked on non-JSON response", function (done) {
63+
sandbox.stub(Parse.Cloud, 'httpRequest', function (options) {
64+
options.url = 'http://google.com';
65+
return sandbox.invokeOriginal("request", [options]);
66+
}, "request");
67+
68+
pubnub.publish({
69+
channel: "blah",
70+
message: "hey",
71+
callback: function () {
72+
done(new Error("Success callback invoked while shouldn't"))
73+
},
74+
error: function (error) {
75+
assert.equal(error.message, "Bad JSON response");
76+
done();
77+
}
78+
});
79+
});
80+
81+
test.it("should be invoked on non-JSON error", function (done) {
82+
sandbox.stub(Parse.Cloud, 'httpRequest', function (options) {
83+
options.url = 'http://blah.hey';
84+
return sandbox.invokeOriginal("request", [options]);
85+
}, "request");
86+
87+
pubnub.publish({
88+
channel: "blah",
89+
message: "hey",
90+
callback: function () {
91+
done(new Error("Success callback invoked while shouldn't"))
92+
},
93+
error: function (error) {
94+
assert.equal(error.message, "Network error");
95+
done();
96+
}
97+
});
98+
});
99+
100+
test.it("should be invoked on any #subscribe() invocation", function (done) {
101+
assert.throws(function () {
102+
pubnub.subscribe({});
103+
});
104+
done();
105+
});
106+
107+
test.afterEach(function () {
108+
sandbox.restore();
109+
});
110+
111+
module.exports = test;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* Sinon.js && Parse.com compatible sandbox and stub implementation
3+
* @type {{create: function}}
4+
*/
5+
var sandbox = {
6+
create: function () {
7+
return new Sandbox();
8+
}
9+
};
10+
11+
function Sandbox() {
12+
this.stubs = {};
13+
}
14+
15+
/**
16+
* Stub method, compatible with Parse.com environment
17+
*
18+
* @param object
19+
* @param method
20+
* @param func
21+
* @param key
22+
*/
23+
Sandbox.prototype.stub = function stub(object, method, func, key) {
24+
if (typeof method !== 'string') {
25+
throw Error("#stub() 'method' argument should be a string");
26+
}
27+
28+
if (!(method in object || typeof object[method] !== 'function')) {
29+
throw Error('Stubbing element is not a function');
30+
}
31+
32+
if (!key) {
33+
key = uuid();
34+
}
35+
36+
this.stubs[key] = new Stub(object, method, object[method]);
37+
38+
object[method] = func;
39+
};
40+
41+
Sandbox.prototype.restore = function restore() {
42+
var keys = Object.keys(this.stubs),
43+
length = keys.length,
44+
i;
45+
46+
for (i = 0; i < length; i++) {
47+
this.stubs[keys[i]].restore();
48+
}
49+
};
50+
51+
Sandbox.prototype.invokeOriginal = function (key, args) {
52+
return this.stubs[key].invokeOriginal(args);
53+
};
54+
55+
function Stub(object, method, func) {
56+
this.object = object;
57+
this.method = method;
58+
this.func = func;
59+
}
60+
61+
Stub.prototype.restore = function () {
62+
return this.object[this.method] = this.func;
63+
};
64+
65+
Stub.prototype.invokeOriginal = function (args) {
66+
return this.func.apply(this.object, args)
67+
};
68+
69+
function uuid() {
70+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,
71+
function (c) {
72+
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
73+
return v.toString(16);
74+
});
75+
}
76+
77+
module.exports = sandbox;

parse/testApp/cloud/utils/test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function Test(name, func) {
2+
this.name = name;
3+
this.func = func;
4+
}
5+
6+
Test.prototype.run = function (next) {
7+
var _this = this;
8+
9+
console.log('>>> TEST: ' + this.name);
10+
11+
try {
12+
this.func(function (error) {
13+
if (error instanceof Error) {
14+
throw error;
15+
}
16+
17+
next({
18+
error: false,
19+
name: _this.name
20+
});
21+
});
22+
23+
} catch (e) {
24+
next({
25+
error: true,
26+
name: _this.name,
27+
message: e.toString(),
28+
stacktrace: e.stack
29+
});
30+
}
31+
};
32+
33+
module.exports = Test;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
var Test = require("cloud/utils/test");
2+
3+
function TestSuite(name) {
4+
this.name = name;
5+
this.beforeFunc = function () {};
6+
this.afterFunc = function () {};
7+
this.beforeEachFunc = function () {};
8+
this.afterEachFunc = function () {};
9+
this.tests = [];
10+
this.onlyFlag = false;
11+
}
12+
13+
TestSuite.prototype.before = function (func) {
14+
this.beforeFunc = func;
15+
};
16+
17+
TestSuite.prototype.beforeEach = function (func) {
18+
this.beforeEachFunc = func;
19+
};
20+
21+
TestSuite.prototype.afterEach = function (func) {
22+
this.afterEachFunc = func;
23+
};
24+
25+
TestSuite.prototype.it = function (name, func) {
26+
if (this.onlyFlag) return;
27+
28+
this.tests.push(new Test(this.name + " " + name + ".", func));
29+
};
30+
31+
TestSuite.prototype.xit = function () {};
32+
33+
TestSuite.prototype.only = function (name, func) {
34+
this.tests = [];
35+
this.onlyFlag = true;
36+
this.tests.push(new Test(this.name + " " + name + ".", func));
37+
};
38+
39+
TestSuite.prototype.run = function (success, error) {
40+
var results = [],
41+
_this = this,
42+
i = 0,
43+
currentTest;
44+
45+
this.beforeFunc();
46+
47+
function runNextTest() {
48+
currentTest = _this.tests[i];
49+
50+
if (currentTest instanceof Test) {
51+
_this.beforeEachFunc();
52+
currentTest.run(function (result) {
53+
results.push(result);
54+
_this.afterEachFunc();
55+
i++;
56+
runNextTest();
57+
})
58+
} else {
59+
_this.afterFunc();
60+
success({
61+
name: _this.name,
62+
payload: results
63+
});
64+
}
65+
}
66+
67+
runNextTest();
68+
};
69+
70+
module.exports = TestSuite;

parse/testApp/config/global.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"global": {
3+
"parseVersion": "1.4.2"
4+
},
5+
"applications": {
6+
"_default": {
7+
"link": "demoApp"
8+
},
9+
"demoApp": {
10+
"applicationId": "zZOc0BCJc8LwfAFnw4wlK9W5taNf7umvTginEnGt",
11+
"masterKey": "2JIxv19E9sxzJ5Lw309Qg5AwHLPhEkKmOJFCyBTU"
12+
}
13+
}
14+
}

parse/testApp/public/.bowerrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"scripts": {
3+
"preinstall": "cp ../../pubnub.js ../cloud/"
4+
}
5+
}

0 commit comments

Comments
 (0)