Skip to content

Commit 2b3b4a2

Browse files
test(notification-service): added unit testcases (#26)
1 parent d58d08e commit 2b3b4a2

File tree

9 files changed

+2947
-1695
lines changed

9 files changed

+2947
-1695
lines changed

package-lock.json

Lines changed: 2069 additions & 1692 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
"eslint": "lb-eslint --report-unused-disable-directives .",
2323
"eslint:fix": "npm run eslint -- --fix",
2424
"pretest": "npm run clean && npm run build",
25-
"test": "echo \"No test specified\"",
25+
"test": "lb-mocha --allow-console-logs \"dist/__tests__\"",
2626
"posttest": "npm run lint",
2727
"test:dev": "lb-mocha --allow-console-logs dist/__tests__/**/*.js && npm run posttest",
2828
"clean": "lb-clean dist *.tsbuildinfo .eslintcache",
2929
"prepublishOnly": "npm run test && npm run lint",
30-
"prepare": "husky install"
30+
"prepare": "husky install",
31+
"coverage": "nyc npm run test"
3132
},
3233
"repository": {
3334
"type": "git",
@@ -49,7 +50,9 @@
4950
"@types/nodemailer": "^6.4.0",
5051
"firebase-admin": "^9.7.0",
5152
"nodemailer": "^6.6.0",
52-
"socket.io-client": "^2.4.0",
53+
"nyc": "^15.1.0",
54+
"proxyquire": "^2.1.3",
55+
"socket.io-client": "^4.0.2",
5356
"tslib": "^2.0.0"
5457
},
5558
"devDependencies": {
@@ -61,6 +64,7 @@
6164
"@semantic-release/changelog": "^5.0.1",
6265
"@semantic-release/git": "^9.0.0",
6366
"@types/node": "^10.17.59",
67+
"@types/proxyquire": "^1.3.28",
6468
"@types/pubnub": "^4.27.3",
6569
"@types/socket.io-client": "^1.4.33",
6670
"aws-sdk": "^2.683.0",

src/__tests__/mock-sdk.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import AWS from 'aws-sdk';
2+
import Pubnub from 'pubnub';
3+
import Mail = require('nodemailer/lib/mailer');
4+
import SMTPTransport = require('nodemailer/lib/smtp-transport');
5+
6+
export class MockSES {
7+
constructor(config: AWS.SES.Types.ClientConfiguration) {}
8+
9+
async sendEmail(emailReq: AWS.SES.SendEmailRequest) {}
10+
}
11+
12+
export class MockSNS {
13+
constructor(config: AWS.SNS.ClientConfiguration) {}
14+
15+
async publish(message: AWS.SNS.PublishInput) {}
16+
}
17+
18+
export class MockSocketIo {
19+
constructor(
20+
url: string,
21+
options?: {
22+
[key: string]: string;
23+
},
24+
) {}
25+
26+
async emit(path: string, message: string) {}
27+
}
28+
29+
export class MockMail {
30+
constructor(config: SMTPTransport.Options) {}
31+
32+
async sendMail(message: Mail.Options) {}
33+
}
34+
35+
export class MockPubnub {
36+
constructor(config: Pubnub.PubnubConfig) {}
37+
38+
grant(grantConfig: Pubnub.GrantParameters) {}
39+
async publish(publishConfig: Pubnub.PublishParameters) {}
40+
}
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
/* eslint-disable @typescript-eslint/no-shadow */
2+
import {expect} from '@loopback/testlab';
3+
import * as admin from 'firebase-admin';
4+
import {FcmMessage, FcmProvider} from '../../providers';
5+
6+
describe('FCM Service', () => {
7+
const app = admin.initializeApp();
8+
const fcmProvider = new FcmProvider(app);
9+
10+
describe('fcm configration addition', () => {
11+
it('returns error message when no firebase config', async () => {
12+
try {
13+
/* eslint-disable @typescript-eslint/no-unused-vars */
14+
const fcmProvider = new FcmProvider();
15+
} catch (err) {
16+
const result = err.message;
17+
expect(result).which.eql('Firebase Config missing !');
18+
}
19+
});
20+
21+
it('returns error message on passing reciever length as zero', async () => {
22+
const message: FcmMessage = {
23+
receiver: {
24+
to: [],
25+
},
26+
body: 'test',
27+
sentDate: new Date(),
28+
type: 0,
29+
options: {},
30+
};
31+
try {
32+
const result = fcmProvider.initialValidations(message);
33+
} catch (err) {
34+
expect(err.message).which.eql(
35+
'Message receiver, topic or condition not found in request !',
36+
);
37+
}
38+
});
39+
40+
it('returns error message on passing reciever length as zero in value function', async () => {
41+
const message: FcmMessage = {
42+
receiver: {
43+
to: [],
44+
},
45+
body: 'test',
46+
sentDate: new Date(),
47+
type: 0,
48+
options: {},
49+
};
50+
try {
51+
const result = fcmProvider.value().publish(message);
52+
} catch (err) {
53+
expect(err.message).which.eql(
54+
'Message receiver, topic or condition not found in request !',
55+
);
56+
}
57+
});
58+
59+
it('returns error message on having no message subject', async () => {
60+
const message: FcmMessage = {
61+
receiver: {
62+
to: [
63+
{
64+
id: 'dummy',
65+
type: 0,
66+
},
67+
],
68+
},
69+
body: 'test',
70+
sentDate: new Date(),
71+
type: 0,
72+
options: {},
73+
};
74+
try {
75+
const result = fcmProvider.initialValidations(message);
76+
} catch (err) {
77+
expect(err.message).which.eql('Message title not found !');
78+
}
79+
});
80+
81+
it('returns error message on having no message subject using value function', async () => {
82+
const message: FcmMessage = {
83+
receiver: {
84+
to: [
85+
{
86+
id: 'dummy',
87+
type: 0,
88+
},
89+
],
90+
},
91+
body: 'test',
92+
sentDate: new Date(),
93+
type: 0,
94+
options: {},
95+
};
96+
try {
97+
const result = fcmProvider.value().publish(message);
98+
} catch (err) {
99+
expect(err.message).which.eql('Message title not found !');
100+
}
101+
});
102+
103+
it('returns array for sending push to conditions', async () => {
104+
const message: FcmMessage = {
105+
receiver: {
106+
to: [
107+
{
108+
id: 'dummy',
109+
type: 0,
110+
},
111+
],
112+
},
113+
body: 'test',
114+
sentDate: new Date(),
115+
type: 0,
116+
options: {},
117+
subject: 'test',
118+
};
119+
120+
const generalMessageObj = {
121+
notification: {
122+
title: 'test',
123+
body: 'test',
124+
},
125+
};
126+
const result = fcmProvider.sendingPushToConditions(
127+
message,
128+
generalMessageObj,
129+
);
130+
expect(result).which.eql([]);
131+
}).timeout(5000);
132+
133+
it('returns array for sending push to receive tokens', async () => {
134+
const message: FcmMessage = {
135+
receiver: {
136+
to: [
137+
{
138+
id: 'dummy',
139+
type: 0,
140+
},
141+
],
142+
},
143+
body: 'test',
144+
sentDate: new Date(),
145+
type: 0,
146+
options: {},
147+
subject: 'test',
148+
};
149+
150+
const generalMessageObj = {
151+
notification: {
152+
title: 'test',
153+
body: 'test',
154+
},
155+
};
156+
const result = fcmProvider.sendingPushToReceiverTokens(
157+
message,
158+
generalMessageObj,
159+
);
160+
expect(result).to.have.Array();
161+
}).timeout(5000);
162+
163+
it('returns array for sending push to topics', async () => {
164+
const message: FcmMessage = {
165+
receiver: {
166+
to: [
167+
{
168+
id: 'dummy',
169+
type: 0,
170+
},
171+
],
172+
},
173+
body: 'test',
174+
sentDate: new Date(),
175+
type: 0,
176+
options: {},
177+
subject: 'test',
178+
};
179+
180+
const generalMessageObj = {
181+
notification: {
182+
title: 'test',
183+
body: 'test',
184+
},
185+
};
186+
const result = fcmProvider.sendingPushToTopics(
187+
message,
188+
generalMessageObj,
189+
);
190+
expect(result).which.eql([]);
191+
}).timeout(5000);
192+
193+
it('returns array for sending in value function', async () => {
194+
const message: FcmMessage = {
195+
receiver: {
196+
to: [
197+
{
198+
id: 'dummy',
199+
type: 0,
200+
},
201+
],
202+
},
203+
body: 'test',
204+
sentDate: new Date(),
205+
type: 0,
206+
options: {},
207+
subject: 'test',
208+
};
209+
const result = fcmProvider.value().publish(message);
210+
expect(result).to.have.Promise();
211+
}).timeout(5000);
212+
});
213+
});

0 commit comments

Comments
 (0)