-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
138 lines (119 loc) · 4.18 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import EventEmitter from 'events';
import test from 'ava';
import {Observable} from 'rxjs';
import PMRObserver from '.';
const sinon = require('sinon');
const mockEvent = require('./mocks/event.json');
const mockUpdated = require('./mocks/eventUpdated');
const mockWithSummary = Object.assign({}, mockEvent, {location: '', summary: '@webex'});
const mockNoWebex = Object.assign({}, mockEvent, {location: '', summary: ''});
const mockWithCancellation = Object.assign({}, mockEvent, {status: 'cancelled'});
class TestEmitter extends EventEmitter {}
const mockEmitter = new TestEmitter();
const stubObservable = Observable.fromEvent(mockEmitter, 'CALENDAR_UPDATE').share();
let stubService;
let stubUpdate;
const expectedEvent = {
kind: 'calendar#event',
etag: '2940518526114000',
id: 'event-1',
status: 'confirmed',
htmlLink: 'https://www.google.com/calendar/event?eid=NWh1cmRtajY0a3VpbGIzbDM1MDZ1MjVqOHMgYnJoaW1AYXBpZGV2ZGVtby5jb20',
created: '2016-08-03T21:19:36.000Z',
updated: '2016-08-04T21:21:03.057Z',
summary: 'Testing',
description: 'meet up online\n==== WebEx Details: Do Not Touch ====\nhttp://cisco.webex.com/meet/brhim',
location: '@webex',
creator: {
email: '[email protected]'
},
organizer: {
email: '[email protected]'
},
start: {
dateTime: '2016-08-15T17:00:00-07:00'
},
end: {
dateTime: '2016-08-16T18:00:00-07:00'
},
iCalUID: '[email protected]',
sequence: 0,
attendees: [
{
email: '[email protected]',
responseStatus: 'needsAction'
},
{
email: '[email protected]',
responseStatus: 'needsAction'
},
{
email: '[email protected]',
responseStatus: 'needsAction'
},
{
email: '[email protected]',
organizer: true,
self: true,
responseStatus: 'accepted'
}
],
hangoutLink: 'https://plus.google.com/hangouts/_/apidevdemo.com/brhim-damyers2?hceid=YnJoaW1AYXBpZGV2ZGVtby5jb20.5hurdmj64kuilb3l3506u25j8s',
reminders: {
useDefault: true
},
colorId: 9,
calendarId: '[email protected]',
userId: 'brhim'
};
// Reassign the stubs so the calls counters are reset
test.beforeEach(() => {
stubUpdate = sinon.stub().resolves('success');
stubService = {
updateEvent: stubUpdate
};
});
test.serial('Update on relevant event', t => {
const observer = new PMRObserver(stubObservable, stubService);
observer.init();
mockEmitter.emit('CALENDAR_UPDATE', mockEvent);
t.truthy(stubUpdate.calledWith({calendarId: '[email protected]', eventId: 'event-1'}, expectedEvent));
});
test.serial('Don\'t update event without webex field', t => {
const observer = new PMRObserver(stubObservable, stubService);
observer.init();
mockEmitter.emit('CALENDAR_UPDATE', mockNoWebex);
t.falsy(stubUpdate.called);
});
test.serial('Don\'t process event it is not a newly created', t => {
const observer = new PMRObserver(stubObservable, stubService);
observer.init();
mockEmitter.emit('CALENDAR_UPDATE', mockWithCancellation);
t.falsy(stubUpdate.called);
});
test.serial('Don\'t process event if already updated', t => {
const observer = new PMRObserver(stubObservable, stubService);
observer.init();
mockEmitter.emit('CALENDAR_UPDATE', mockUpdated);
t.falsy(stubUpdate.called);
});
test.serial('Throw error for not observable or stub service', t => {
const observerError = t.throws(() => {
// eslint-disable-next-line no-unused-vars
const test = new PMRObserver(null);
});
t.is(observerError.message, 'An observable is expected, in order to subscribe to calendars');
const serviceError = t.throws(() => {
// eslint-disable-next-line no-unused-vars
const test = new PMRObserver(stubObservable, null);
});
t.is(serviceError.message, 'A calendarService is expected');
});
test.serial('Update based on options', t => {
const observer = new PMRObserver(stubObservable, stubService, {field: 'summary', cmrDomain: 'test'});
observer.init();
mockEmitter.emit('CALENDAR_UPDATE', mockWithSummary);
const description = `meet up online\n==== WebEx Details: Do Not Touch ====\nhttp://test.webex.com/meet/brhim`;
const updatedEvent = Object.assign({}, expectedEvent, {description, summary: '@webex', location: ''});
t.truthy(stubUpdate.calledWith({calendarId: '[email protected]', eventId: 'event-1'}, updatedEvent));
});