Skip to content

Commit e2178d5

Browse files
committed
Unit test for trigger
1 parent 8b64cda commit e2178d5

File tree

3 files changed

+159
-4
lines changed

3 files changed

+159
-4
lines changed

lib/triggers/contacts.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ function processAction(msg, cfg, snapshot) {
5858
console.log('Found %s contacts', values.length);
5959
if (values.length > 0) {
6060
values.forEach((elem)=> {
61-
const messageBody = _.omitBy(elem, (value, key) => key.startsWith('@odata.'));
62-
messageBody.calendarId = cfg.calendarId;
63-
console.log('MessageBody: ' + JSON.stringify(messageBody));
64-
this.emit('data', messages.newMessageWithBody(messageBody));
61+
const messageBody = _.omitBy(elem, (value, key) => key.startsWith('@odata.'));
62+
messageBody.calendarId = cfg.calendarId;
63+
console.log('MessageBody: ' + JSON.stringify(messageBody));
64+
this.emit('data', messages.newMessageWithBody(messageBody));
6565
});
6666
let lmdate = new Date(values[values.length - 1].lastModifiedDateTime);
6767
// The output value has always 0 milliseconds

spec/data/getContact_test.out.json

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
[
2+
{
3+
"@odata.etag": "W/\"EQAAABYAAAAPk/xXi/m8SpOJXtHI8EVyAAAAADMd\"",
4+
"otherAddress": {},
5+
"businessAddress": {},
6+
"homeAddress": {},
7+
"emailAddresses": [],
8+
"children": [],
9+
"personalNotes": null,
10+
"spouseName": null,
11+
"businessPhones": [],
12+
"mobilePhone": null,
13+
"homePhones": [],
14+
"manager": null,
15+
"assistantName": null,
16+
"businessHomePage": null,
17+
"profession": null,
18+
"officeLocation": null,
19+
"department": null,
20+
"companyName": null,
21+
"jobTitle": null,
22+
"imAddresses": [],
23+
"generation": null,
24+
"yomiCompanyName": null,
25+
"yomiSurname": null,
26+
"yomiGivenName": null,
27+
"title": null,
28+
"surname": "LastName",
29+
"nickName": null,
30+
"middleName": null,
31+
"initials": null,
32+
"givenName": "Contact1",
33+
"displayName": "Contact1 LastName",
34+
"fileAs": "LastName, Contact1",
35+
"birthday": null,
36+
"parentFolderId": "AQMkADAwATMwMAItY2ZmADktNjM4OAAtMDACLTAwCgAuAAADsOSavd0-LEaQVsombMCNegEAO2Hd7H7x9USxohZwL5iAFQAAAgEOAAAA",
37+
"categories": [],
38+
"changeKey": "EQAAABYAAAA7Yd3sfvH1RLGiFnAvmIAVAACGnyqI",
39+
"lastModifiedDateTime": "2017-03-13T14:27:23Z",
40+
"createdDateTime": "2017-03-13T14:17:05Z",
41+
"id": "AQMkADAwATMwMAItY2ZmADktNjM4OAAtMDACLTAwCgBGAAADsOSavd0-LEaQVsombMCNegcAO2Hd7H7x9USxohZwL5iAFQAAAgEOAAAAO2Hd7H7x9USxohZwL5iAFQAAAIaR-vMAAAA="
42+
},
43+
{
44+
"@odata.etag": "W/\"EQAAABYAAAAPk/xXi/m8SpOJXtHI8EVyAAAAADMe\"",
45+
"otherAddress": {},
46+
"businessAddress": {},
47+
"homeAddress": {},
48+
"emailAddresses": [],
49+
"children": [],
50+
"personalNotes": null,
51+
"spouseName": null,
52+
"businessPhones": [],
53+
"mobilePhone": null,
54+
"homePhones": [],
55+
"manager": null,
56+
"assistantName": null,
57+
"businessHomePage": null,
58+
"profession": null,
59+
"officeLocation": null,
60+
"department": null,
61+
"companyName": null,
62+
"jobTitle": null,
63+
"imAddresses": [],
64+
"generation": null,
65+
"yomiCompanyName": null,
66+
"yomiSurname": null,
67+
"yomiGivenName": null,
68+
"title": null,
69+
"surname": "LastName2",
70+
"nickName": null,
71+
"middleName": null,
72+
"initials": null,
73+
"givenName": "contact2",
74+
"displayName": "contact2 LastName2",
75+
"fileAs": "LastName2, contact2",
76+
"birthday": null,
77+
"parentFolderId": "AQMkADAwATMwMAItY2ZmADktNjM4OAAtMDACLTAwCgAuAAADsOSavd0-LEaQVsombMCNegEAO2Hd7H7x9USxohZwL5iAFQAAAgEOAAAA",
78+
"categories": [],
79+
"changeKey": "EQAAABYAAAA7Yd3sfvH1RLGiFnAvmIAVAACGnyqK",
80+
"lastModifiedDateTime": "2017-03-13T15:22:32Z",
81+
"createdDateTime": "2017-03-13T15:16:17Z",
82+
"id": "AQMkADAwATMwMAItY2ZmADktNjM4OAAtMDACLTAwCgBGAAADsOSavd0-LEaQVsombMCNegcAO2Hd7H7x9USxohZwL5iAFQAAAgEOAAAAO2Hd7H7x9USxohZwL5iAFQAAAIaR-vQAAAA="
83+
}
84+
]

spec/triggers/getContacts.spec.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'use strict';
2+
3+
describe('Outlook Contacts', function test() {
4+
5+
const nock = require('nock');
6+
const _ = require('lodash');
7+
const trigger = require('../../lib/triggers/contacts');
8+
9+
const cfg = require('../data/configuration.in.json');
10+
const jsonOut = {
11+
value: require('../data/getContact_test.out.json')
12+
};
13+
14+
const refreshTokenUri = 'https://login.microsoftonline.com';
15+
const refreshTokenApi = '/common/oauth2/v2.0/token';
16+
const microsoftGraphUri = 'https://graph.microsoft.com/v1.0';
17+
const microsoftGraphApi = '/me/contacts';
18+
19+
20+
var self;
21+
beforeEach(function createSpy() {
22+
self = jasmine.createSpyObj('self', ['emit']);
23+
});
24+
25+
it('should emit data without @oauth property - case: http 200', done => {
26+
const scope1 = nock(refreshTokenUri).post(refreshTokenApi)
27+
.reply(200, {
28+
access_token: 1
29+
});
30+
31+
const scope2 = nock(microsoftGraphUri).get(microsoftGraphApi)
32+
.query({
33+
$orderby: 'lastModifiedDateTime asc',
34+
$top: 900,
35+
$filter: 'lastModifiedDateTime gt 1970-01-01T00:00:00.000Z'
36+
})
37+
.reply(200, jsonOut);
38+
39+
function checkResults() {
40+
let calls = self.emit.calls;
41+
expect(calls.count()).toEqual(4);
42+
expect(calls.argsFor(0)[0]).toEqual('updateKeys');
43+
expect(calls.argsFor(1)[0]).toEqual('data');
44+
const [firstEvent, firstEmitData] = calls.argsFor(1);
45+
expect(firstEvent).toBe('data');
46+
console.dir(firstEmitData);
47+
expect(firstEmitData.body).toEqual(Object.assign(
48+
_.omit(jsonOut.value[0], '@odata.etag'),
49+
{
50+
calendarId: cfg.calendarId
51+
}
52+
));
53+
const [secondEvent, secondEmitData] = calls.argsFor(2);
54+
expect(secondEvent).toBe('data');
55+
expect(secondEmitData.body).toEqual(Object.assign(
56+
_.omit(jsonOut.value[1], '@odata.etag'),
57+
{
58+
calendarId: cfg.calendarId
59+
}
60+
));
61+
expect(scope1.isDone()).toBeTruthy();
62+
expect(scope2.isDone()).toBeTruthy();
63+
}
64+
65+
trigger.process.call(self, {}, cfg, {})
66+
.then(checkResults)
67+
.then(done)
68+
.catch(done.fail);
69+
});
70+
71+
});

0 commit comments

Comments
 (0)