forked from aws-amplify/amplify-js
-
Notifications
You must be signed in to change notification settings - Fork 2
/
AmazonPersonalizeProvider.test.ts
127 lines (108 loc) · 3.33 KB
/
AmazonPersonalizeProvider.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
126
127
/*
* Copyright 2019-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
import { Credentials } from '@aws-amplify/core';
import { AmazonPersonalizeProvider } from '../../src/Providers/AmazonPersonalizeProvider';
import {
PersonalizeEventsClient,
PutEventsCommand,
} from '@aws-sdk/client-personalize-events';
const credentials = {
accessKeyId: 'accessKeyId',
sessionToken: 'sessionToken',
secretAccessKey: 'secretAccessKey',
identityId: 'identityId',
authenticated: true,
};
const TRACKING_ID = 'trackingId';
jest.useFakeTimers();
jest.mock('@aws-sdk/client-personalize-events');
beforeEach(() => {
PersonalizeEventsClient.prototype.send = jest.fn(async command => {
if (command instanceof PutEventsCommand) {
return 'data';
}
});
});
afterEach(() => {
jest.restoreAllMocks();
});
describe('Personalize provider test', () => {
describe('getProviderName test', () => {
test('happy case', () => {
const analytics = new AmazonPersonalizeProvider();
expect(analytics.getProviderName()).toBe('AmazonPersonalize');
});
});
describe('configure test', () => {
test('happy case', () => {
const analytics = new AmazonPersonalizeProvider();
analytics.configure({ trackingId: TRACKING_ID });
expect(analytics.configure({ region: 'region1' })).toEqual({
flushInterval: 5000,
flushSize: 5,
region: 'region1',
trackingId: 'trackingId',
});
});
});
describe('record test', () => {
test('record without credentials', async () => {
const analytics = new AmazonPersonalizeProvider();
analytics.configure({ trackingId: TRACKING_ID });
const spyon = jest
.spyOn(Credentials, 'get')
.mockImplementationOnce(() => {
return Promise.reject('err');
});
expect(await analytics.record('params')).toBe(false);
expect(spyon).toHaveBeenCalledTimes(1);
spyon.mockClear();
});
test('record happy case with identify event', async () => {
const analytics = new AmazonPersonalizeProvider();
analytics.configure({ trackingId: TRACKING_ID });
const spyon = jest
.spyOn(Credentials, 'get')
.mockImplementationOnce(() => {
return Promise.resolve(credentials);
});
await analytics.record({
event: {
eventType: 'Identify',
properties: { userId: 'user1' },
},
config: {},
});
jest.advanceTimersByTime(6000);
spyon.mockClear();
});
test('record happy case with Click event', async () => {
const analytics = new AmazonPersonalizeProvider();
analytics.configure({ trackingId: TRACKING_ID });
const spyon = jest
.spyOn(Credentials, 'get')
.mockImplementationOnce(() => {
return Promise.resolve(credentials);
});
await analytics.record({
event: {
eventType: 'Click',
properties: { itemId: 'item1', eventValue: 'value1' },
},
config: {},
});
jest.advanceTimersByTime(6000);
spyon.mockClear();
});
});
});