Skip to content

Commit 032cd76

Browse files
authored
test: Improve code coverage (#265)
1 parent b7f0bd4 commit 032cd76

File tree

3 files changed

+201
-12
lines changed

3 files changed

+201
-12
lines changed

spec/EXPO.spec.js

+7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ describe('EXPO', () => {
2828
expect(function() { new EXPO(undefined); }).toThrow();
2929
});
3030

31+
it('does log on invalid payload', async () => {
32+
const spy = spyOn(log, 'warn');
33+
const expo = new EXPO({});
34+
expo.send();
35+
expect(spy).toHaveBeenCalledWith('parse-server-push-adapter EXPO', 'invalid push payload');
36+
});
37+
3138
it('can send successful EXPO request', async () => {
3239
const spy = spyOn(log, 'verbose');
3340

spec/FCM.spec.js

+192-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,203 @@
11
import path from 'path';
2+
import log from 'npmlog';
23
import FCM from '../src/FCM.js';
34

5+
const testArgs = {
6+
firebaseServiceAccount: path.join(
7+
__dirname,
8+
'..',
9+
'spec',
10+
'support',
11+
'fakeServiceAccount.json',
12+
),
13+
};
14+
415
describe('FCM', () => {
516
it('can initialize', () => {
6-
const args = {
7-
firebaseServiceAccount: path.join(
8-
__dirname,
9-
'..',
10-
'spec',
11-
'support',
12-
'fakeServiceAccount.json',
13-
),
14-
};
15-
const fcm = new FCM(args);
17+
const fcm = new FCM(testArgs);
1618
expect(fcm).toBeDefined();
1719
});
1820

21+
it('can throw on initializing with invalid args', () => {
22+
expect(function() { new FCM(123); }).toThrow();
23+
expect(function() { new FCM({}); }).toThrow();
24+
});
25+
26+
it('does log on invalid payload', async () => {
27+
const spy = spyOn(log, 'warn');
28+
const fcm = new FCM(testArgs);
29+
fcm.send();
30+
expect(spy).toHaveBeenCalled();
31+
expect(spy).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'invalid push payload');
32+
});
33+
34+
it('can send successful FCM android request', async () => {
35+
const spyVerbose = spyOn(log, 'verbose').and.callFake(() => {});
36+
const spyInfo = spyOn(log, 'info').and.callFake(() => {});
37+
const fcm = new FCM(testArgs);
38+
spyOn(fcm.sender, 'sendEachForMulticast').and.callFake(() => {
39+
return Promise.resolve({
40+
responses: [{ success: true }],
41+
});
42+
});
43+
fcm.pushType = 'android';
44+
const data = { data: { alert: 'alert' } };
45+
const devices = [{ deviceToken: 'token' }];
46+
const response = await fcm.send(data, devices);
47+
expect(fcm.sender.sendEachForMulticast).toHaveBeenCalled();
48+
const args = fcm.sender.sendEachForMulticast.calls.first().args;
49+
expect(args.length).toEqual(1);
50+
expect(args[0].android.priority).toEqual('high');
51+
expect(args[0].android.data.data).toEqual('{"alert":"alert"}');
52+
expect(args[0].tokens).toEqual(['token']);
53+
expect(spyVerbose).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'tokens with successful pushes: ["token"]');
54+
expect(spyInfo).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'sending push to 1 devices');
55+
expect(response).toEqual([[{
56+
device: { deviceToken: 'token', deviceType: undefined },
57+
transmitted: true
58+
}]]);
59+
});
60+
61+
it('can send successful FCM android request with apns integer keys', async () => {
62+
const spyVerbose = spyOn(log, 'verbose').and.callFake(() => {});
63+
const spyInfo = spyOn(log, 'info').and.callFake(() => {});
64+
const fcm = new FCM(testArgs);
65+
spyOn(fcm.sender, 'sendEachForMulticast').and.callFake(() => {
66+
return Promise.resolve({
67+
responses: [{ success: true }],
68+
});
69+
});
70+
fcm.pushType = 'android';
71+
const data = { data: { alert: 'alert', badge: 1 } };
72+
const devices = [{ deviceToken: 'token' }];
73+
const response = await fcm.send(data, devices);
74+
expect(fcm.sender.sendEachForMulticast).toHaveBeenCalled();
75+
const args = fcm.sender.sendEachForMulticast.calls.first().args;
76+
expect(args.length).toEqual(1);
77+
expect(args[0].android.priority).toEqual('high');
78+
// Should not include badge key in data
79+
expect(args[0].android.data.data).toEqual('{"alert":"alert"}');
80+
expect(args[0].tokens).toEqual(['token']);
81+
expect(spyVerbose).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'tokens with successful pushes: ["token"]');
82+
expect(spyInfo).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'sending push to 1 devices');
83+
expect(response).toEqual([[{
84+
device: { deviceToken: 'token', deviceType: undefined },
85+
transmitted: true
86+
}]]);
87+
});
88+
89+
it('can send successful FCM apple request with alert', async () => {
90+
const spyVerbose = spyOn(log, 'verbose').and.callFake(() => {});
91+
const spyInfo = spyOn(log, 'info').and.callFake(() => {});
92+
const fcm = new FCM(testArgs);
93+
spyOn(fcm.sender, 'sendEachForMulticast').and.callFake(() => {
94+
return Promise.resolve({
95+
responses: [{ success: true }],
96+
});
97+
});
98+
fcm.pushType = 'apple';
99+
const data = { data: { alert: 'alert' } };
100+
const devices = [{ deviceToken: 'token' }];
101+
const response = await fcm.send(data, devices);
102+
expect(fcm.sender.sendEachForMulticast).toHaveBeenCalled();
103+
const args = fcm.sender.sendEachForMulticast.calls.first().args;
104+
expect(args.length).toEqual(1);
105+
expect(args[0].apns.payload).toEqual({ aps: { alert: { body: 'alert' } } });
106+
expect(args[0].apns.headers).toEqual({ 'apns-push-type': 'alert' });
107+
expect(args[0].tokens).toEqual(['token']);
108+
expect(spyVerbose).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'tokens with successful pushes: ["token"]');
109+
expect(spyInfo).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'sending push to 1 devices');
110+
expect(response).toEqual([[{
111+
device: { deviceToken: 'token', deviceType: undefined },
112+
transmitted: true
113+
}]]);
114+
});
115+
116+
it('can send successful FCM apple request with title', async () => {
117+
const spyVerbose = spyOn(log, 'verbose').and.callFake(() => {});
118+
const spyInfo = spyOn(log, 'info').and.callFake(() => {});
119+
const fcm = new FCM(testArgs);
120+
spyOn(fcm.sender, 'sendEachForMulticast').and.callFake(() => {
121+
return Promise.resolve({
122+
responses: [{ success: true }],
123+
});
124+
});
125+
fcm.pushType = 'apple';
126+
const data = { data: { title: 'title' } };
127+
const devices = [{ deviceToken: 'token' }];
128+
const response = await fcm.send(data, devices);
129+
expect(fcm.sender.sendEachForMulticast).toHaveBeenCalled();
130+
const args = fcm.sender.sendEachForMulticast.calls.first().args;
131+
expect(args.length).toEqual(1);
132+
expect(args[0].apns.payload).toEqual({ aps: { alert: { title: 'title' } } });
133+
expect(args[0].apns.headers).toEqual({ 'apns-push-type': 'alert' });
134+
expect(args[0].tokens).toEqual(['token']);
135+
expect(spyVerbose).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'tokens with successful pushes: ["token"]');
136+
expect(spyInfo).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'sending push to 1 devices');
137+
expect(response).toEqual([[{
138+
device: { deviceToken: 'token', deviceType: undefined },
139+
transmitted: true
140+
}]]);
141+
});
142+
143+
it('can send failed FCM request', async () => {
144+
const spyInfo = spyOn(log, 'info').and.callFake(() => {});
145+
const spyError = spyOn(log, 'error').and.callFake(() => {});
146+
const fcm = new FCM(testArgs);
147+
spyOn(fcm.sender, 'sendEachForMulticast').and.callFake(() => {
148+
return Promise.resolve({
149+
responses: [{ success: false, error: 'testing failed' }],
150+
});
151+
});
152+
fcm.pushType = 'android';
153+
const data = { data: { alert: 'alert' } };
154+
const devices = [{ deviceToken: 'token', deviceType: 'apple' }];
155+
const response = await fcm.send(data, devices);
156+
expect(fcm.sender.sendEachForMulticast).toHaveBeenCalled();
157+
const args = fcm.sender.sendEachForMulticast.calls.first().args;
158+
expect(args.length).toEqual(1);
159+
expect(args[0].android.priority).toEqual('high');
160+
expect(args[0].android.data.data).toEqual('{"alert":"alert"}');
161+
expect(args[0].tokens).toEqual(['token']);
162+
expect(spyInfo).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'sending push to 1 devices');
163+
expect(spyError.calls.all()[0].args).toEqual(['parse-server-push-adapter FCM', 'failed to send to token with error: "testing failed"']);
164+
expect(spyError.calls.all()[1].args).toEqual(['parse-server-push-adapter FCM', 'tokens with failed pushes: ["token"]']);
165+
expect(response).toEqual([[{
166+
device: { deviceToken: 'token', deviceType: 'apple' },
167+
response: { error: 'testing failed'},
168+
transmitted: false,
169+
}]]);
170+
});
171+
172+
it('can handle FCM request error', async () => {
173+
const spyInfo = spyOn(log, 'info').and.callFake(() => {});
174+
const spyError = spyOn(log, 'error').and.callFake(() => {});
175+
const fcm = new FCM(testArgs);
176+
spyOn(fcm.sender, 'sendEachForMulticast').and.callFake(() => {
177+
return Promise.reject('testing error abort');
178+
});
179+
fcm.pushType = 'android';
180+
const data = { data: { alert: 'alert' } };
181+
const devices = [{ deviceToken: 'token' }];
182+
await fcm.send(data, devices);
183+
expect(fcm.sender.sendEachForMulticast).toHaveBeenCalled();
184+
expect(spyInfo).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'sending push to 1 devices');
185+
expect(spyError).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'error sending push: testing error abort');
186+
});
187+
188+
it('FCM request invalid push type', async () => {
189+
const fcm = new FCM(testArgs);
190+
fcm.pushType = 'invalid';
191+
const data = { data: { alert: 'alert' } };
192+
const devices = [{ deviceToken: 'token' }];
193+
try {
194+
await fcm.send(data, devices);
195+
expect(true).toBe(false);
196+
} catch (e) {
197+
expect(e.message).toBe('Unsupported push type, apple or android only.');
198+
}
199+
});
200+
19201
it('can use a raw FCM payload', () => {
20202
// If the payload is wrapped inside a key named 'rawPayload', a user can use the raw FCM payload structure
21203
// See: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages

spec/WEB.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ describe('WEB', () => {
5555
expect(function() { new WEB(undefined); }).toThrow();
5656
});
5757

58-
it('does log on invalid APNS notification', async () => {
58+
it('does log on invalid payload', async () => {
5959
const spy = spyOn(log, 'warn');
6060
const web = new WEB({ vapidDetails });
6161
web.send();
62-
expect(spy).toHaveBeenCalled();
62+
expect(spy).toHaveBeenCalledWith('parse-server-push-adapter WEB', 'invalid push payload');
6363
});
6464

6565
it('can send successful WEB request', async () => {

0 commit comments

Comments
 (0)