This repository has been archived by the owner on Feb 8, 2019. It is now read-only.
/
GeotriggerSpec.js
192 lines (159 loc) · 4.76 KB
/
GeotriggerSpec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
if(typeof module === "object"){
var Geotrigger = require("../geotrigger");
var config = require("./config");
}
var ClientId = config.clientId;
var ClientSecret = config.clientSecret;
describe("geotrigger.js", function() {
it("should throw an error if initialized without a client id or session", function(){
expect(function(){
var geotriggers = new Geotrigger.Session();
}).toThrow();
});
it("should fire an `authenticated` event after initializing successfully with a client id and secret", function(){
var spy = jasmine.createSpy();
var geotriggers;
runs(function(){
geotriggers = new Geotrigger.Session({
clientId: ClientId,
clientSecret: ClientSecret,
persistSession: false
});
geotriggers.on("authentication:success", spy);
});
waitsFor(function(){
return geotriggers.authenticated();
}, "Did not auth", 6000);
runs(function(){
expect(spy).toHaveBeenCalled();
});
});
it("should fire an `authenticated` event after initializing successfully with a client id", function(){
var spy = jasmine.createSpy();
var geotriggers;
runs(function(){
geotriggers = new Geotrigger.Session({
clientId: ClientId,
persistSession: false
});
geotriggers.on("authentication:success", spy);
});
waitsFor(function(){
return geotriggers.authenticated();
}, "Did not auth", 3000);
runs(function(){
expect(spy).toHaveBeenCalled();
expect(geotriggers.authenticated()).toBeTruthy();
});
});
describe("api request methods", function(){
var geotriggers = new Geotrigger.Session({
clientId: ClientId,
persistSession: false
});
it("should get a list of devices with a callback", function(){
var spy = jasmine.createSpy();
runs(function(){
geotriggers.request("device/list", spy);
});
waitsFor(function(){
return spy.callCount;
}, "Did not make request for device/list", 3000);
runs(function(){
var callbackArgs = spy.mostRecentCall.args;
expect(callbackArgs[0]).toBeFalsy();
expect(callbackArgs[1]).objectToLooselyMatch({
devices: [{
deviceId: "xxx",
deviceSecret: null,
createdOn: "date",
tags: ["deviceTag"],
updatedAt: null
}]
}, null, {});
expect(callbackArgs[2]).toBeInstanceOf(XMLHttpRequest);
});
});
it("should be able to update a device", function(){
var spy = jasmine.createSpy();
runs(function(){
geotriggers.request("device/update", {
addTags: ["foo"],
properties: {
foo: "bar"
}
}, spy);
});
waitsFor(function(){
return spy.callCount;
}, "Did not make request for device/update", 3000);
runs(function(){
expect(spy).toHaveBeenCalledWithArgsLike(null, {
devices: [{
deviceId: "xxx",
tags: ["deviceTag", "foo"],
properties: {
foo: "bar"
},
trackingProfile: null,
lastSeen: "date"
}]
});
});
});
it("should be able to update location", function(){
var spy = jasmine.createSpy();
runs(function(){
geotriggers.request("location/update", {
locations: [{
"timestamp": "2012-05-09T16:03:53-0700",
"planet": "earth",
"latitude": 45.51294827744629,
"longitude": -122.66232132911682,
"accuracy": 10.0,
"speed": null,
"altitude": 0,
"bearing": null,
"verticalAccuracy": null,
"properties": {}
}]
}, spy);
});
waitsFor(function(){
return spy.callCount;
}, "Did not run callback", 3000);
runs(function(){
expect(spy).toHaveBeenCalled();
});
});
it("should be able to create a trigger", function(){
var spy = jasmine.createSpy();
runs(function(){
geotriggers.request("trigger/create", {
condition: {
direction: "enter",
geo: {
geojson: {
type: "Polygon",
coordinates: [
[ [-122.65, 45.55], [-122.65, 45.50], [-122.62, 45.50], [-122.62, 45.55], [-122.65, 45.55] ]
]
}
}
},
action: {
notification:{
text: "At some random polygon in portland"
}
}
}, spy);
});
waitsFor(function(){
return spy.callCount;
}, "Did not run callback", 3000);
runs(function(){
expect(spy).toHaveBeenCalled();
});
});
});
});