|
1 | 1 | var PushController = require('../src/Controllers/PushController').PushController;
|
2 | 2 |
|
| 3 | +var Config = require('../src/Config'); |
| 4 | + |
3 | 5 | describe('PushController', () => {
|
4 | 6 | it('can check valid master key of request', (done) => {
|
5 | 7 | // Make mock request
|
@@ -127,5 +129,121 @@ describe('PushController', () => {
|
127 | 129 | }).toThrow();
|
128 | 130 | done();
|
129 | 131 | });
|
| 132 | + |
| 133 | + it('properly increment badges', (done) => { |
| 134 | + |
| 135 | + var payload = { |
| 136 | + alert: "Hello World!", |
| 137 | + badge: "Increment", |
| 138 | + } |
| 139 | + var installations = []; |
| 140 | + while(installations.length != 10) { |
| 141 | + var installation = new Parse.Object("_Installation"); |
| 142 | + installation.set("installationId", "installation_"+installations.length); |
| 143 | + installation.set("deviceToken","device_token_"+installations.length) |
| 144 | + installation.set("badge", installations.length); |
| 145 | + installation.set("originalBadge", installations.length); |
| 146 | + installation.set("deviceType", "ios"); |
| 147 | + installations.push(installation); |
| 148 | + } |
| 149 | + |
| 150 | + while(installations.length != 15) { |
| 151 | + var installation = new Parse.Object("_Installation"); |
| 152 | + installation.set("installationId", "installation_"+installations.length); |
| 153 | + installation.set("deviceToken","device_token_"+installations.length) |
| 154 | + installation.set("deviceType", "android"); |
| 155 | + installations.push(installation); |
| 156 | + } |
| 157 | + |
| 158 | + var pushAdapter = { |
| 159 | + send: function(body, installations) { |
| 160 | + var badge = body.badge; |
| 161 | + installations.forEach((installation) => { |
| 162 | + if (installation.deviceType == "ios") { |
| 163 | + expect(installation.badge).toEqual(badge); |
| 164 | + expect(installation.originalBadge+1).toEqual(installation.badge); |
| 165 | + } else { |
| 166 | + expect(installation.badge).toBeUndefined(); |
| 167 | + } |
| 168 | + }) |
| 169 | + return Promise.resolve({ |
| 170 | + body: body, |
| 171 | + installations: installations |
| 172 | + }) |
| 173 | + }, |
| 174 | + getValidPushTypes: function() { |
| 175 | + return ["ios", "android"]; |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + var config = new Config(Parse.applicationId); |
| 180 | + var auth = { |
| 181 | + isMaster: true |
| 182 | + } |
| 183 | + |
| 184 | + var pushController = new PushController(pushAdapter, Parse.applicationId); |
| 185 | + Parse.Object.saveAll(installations).then((installations) => { |
| 186 | + return pushController.sendPush(payload, {}, config, auth); |
| 187 | + }).then((result) => { |
| 188 | + done(); |
| 189 | + }, (err) => { |
| 190 | + console.error(err); |
| 191 | + fail("should not fail"); |
| 192 | + done(); |
| 193 | + }); |
| 194 | + |
| 195 | + }); |
| 196 | + |
| 197 | + it('properly set badges to 1', (done) => { |
| 198 | + |
| 199 | + var payload = { |
| 200 | + alert: "Hello World!", |
| 201 | + badge: 1, |
| 202 | + } |
| 203 | + var installations = []; |
| 204 | + while(installations.length != 10) { |
| 205 | + var installation = new Parse.Object("_Installation"); |
| 206 | + installation.set("installationId", "installation_"+installations.length); |
| 207 | + installation.set("deviceToken","device_token_"+installations.length) |
| 208 | + installation.set("badge", installations.length); |
| 209 | + installation.set("originalBadge", installations.length); |
| 210 | + installation.set("deviceType", "ios"); |
| 211 | + installations.push(installation); |
| 212 | + } |
| 213 | + |
| 214 | + var pushAdapter = { |
| 215 | + send: function(body, installations) { |
| 216 | + var badge = body.badge; |
| 217 | + installations.forEach((installation) => { |
| 218 | + expect(installation.badge).toEqual(badge); |
| 219 | + expect(1).toEqual(installation.badge); |
| 220 | + }) |
| 221 | + return Promise.resolve({ |
| 222 | + body: body, |
| 223 | + installations: installations |
| 224 | + }) |
| 225 | + }, |
| 226 | + getValidPushTypes: function() { |
| 227 | + return ["ios"]; |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + var config = new Config(Parse.applicationId); |
| 232 | + var auth = { |
| 233 | + isMaster: true |
| 234 | + } |
| 235 | + |
| 236 | + var pushController = new PushController(pushAdapter, Parse.applicationId); |
| 237 | + Parse.Object.saveAll(installations).then((installations) => { |
| 238 | + return pushController.sendPush(payload, {}, config, auth); |
| 239 | + }).then((result) => { |
| 240 | + done(); |
| 241 | + }, (err) => { |
| 242 | + console.error(err); |
| 243 | + fail("should not fail"); |
| 244 | + done(); |
| 245 | + }); |
| 246 | + |
| 247 | + }) |
130 | 248 |
|
131 | 249 | });
|
0 commit comments