-
Notifications
You must be signed in to change notification settings - Fork 200
/
alerts.spec.ts
177 lines (152 loc) · 4.61 KB
/
alerts.spec.ts
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { expect } from 'chai';
import * as options from '../../../../src/v2/options';
import * as alerts from '../../../../src/v2/providers/alerts';
import { FULL_ENDPOINT, FULL_OPTIONS } from '../helpers';
const ALERT_TYPE = 'new-alert-type';
const APPID = '123456789';
const ALERT_EVENT_FILTER = {
alerttype: ALERT_TYPE,
};
const ALERT_APP_EVENT_FILTER = {
alerttype: ALERT_TYPE,
appid: APPID,
};
describe('alerts', () => {
describe('onAlertPublished', () => {
it('should create the function without opts', () => {
const result = alerts.onAlertPublished(ALERT_TYPE, () => 42);
expect(result.__endpoint).to.deep.equal({
platform: 'gcfv2',
labels: {},
eventTrigger: {
eventType: alerts.eventType,
eventFilters: ALERT_EVENT_FILTER,
retry: false,
},
});
});
it('should create the function with opts', () => {
const result = alerts.onAlertPublished(
{
...FULL_OPTIONS,
alertType: ALERT_TYPE,
appId: APPID,
},
() => 42
);
expect(result.__endpoint).to.deep.equal({
...FULL_ENDPOINT,
eventTrigger: {
eventType: alerts.eventType,
eventFilters: ALERT_APP_EVENT_FILTER,
retry: false,
},
});
});
it('should have a .run method', () => {
const func = alerts.onAlertPublished(ALERT_TYPE, (event) => event);
const res = func.run('input' as any);
expect(res).to.equal('input');
});
});
describe('getEndpointAnnotation', () => {
beforeEach(() => {
process.env.GCLOUD_PROJECT = 'aProject';
});
afterEach(() => {
options.setGlobalOptions({});
delete process.env.GCLOUD_PROJECT;
});
it('should define the endpoint without appId and opts', () => {
expect(alerts.getEndpointAnnotation({}, ALERT_TYPE)).to.deep.equal({
platform: 'gcfv2',
labels: {},
eventTrigger: {
eventType: alerts.eventType,
eventFilters: ALERT_EVENT_FILTER,
retry: false,
},
});
});
it('should define a complex endpoint without appId', () => {
expect(
alerts.getEndpointAnnotation({ ...FULL_OPTIONS }, ALERT_TYPE)
).to.deep.equal({
...FULL_ENDPOINT,
eventTrigger: {
eventType: alerts.eventType,
eventFilters: ALERT_EVENT_FILTER,
retry: false,
},
});
});
it('should define a complex endpoint', () => {
expect(
alerts.getEndpointAnnotation({ ...FULL_OPTIONS }, ALERT_TYPE, APPID)
).to.deep.equal({
...FULL_ENDPOINT,
eventTrigger: {
eventType: alerts.eventType,
eventFilters: ALERT_APP_EVENT_FILTER,
retry: false,
},
});
});
it('should merge global & specific opts', () => {
options.setGlobalOptions({
concurrency: 20,
region: 'europe-west1',
minInstances: 1,
});
const specificOpts = {
region: 'us-west1',
minInstances: 3,
};
expect(
alerts.getEndpointAnnotation(specificOpts, ALERT_TYPE, APPID)
).to.deep.equal({
platform: 'gcfv2',
labels: {},
concurrency: 20,
region: ['us-west1'],
minInstances: 3,
eventTrigger: {
eventType: alerts.eventType,
eventFilters: ALERT_APP_EVENT_FILTER,
retry: false,
},
});
});
});
describe('getOptsAndAlertTypeAndApp', () => {
it('should parse a string', () => {
const [opts, alertType, appId] = alerts.getOptsAndAlertTypeAndApp(
ALERT_TYPE
);
expect(opts).to.deep.equal({});
expect(alertType).to.equal(ALERT_TYPE);
expect(appId).to.be.undefined;
});
it('should parse an options object without appId', () => {
const myOpts: alerts.FirebaseAlertOptions = {
alertType: ALERT_TYPE,
region: 'us-west1',
};
const [opts, alertType, appId] = alerts.getOptsAndAlertTypeAndApp(myOpts);
expect(opts).to.deep.equal({ region: 'us-west1' });
expect(alertType).to.equal(myOpts.alertType);
expect(appId).to.be.undefined;
});
it('should parse an options object with appId', () => {
const myOpts: alerts.FirebaseAlertOptions = {
alertType: ALERT_TYPE,
appId: APPID,
region: 'us-west1',
};
const [opts, alertType, appId] = alerts.getOptsAndAlertTypeAndApp(myOpts);
expect(opts).to.deep.equal({ region: 'us-west1' });
expect(alertType).to.equal(myOpts.alertType);
expect(appId).to.be.equal(myOpts.appId);
});
});
});