-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRevealContainer.test.js
More file actions
201 lines (184 loc) · 5.32 KB
/
RevealContainer.test.js
File metadata and controls
201 lines (184 loc) · 5.32 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
/*
Copyright (c) 2022 Skyflow, Inc.
*/
import * as revealUtils from '../../src/core-utils/reveal';
import RevealContainer from '../../src/core/RevealContainer';
import RevealSkyflowElement from '../../src/core/RevealSkyflowElement';
import Skyflow from '../../src/core/Skyflow';
import SKYFLOW_ERROR_CODE from '../../src/utils/skyflow-error-code';
const testSkyflowClient = new Skyflow({
vaultID: '1234',
vaultURL: 'https://validurl.com',
getBearerToken: () => Promise.resolve('valid_auth_token'),
});
describe('test RevealContainer Class', () => {
let revealContainer;
beforeEach(() => {
revealContainer = new RevealContainer(testSkyflowClient);
});
it('test constructor', () => {
expect(revealContainer).toBeInstanceOf(RevealContainer);
});
it('test create method', () => {
const revealElement = revealContainer.create({
token: 'random_token',
});
expect(revealElement).toBeInstanceOf(RevealSkyflowElement);
});
it('test reveal method success', (done) => {
const revealSuccessValue = {
records: [
{
token: 'random_token',
value: 'test_value',
},
],
};
jest
.spyOn(revealUtils, 'fetchRecordsByTokenId')
.mockResolvedValue(revealSuccessValue);
const setValueMock = jest.fn();
const setErrorMock = jest.fn();
const revealElement = revealContainer.create({
token: 'random_token',
});
revealElement.setMethods(setValueMock, setErrorMock);
revealContainer
.reveal()
.then((res) => {
expect(res).toEqual({ success: [{ token: 'random_token' }] });
done();
})
.catch((err) => {
done(err);
});
});
it('test reveal method partial success', (done) => {
const setValueMock = jest.fn();
const setErrorMock = jest.fn();
const setValueMock2 = jest.fn();
const setErrorMock2 = jest.fn();
const revealElement1 = revealContainer.create({
token: 'random_token',
elementId: '123',
});
revealElement1.setMethods(setValueMock, setErrorMock);
const revealElement2 = revealContainer.create({
token: 'invalid_token',
elementId: '456',
});
revealElement2.setMethods(setValueMock2, setErrorMock2);
const revealFailedValue = {
records: [
{
token: 'random_token',
value: 'test_value',
valueType: 'STRING',
elementId: revealElement1.elementId,
},
],
errors: [
{
token: 'invalid_token',
error: {
code: 404,
description: 'Tokens not found for invalid_token',
},
},
],
};
jest
.spyOn(revealUtils, 'fetchRecordsByTokenId')
.mockRejectedValue(revealFailedValue);
revealContainer
.reveal()
.then((res) => {
done(res);
})
.catch((err) => {
try {
expect(setValueMock).toBeCalledTimes(1);
expect(setErrorMock).toBeCalledTimes(0);
expect(setErrorMock2).toHaveBeenCalledTimes(1);
expect(setValueMock2).toBeCalledTimes(0);
expect(err).toEqual({
success: [{ token: 'random_token', valueType: 'STRING' }],
errors: revealFailedValue.errors,
});
done();
} catch (error) {
done(error);
}
});
});
it('test reveal validations invalid skyflow client', (done) => {
const testRevealContainer = new RevealContainer(new Skyflow({}));
testRevealContainer.reveal().catch((error) => {
try {
expect(error.error).toEqual(SKYFLOW_ERROR_CODE.VAULTID_IS_REQUIRED);
done();
} catch (err) {
done(err);
}
});
});
it('test reveal validations invalid token in element', (done) => {
const revealElement = revealContainer.create({
token: '',
});
revealContainer.reveal().catch((error) => {
try {
expect(error.errors).toEqual([
{ ...SKYFLOW_ERROR_CODE.EMPTY_TOKEN_ID_REVEAL },
]);
done();
} catch (err) {
done(err);
}
});
});
it('test reveal method uses updated token when setToken is called', (done) => {
const initialToken = 'initial_token';
const updatedToken = 'updated_token';
const revealElement = revealContainer.create({
token: initialToken,
});
const revealSuccessValue = {
records: [
{
token: updatedToken,
value: 'revealed_value',
},
],
};
const fetchSpy = jest
.spyOn(revealUtils, 'fetchRecordsByTokenId')
.mockResolvedValue(revealSuccessValue);
const setValueMock = jest.fn();
const setErrorMock = jest.fn();
revealElement.setMethods(setValueMock, setErrorMock);
revealElement.setToken(updatedToken);
revealContainer
.reveal()
.then((res) => {
try {
expect(fetchSpy).toHaveBeenCalledWith(
expect.anything(),
expect.arrayContaining([
expect.objectContaining({
token: updatedToken,
elementId: revealElement.elementId,
}),
])
);
expect(res).toEqual({ success: [{ token: updatedToken }] });
done();
} catch (assertionError) {
done(assertionError);
}
})
.catch((err) => {
done(err);
});
});
});