forked from aws-amplify/amplify-js
-
Notifications
You must be signed in to change notification settings - Fork 2
/
AWSKinesisProvider.test.ts
125 lines (101 loc) · 2.64 KB
/
AWSKinesisProvider.test.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
import { Credentials } from '@aws-amplify/core';
import KinesisProvider from '../../src/Providers/AWSKinesisProvider';
import { KinesisClient, PutRecordsCommand } from '@aws-sdk/client-kinesis';
jest.useFakeTimers();
const credentials = {
accessKeyId: 'accessKeyId',
sessionToken: 'sessionToken',
secretAccessKey: 'secretAccessKey',
identityId: 'identityId',
authenticated: true,
};
jest.mock('@aws-sdk/client-kinesis');
beforeEach(() => {
KinesisClient.prototype.send = jest.fn(async command => {
if (command instanceof PutRecordsCommand) {
return 'data';
}
});
});
afterEach(() => {
jest.restoreAllMocks();
});
describe('kinesis provider test', () => {
describe('getCategory test', () => {
test('happy case', () => {
const analytics = new KinesisProvider();
expect(analytics.getCategory()).toBe('Analytics');
});
});
describe('getProviderName test', () => {
test('happy case', () => {
const analytics = new KinesisProvider();
expect(analytics.getProviderName()).toBe('AWSKinesis');
});
});
describe('configure test', () => {
test('happy case', () => {
const analytics = new KinesisProvider();
expect(analytics.configure({ region: 'region1' })).toEqual({
bufferSize: 1000,
flushInterval: 5000,
flushSize: 100,
region: 'region1',
resendLimit: 5,
});
});
});
describe('record test', () => {
test('record without credentials', async () => {
const analytics = new KinesisProvider();
const spyon = jest
.spyOn(Credentials, 'get')
.mockImplementationOnce(() => {
return Promise.reject('err');
});
expect(await analytics.record('params')).toBe(false);
});
test('record happy case', async () => {
const analytics = new KinesisProvider();
const spyon = jest
.spyOn(Credentials, 'get')
.mockImplementationOnce(() => {
return Promise.resolve(credentials);
});
await analytics.record({
event: {
data: {
data: 'data',
},
streamName: 'stream',
},
config: {},
});
jest.advanceTimersByTime(6000);
});
describe('passing parameters to KinesisClient', () => {
test('happy case', async () => {
const config = {
region: 'region1',
endpoint: 'endpoint1',
};
const analytics = new KinesisProvider({ ...config });
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return Promise.resolve(credentials);
});
await analytics.record({
event: {
data: {
data: 'data',
},
streamName: 'stream',
},
config: {},
});
jest.advanceTimersByTime(6000);
expect(KinesisClient).toHaveBeenCalledWith(
expect.objectContaining(config)
);
});
});
});