|
1 | | -import path from 'path'; |
| 1 | +import { deleteApp, getApps } from 'firebase-admin/app'; |
2 | 2 | import log from 'npmlog'; |
| 3 | +import Parse from 'parse/node.js'; |
| 4 | +import path from 'path'; |
3 | 5 | import FCM from '../src/FCM.js'; |
4 | | -import { getApps, deleteApp } from 'firebase-admin/app'; |
5 | | - |
6 | | -const testArgs = { |
7 | | - firebaseServiceAccount: path.join( |
8 | | - __dirname, |
9 | | - '..', |
10 | | - 'spec', |
11 | | - 'support', |
12 | | - 'fakeServiceAccount.json', |
13 | | - ), |
14 | | -}; |
| 6 | + |
| 7 | +let testArgs; |
15 | 8 |
|
16 | 9 | describe('FCM', () => { |
17 | 10 | beforeEach(async () => { |
18 | 11 | getApps().forEach(app => deleteApp(app)); |
| 12 | + |
| 13 | + testArgs = { |
| 14 | + firebaseServiceAccount: path.join( |
| 15 | + __dirname, |
| 16 | + '..', |
| 17 | + 'spec', |
| 18 | + 'support', |
| 19 | + 'fakeServiceAccount.json', |
| 20 | + ), |
| 21 | + }; |
19 | 22 | }); |
20 | 23 |
|
21 | 24 | it('can initialize', () => { |
@@ -221,6 +224,44 @@ describe('FCM', () => { |
221 | 224 | expect(spyError).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'error sending push: testing error abort'); |
222 | 225 | }); |
223 | 226 |
|
| 227 | + it('rejects exceptions that are unhandled by FCM client', async () => { |
| 228 | + const spyInfo = spyOn(log, 'info').and.callFake(() => {}); |
| 229 | + const spyError = spyOn(log, 'error').and.callFake(() => {}); |
| 230 | + testArgs.resolveUnhandledClientError = false; |
| 231 | + const fcm = new FCM(testArgs); |
| 232 | + spyOn(fcm.sender, 'sendEachForMulticast').and.callFake(() => { |
| 233 | + throw new Error('test error'); |
| 234 | + }); |
| 235 | + fcm.pushType = 'android'; |
| 236 | + const data = { data: { alert: 'alert' } }; |
| 237 | + const devices = [{ deviceToken: 'token' }]; |
| 238 | + await expectAsync(fcm.send(data, devices)).toBeRejectedWith(new Parse.Error(Parse.Error.OTHER_CAUSE, 'Error: test error')); |
| 239 | + expect(fcm.sender.sendEachForMulticast).toHaveBeenCalled(); |
| 240 | + expect(spyInfo).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'sending push to 1 devices'); |
| 241 | + expect(spyError).toHaveBeenCalledTimes(2); |
| 242 | + expect(spyError.calls.all()[0].args).toEqual(['parse-server-push-adapter FCM', 'error sending push: firebase client exception: Error: test error']); |
| 243 | + expect(spyError.calls.all()[1].args).toEqual(['parse-server-push-adapter FCM', 'error sending push: ParseError: -1 Error: test error']); |
| 244 | + }); |
| 245 | + |
| 246 | + it('resolves exceptions that are unhandled by FCM client', async () => { |
| 247 | + const spyInfo = spyOn(log, 'info').and.callFake(() => {}); |
| 248 | + const spyError = spyOn(log, 'error').and.callFake(() => {}); |
| 249 | + testArgs.resolveUnhandledClientError = true; |
| 250 | + const fcm = new FCM(testArgs); |
| 251 | + spyOn(fcm.sender, 'sendEachForMulticast').and.callFake(() => { |
| 252 | + throw new Error('test error'); |
| 253 | + }); |
| 254 | + fcm.pushType = 'android'; |
| 255 | + const data = { data: { alert: 'alert' } }; |
| 256 | + const devices = [{ deviceToken: 'token' }]; |
| 257 | + await expectAsync(fcm.send(data, devices)).toBeResolved(); |
| 258 | + expect(fcm.sender.sendEachForMulticast).toHaveBeenCalled(); |
| 259 | + expect(spyInfo).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'sending push to 1 devices'); |
| 260 | + expect(spyError).toHaveBeenCalledTimes(2); |
| 261 | + expect(spyError.calls.all()[0].args).toEqual(['parse-server-push-adapter FCM', 'error sending push: firebase client exception: Error: test error']); |
| 262 | + expect(spyError.calls.all()[1].args).toEqual(['parse-server-push-adapter FCM', 'error sending push: ParseError: -1 Error: test error']); |
| 263 | + }); |
| 264 | + |
224 | 265 | it('FCM request invalid push type', async () => { |
225 | 266 | const fcm = new FCM(testArgs); |
226 | 267 | fcm.pushType = 'invalid'; |
|
0 commit comments