-
Notifications
You must be signed in to change notification settings - Fork 26
/
mocks.ts
50 lines (45 loc) · 1.36 KB
/
mocks.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
import type {ExecutionContext} from '../api_types';
import type {FetchResponse} from '../api_types';
import type {Sync} from '../api_types';
import sinon from 'sinon';
import {v4} from 'uuid';
export interface MockExecutionContext extends ExecutionContext {
fetcher: {
fetch: sinon.SinonStub;
};
temporaryBlobStorage: {
storeUrl: sinon.SinonStub;
storeBlob: sinon.SinonStub;
};
}
export interface MockSyncExecutionContext extends MockExecutionContext {
readonly sync: Sync;
}
export function newMockSyncExecutionContext(overrides?: Partial<MockSyncExecutionContext>): MockSyncExecutionContext {
return {...newMockExecutionContext(), sync: {}, ...overrides};
}
export function newMockExecutionContext(overrides?: Partial<MockExecutionContext>): MockExecutionContext {
return {
invocationLocation: {
protocolAndHost: 'https://coda.io',
},
timezone: 'America/Los_Angeles',
invocationToken: v4(),
fetcher: {
fetch: sinon.stub(),
},
temporaryBlobStorage: {
storeUrl: sinon.stub(),
storeBlob: sinon.stub(),
},
...overrides,
};
}
export function newJsonFetchResponse<T>(
body: T,
status: number = 200,
headers?: {[header: string]: string | string[] | undefined},
): FetchResponse<T> {
const allHeaders = {'Content-Type': 'application/json', ...headers};
return {status, body, headers: allHeaders};
}