-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathUpdateUserOperationExecutor.test.ts
More file actions
182 lines (161 loc) · 5.78 KB
/
UpdateUserOperationExecutor.test.ts
File metadata and controls
182 lines (161 loc) · 5.78 KB
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
178
179
180
181
182
import { APP_ID, ONESIGNAL_ID } from '__test__/constants';
import { TestEnvironment } from '__test__/support/environment/TestEnvironment';
import { SomeOperation } from '__test__/support/helpers/executors';
import {
setUpdateUserError,
setUpdateUserResponse,
} from '__test__/support/helpers/requests';
import { updateIdentityModel } from '__test__/support/helpers/setup';
import { type MockInstance } from 'vitest';
import { OPERATION_NAME } from '../constants';
import { RebuildUserService } from '../modelRepo/RebuildUserService';
import { IdentityModelStore } from '../modelStores/IdentityModelStore';
import { PropertiesModelStore } from '../modelStores/PropertiesModelStore';
import { SubscriptionModelStore } from '../modelStores/SubscriptionModelStore';
import { NewRecordsState } from '../operationRepo/NewRecordsState';
import { SetPropertyOperation } from '../operations/SetPropertyOperation';
import { ExecutionResult } from '../types/operation';
import { UpdateUserOperationExecutor } from './UpdateUserOperationExecutor';
let identityModelStore: IdentityModelStore;
let propertiesModelStore: PropertiesModelStore;
let newRecordsState: NewRecordsState;
let buildUserService: RebuildUserService;
let subscriptionsModelStore: SubscriptionModelStore;
let getRebuildOpsSpy: MockInstance;
vi.mock('src/shared/libraries/Log');
describe('UpdateUserOperationExecutor', () => {
beforeAll(() => {
TestEnvironment.initialize();
});
beforeEach(() => {
identityModelStore = OneSignal._coreDirector.identityModelStore;
propertiesModelStore = OneSignal._coreDirector.propertiesModelStore;
newRecordsState = OneSignal._coreDirector.newRecordsState;
subscriptionsModelStore = OneSignal._coreDirector.subscriptionModelStore;
buildUserService = new RebuildUserService(
identityModelStore,
propertiesModelStore,
subscriptionsModelStore,
);
getRebuildOpsSpy = vi.spyOn(
buildUserService,
'getRebuildOperationsIfCurrentUser',
);
// Set up initial model state
updateIdentityModel('onesignal_id', ONESIGNAL_ID);
});
const getExecutor = () => {
return new UpdateUserOperationExecutor(
identityModelStore,
propertiesModelStore,
buildUserService,
newRecordsState,
);
};
test('should return correct operations (names)', () => {
const executor = getExecutor();
expect(executor.operations).toEqual([OPERATION_NAME.SET_PROPERTY]);
});
test('should validate operations', async () => {
const executor = getExecutor();
const someOp = new SomeOperation();
const ops = [someOp];
const result = executor.execute(ops);
await expect(() => result).rejects.toThrow(
`Unrecognized operation: ${ops[0]}`,
);
});
describe('SetPropertyOperation', () => {
beforeEach(() => {
setUpdateUserResponse();
});
test('should update property in properties model on success', async () => {
const executor = getExecutor();
const setPropertyOp = new SetPropertyOperation(
APP_ID,
ONESIGNAL_ID,
'language',
'fr',
);
const result = await executor.execute([setPropertyOp]);
expect(result.result).toBe(ExecutionResult.SUCCESS);
expect(propertiesModelStore.model.language).toBe('fr');
});
test('can set tags', async () => {
const executor = getExecutor();
const setPropertyOp = new SetPropertyOperation(
APP_ID,
ONESIGNAL_ID,
'tags',
{ tagA: 'valueA', tagB: 'valueB' },
);
const result = await executor.execute([setPropertyOp]);
expect(result.result).toBe(ExecutionResult.SUCCESS);
expect(propertiesModelStore.model.tags).toEqual({
tagA: 'valueA',
tagB: 'valueB',
});
});
});
describe('Error Handling', () => {
test('should handle network errors', async () => {
const executor = getExecutor();
const setTagOp = new SetPropertyOperation(APP_ID, ONESIGNAL_ID, 'tags', {
test_tag: 'test_value',
});
// Retryable error
setUpdateUserError({ status: 429, retryAfter: 10 });
const res1 = await executor.execute([setTagOp]);
expect(res1).toMatchObject({
result: ExecutionResult.FAIL_RETRY,
retryAfterSeconds: 10,
});
// Unauthorized error
setUpdateUserError({ status: 401, retryAfter: 15 });
const res2 = await executor.execute([setTagOp]);
expect(res2).toMatchObject({
result: ExecutionResult.FAIL_UNAUTHORIZED,
retryAfterSeconds: 15,
});
// Missing error without rebuild ops
setUpdateUserError({ status: 404, retryAfter: 5 });
getRebuildOpsSpy.mockReturnValueOnce(null);
const res3 = await executor.execute([setTagOp]);
expect(res3).toMatchObject({
result: ExecutionResult.FAIL_NORETRY,
});
// Missing error with rebuild ops
const res4 = await executor.execute([setTagOp]);
expect(res4).toMatchObject({
result: ExecutionResult.FAIL_RETRY,
retryAfterSeconds: 5,
operations: [
{
name: 'login-user',
appId: APP_ID,
onesignalId: ONESIGNAL_ID,
},
{
name: 'refresh-user',
appId: APP_ID,
onesignalId: ONESIGNAL_ID,
},
],
});
// Missing error in retry window
newRecordsState._add(ONESIGNAL_ID);
setUpdateUserError({ status: 404, retryAfter: 20 });
const res5 = await executor.execute([setTagOp]);
expect(res5).toMatchObject({
result: ExecutionResult.FAIL_RETRY,
retryAfterSeconds: 20,
});
// Other errors
setUpdateUserError({ status: 400 });
const res6 = await executor.execute([setTagOp]);
expect(res6).toMatchObject({
result: ExecutionResult.FAIL_NORETRY,
});
});
});
});