-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathwrapperAdapter.spec.ts
More file actions
157 lines (134 loc) · 4.68 KB
/
wrapperAdapter.spec.ts
File metadata and controls
157 lines (134 loc) · 4.68 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
// @ts-nocheck
import { loggerMock } from '../../../logger/__tests__/sdkLogger.mock';
import thenable from '../../../utils/promise/thenable';
import { LOG_PREFIX } from '../constants';
import { SplitError } from '../../../utils/lang/errors';
/** Mocks */
import { wrapperMock } from './wrapper.mock';
function throwsException() {
throw new Error('some error');
}
function rejectedPromise() {
return Promise.reject('some error');
}
function invalidThenable() {
return { then() { } };
}
export const wrapperWithIssues = {
get: undefined,
set: 'invalid value',
del: throwsException,
getKeysByPrefix: rejectedPromise,
incr: invalidThenable,
decr: 'invalid value',
getMany: throwsException,
connect: rejectedPromise,
close: invalidThenable,
getAndSet: 'invalid value',
getByPrefix: throwsException,
pushItems: rejectedPromise,
popItems: invalidThenable,
getItemsCount: 'invalid value',
itemContains: throwsException,
};
export const wrapperWithValuesToSanitize = {
get: () => Promise.resolve(10),
set: () => Promise.resolve('tRue'),
del: () => Promise.resolve(), // no result
getKeysByPrefix: () => Promise.resolve(['1', null, false, true, '2', null]),
incr: () => Promise.resolve('1'),
decr: () => Promise.resolve('0'),
getMany: () => Promise.resolve(['1', null, false, true, '2', null]),
connect: () => Promise.resolve(1),
getAndSet: () => Promise.resolve(true),
getByPrefix: () => Promise.resolve(['1', null, false, true, '2', null]),
popItems: () => Promise.resolve('invalid array'),
getItemsCount: () => Promise.resolve('10'),
itemContains: () => Promise.resolve('true'),
};
const SANITIZED_RESULTS = {
get: '10',
set: true,
del: false,
getKeysByPrefix: ['1', '2'],
incr: false,
decr: false,
getMany: ['1', null, '2', null],
connect: false,
getAndSet: 'true',
getByPrefix: ['1', '2'],
popItems: [],
getItemsCount: 10,
itemContains: true,
};
const VALID_METHOD_CALLS = {
'get': ['some_key'],
'set': ['some_key', 'some_value'],
'del': ['some_key'],
'getKeysByPrefix': ['some_prefix'],
'incr': ['some_key'],
'decr': ['some_key'],
'getMany': [['some_key_1', 'some_key_2']],
'connect': [],
'close': [],
'getAndSet': ['some_key', 'some_value'],
'getByPrefix': ['some_prefix'],
'pushItems': ['some_key_list', ['item1', 'item2']],
'popItems': ['some_key'],
'getItemsCount': ['some_key'],
'itemContains': ['some_key', 'some_value'],
};
// Test target
import { wrapperAdapter } from '../wrapperAdapter';
describe('Wrapper Adapter', () => {
afterEach(() => {
loggerMock.mockClear();
wrapperMock.mockClear();
});
test('calls original wrapper methods', async () => {
const instance = wrapperAdapter(loggerMock, wrapperMock);
const methods = Object.keys(VALID_METHOD_CALLS);
for (let i = 0; i < methods.length; i++) {
const method = methods[i];
const result = instance[method](...VALID_METHOD_CALLS[method]);
expect(wrapperMock[method]).toBeCalledTimes(1);
expect(wrapperMock[method]).toBeCalledWith(...VALID_METHOD_CALLS[method]);
expect(wrapperMock[method]).toHaveReturnedWith(result);
await result;
}
// no logs if there isn't issues
expect(loggerMock.error).not.toBeCalled();
expect(loggerMock.warn).not.toBeCalled();
});
test('handle wrapper call exceptions', async () => {
const instance = wrapperAdapter(loggerMock, wrapperWithIssues);
const methods = Object.keys(VALID_METHOD_CALLS);
for (let i = 0; i < methods.length; i++) {
const method = methods[i];
const result = instance[method](...VALID_METHOD_CALLS[method]);
expect(thenable(result)).toBe(true);
try {
await result;
expect(true).toBe(false); // promise shouldn't be resolved
} catch (e) {
expect(e).toBeInstanceOf(SplitError);
expect(loggerMock.error).toHaveBeenCalledWith(`${LOG_PREFIX} Wrapper '${method}' operation threw an error. Message: ${e.message}`);
}
}
expect(loggerMock.error).toBeCalledTimes(methods.length);
});
test('sanitize wrapper call results', async () => {
const instance = wrapperAdapter(loggerMock, wrapperWithValuesToSanitize);
const methods = Object.keys(SANITIZED_RESULTS);
for (let i = 0; i < methods.length; i++) {
try {
const method = methods[i];
const result = await instance[method](...VALID_METHOD_CALLS[method]);
expect(result).toEqual(SANITIZED_RESULTS[method]); // result should be sanitized
} catch (e) {
expect(true).toBe(methods[i]); // promise shouldn't be rejected
}
}
expect(loggerMock.warn).toBeCalledTimes(methods.length); // one warning for each wrapper operation call which result had to be sanitized
});
});