forked from webex/widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.ts
More file actions
259 lines (217 loc) · 8.64 KB
/
Copy pathstore.ts
File metadata and controls
259 lines (217 loc) · 8.64 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import {makeAutoObservable} from 'mobx';
import Webex from '@webex/contact-center';
import store from '../src/store'; // Adjust the import path as necessary
import {IStore} from '../src/store.types';
import {mockProfile} from '@webex/test-fixtures';
let mockShouldCallback = true;
let webexInitSpy;
console.log = jest.fn(); // Mock console.log
jest.mock('mobx', () => ({
makeAutoObservable: jest.fn(),
observable: {ref: jest.fn()},
}));
jest.mock('@webex/contact-center', () => ({
init: jest.fn(() => ({
once: jest.fn((event, callback) => {
if (event === 'ready' && mockShouldCallback) {
callback();
}
}),
cc: {
register: jest.fn().mockResolvedValue(mockProfile),
LoggerProxy: {
error: jest.fn(),
log: jest.fn(),
warn: jest.fn(),
info: jest.fn(),
trace: jest.fn(),
},
},
})),
}));
describe('Store', () => {
let mockWebex;
let storeInstance: IStore;
beforeEach(() => {
// Reset store values before each test since store is a singleton
storeInstance = store.getInstance();
//@ts-expect-error To be fixed in SDK - https://jira-eng-sjc12.cisco.com/jira/browse/CAI-6762
mockWebex = Webex.init({
config: {anyConfig: true},
credentials: {
access_token: 'fake_token',
},
});
//@ts-expect-error To be fixed in SDK - https://jira-eng-sjc12.cisco.com/jira/browse/CAI-6762
webexInitSpy = jest.spyOn(Webex, 'init');
jest.useFakeTimers(); // Use fake timers for testing setTimeout
});
afterEach(() => {
jest.useRealTimers(); // Restore real timers after each test
});
it('should initialize with default values', () => {
expect(storeInstance.teams).toEqual([]);
expect(storeInstance.loginOptions).toEqual([]);
expect(storeInstance.idleCodes).toEqual([]);
expect(storeInstance.agentId).toBe('');
expect(storeInstance.dataCenter).toBe('');
expect(storeInstance.wrapupCodes).toEqual([]);
expect(storeInstance.currentTask).toBeNull();
expect(storeInstance.isAgentLoggedIn).toBe(false);
expect(storeInstance.deviceType).toBe('');
expect(storeInstance.taskList).toEqual({});
expect(storeInstance.agentProfile).toEqual({});
expect(makeAutoObservable).toHaveBeenCalledWith(storeInstance, {
cc: expect.any(Function),
});
});
describe('registerCC', () => {
it('should initialise store values on successful register', async () => {
const mockAgentName = 'John Doe';
const date = new Date().getTime();
const mockResponse = {
teams: [{id: 'team1', name: 'Team 1'}],
loginVoiceOptions: ['option1', 'option2'],
idleCodes: [{id: 'code1', name: 'Code 1', isSystem: false, isDefault: false}],
agentId: 'agent1',
isAgentLoggedIn: true,
deviceType: 'BROWSER',
dialNumber: '12345',
lastStateAuxCodeId: 'auxCodeId',
lastStateChangeTimestamp: date,
agentName: mockAgentName,
environment: 'produs1',
isTimeoutDesktopInactivityEnabled: true,
timeoutDesktopInactivityMins: 15,
};
mockWebex.cc.register.mockResolvedValue(mockResponse);
await storeInstance.registerCC(mockWebex);
expect(storeInstance.teams).toEqual(mockResponse.teams);
expect(storeInstance.loginOptions).toEqual(mockResponse.loginVoiceOptions);
expect(storeInstance.idleCodes).toEqual(mockResponse.idleCodes);
expect(storeInstance.agentId).toEqual(mockResponse.agentId);
expect(storeInstance.isAgentLoggedIn).toEqual(mockResponse.isAgentLoggedIn);
expect(storeInstance.deviceType).toEqual(mockResponse.deviceType);
expect(storeInstance.currentState).toEqual(mockResponse.lastStateAuxCodeId);
expect(storeInstance.lastStateChangeTimestamp).toEqual(date);
expect(storeInstance.agentProfile).toEqual({
agentName: mockAgentName,
isTimeoutDesktopInactivityEnabled: true,
timeoutDesktopInactivityMins: 15,
});
expect(storeInstance.dataCenter).toEqual(mockResponse.environment);
});
it('should log an error on failed register', async () => {
const mockError = new Error('Register failed');
mockWebex.cc.register.mockRejectedValue(mockError);
try {
await storeInstance.registerCC(mockWebex);
} catch (error) {
expect(error).toEqual(mockError);
expect(storeInstance.logger.error).toHaveBeenCalledWith(
'CC-Widgets: Contact-center registerCC(): failed - Error: Register failed',
{
method: 'registerCC',
module: 'cc-store#store.ts',
}
);
}
});
it('should throw error if webex and cc object are not present', async () => {
try {
storeInstance.cc = undefined;
await storeInstance.registerCC(undefined);
} catch (error) {
expect(error.message).toEqual('Webex SDK not initialized');
}
});
});
describe('init', () => {
it('should call eventListenerCallback ', async () => {
const eventListenerCallback = jest.fn();
const initParams = {webex: mockWebex};
jest.spyOn(storeInstance, 'registerCC').mockResolvedValue();
webexInitSpy.mockClear();
await storeInstance.init(initParams, eventListenerCallback);
expect(eventListenerCallback).toHaveBeenCalled();
expect(storeInstance.registerCC).toHaveBeenCalledWith(mockWebex);
expect(webexInitSpy).not.toHaveBeenCalled();
});
it('should call registerCC if webex is in options', async () => {
const initParams = {webex: mockWebex};
jest.spyOn(storeInstance, 'registerCC').mockResolvedValue();
webexInitSpy.mockClear();
await storeInstance.init(initParams, jest.fn());
expect(storeInstance.registerCC).toHaveBeenCalledWith(mockWebex);
expect(webexInitSpy).not.toHaveBeenCalled();
});
it('should initialize webex and call registerCC on ready event', async () => {
//@ts-expect-error To be fixed in SDK - https://jira-eng-sjc12.cisco.com/jira/browse/CAI-6762
webexInitSpy = jest.spyOn(Webex, 'init').mockReturnValue(mockWebex);
jest.spyOn(storeInstance, 'registerCC').mockClear();
const initParams = {
webexConfig: {anyConfig: true},
access_token: 'fake_token',
};
await storeInstance.init(initParams, jest.fn());
expect(webexInitSpy).toHaveBeenCalledWith({
config: initParams.webexConfig,
credentials: {access_token: initParams.access_token},
});
expect(storeInstance.registerCC).toHaveBeenCalledWith(mockWebex);
});
it('should log an error and reject the promise if registerCC fails in init method', async () => {
const initParams = {
webexConfig: {anyConfig: true},
access_token: 'fake_token',
};
const error = new Error('registerCC failed');
jest.spyOn(storeInstance, 'registerCC').mockRejectedValue(error);
// Provide a logger so the init() error handler can log without failing
storeInstance.logger = {
error: jest.fn(),
log: jest.fn(),
warn: jest.fn(),
info: jest.fn(),
trace: jest.fn(),
};
await expect(storeInstance.init(initParams, jest.fn())).rejects.toThrow('registerCC failed');
expect(storeInstance.logger.error).toHaveBeenCalledWith(
'CC-Widgets: Store init(): registration failed - Error: registerCC failed',
{
module: 'cc-store#store.ts',
method: 'init',
}
);
});
it('should reject the promise if Webex SDK fails to initialize', async () => {
const initParams = {
webexConfig: {anyConfig: true},
access_token: 'fake_token',
};
mockShouldCallback = false;
jest.spyOn(storeInstance, 'registerCC').mockResolvedValue();
const initPromise = storeInstance.init(initParams, jest.fn());
jest.runAllTimers(); // Fast-forward the timers to simulate timeout
await expect(initPromise).rejects.toThrow('Webex SDK failed to initialize');
});
it('should clear timeout and reject if Webex.init throws synchronously', async () => {
const initParams = {
webexConfig: {anyConfig: true},
access_token: 'fake_token',
};
const syncError = new Error('sync init error');
// @ts-expect-error overriding mock implementation for this test
const initSpy = jest.spyOn(Webex, 'init').mockImplementation(() => {
throw syncError;
});
await expect(storeInstance.init(initParams, jest.fn())).rejects.toThrow('sync init error');
expect(initSpy).toHaveBeenCalledWith({
config: initParams.webexConfig,
credentials: {
access_token: initParams.access_token,
},
});
});
});
});