-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathscheduled.spec.ts
51 lines (46 loc) · 1.45 KB
/
scheduled.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
import * as sinon from 'sinon';
import * as functions from 'firebase-functions/v1';
import fft = require('../../src/index');
import { WrappedScheduledFunction } from '../../src/main';
describe('providers/scheduled', () => {
const fakeFn = sinon.fake.resolves();
const scheduledFunc = functions.pubsub
.schedule('every 2 hours')
.onRun(fakeFn);
const emptyObjectMatcher = sinon.match(
(v) => sinon.match.object.test(v) && Object.keys(v).length === 0
);
afterEach(() => {
fakeFn.resetHistory();
});
it('should run the wrapped function with generated context', async () => {
const test = fft();
const fn: WrappedScheduledFunction = test.wrap(scheduledFunc);
await fn();
// Function should only be called with 1 argument
sinon.assert.calledOnce(fakeFn);
sinon.assert.calledWithExactly(
fakeFn,
sinon.match({
eventType: sinon.match.string,
timestamp: sinon.match.string,
params: emptyObjectMatcher,
})
);
});
it('should run the wrapped function with provided context', async () => {
const timestamp = new Date().toISOString();
const test = fft();
const fn: WrappedScheduledFunction = test.wrap(scheduledFunc);
await fn({ timestamp });
sinon.assert.calledOnce(fakeFn);
sinon.assert.calledWithExactly(
fakeFn,
sinon.match({
eventType: sinon.match.string,
timestamp,
params: emptyObjectMatcher,
})
);
});
});