@evervault/react-native 2.3.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/Input.tsx CHANGED
@@ -8,11 +8,13 @@ import {
8
8
  useCallback,
9
9
  useContext,
10
10
  useImperativeHandle,
11
+ useMemo,
11
12
  useRef,
13
+ useState,
12
14
  } from "react";
13
15
  import { TextInput, TextInputProps } from "react-native";
14
16
  import { mergeRefs } from "./utils";
15
- import { Controller, useFormContext } from "react-hook-form";
17
+ import { Controller, useController, useFormContext } from "react-hook-form";
16
18
  import MaskInput, { Mask, MaskArray } from "react-native-mask-input";
17
19
 
18
20
  export interface EvervaultInputContextValue {
@@ -82,73 +84,97 @@ export type BaseEvervaultInputProps = Omit<
82
84
  >;
83
85
 
84
86
  export function mask(format: string): MaskArray {
85
- return format.split("").map((char) => {
87
+ const maskArray: MaskArray = [];
88
+
89
+ let isObfuscated = false;
90
+ format.split("").forEach((char) => {
91
+ if (char === "[") {
92
+ isObfuscated = true;
93
+ return;
94
+ } else if (char === "]") {
95
+ isObfuscated = false;
96
+ return;
97
+ }
98
+
99
+ let value: string | RegExp | [RegExp] = char;
86
100
  if (char === "9") {
87
- return /\d/;
101
+ value = isObfuscated ? [/\d/] : /\d/;
88
102
  }
89
- return char;
103
+ maskArray.push(value);
90
104
  });
105
+
106
+ return maskArray;
91
107
  }
92
108
 
93
109
  export interface EvervaultInputProps<Values extends Record<string, unknown>>
94
110
  extends BaseEvervaultInputProps {
95
111
  name: keyof Values;
96
112
  mask?: Mask;
113
+ obfuscateValue?: boolean | string;
97
114
  }
98
115
 
99
116
  export const EvervaultInput = forwardRef<
100
117
  EvervaultInput,
101
118
  EvervaultInputProps<Record<string, unknown>>
102
- >(function EvervaultInput({ name, mask, ...props }, ref) {
119
+ >(function EvervaultInput({ name, mask, obfuscateValue, ...props }, ref) {
103
120
  const { validationMode } = useContext(EvervaultInputContext);
104
121
 
105
122
  const inputRef = useForwardedInputRef(ref);
106
123
 
107
124
  const methods = useFormContext();
108
125
 
126
+ const { field, fieldState } = useController({
127
+ control: methods.control,
128
+ name,
129
+ shouldUnregister: true,
130
+ });
131
+
132
+ const obfuscationCharacter = useMemo(() => {
133
+ if (typeof obfuscateValue === "string") {
134
+ return obfuscateValue;
135
+ } else {
136
+ return "•";
137
+ }
138
+ }, [obfuscateValue]);
139
+
109
140
  return (
110
- <Controller
111
- control={methods.control}
112
- name={name}
113
- shouldUnregister
114
- render={({ field, fieldState }) => (
115
- <MaskInput
116
- // Overridable props
117
- id={field.name}
118
- {...props}
119
- // Strict props
120
- ref={mergeRefs(inputRef, field.ref)}
121
- editable={!field.disabled && (props.editable ?? true)}
122
- onBlur={(evt) => {
123
- const shouldValidate =
124
- validationMode === "onBlur" ||
125
- validationMode === "onTouched" ||
126
- validationMode === "all";
127
- methods.setValue(field.name, field.value, {
128
- shouldDirty: true,
129
- shouldTouch: true,
130
- shouldValidate,
131
- });
132
- props.onBlur?.(evt);
133
- }}
134
- mask={mask}
135
- maskAutoComplete={!!mask}
136
- value={field.value}
137
- onChangeText={(masked, unmasked) => {
138
- const shouldValidate =
139
- (validationMode === "onTouched" && fieldState.isTouched) ||
140
- ((validationMode === "onChange" || validationMode === "all") &&
141
- (!!fieldState.error || fieldState.isTouched));
142
- methods.setValue(field.name, unmasked, {
143
- shouldDirty: true,
144
- shouldValidate,
145
- });
146
- }}
147
- // Remove unwanted props
148
- defaultValue={undefined}
149
- onChange={undefined}
150
- />
151
- )}
141
+ <MaskInput
142
+ // Overridable props
143
+ id={field.name}
144
+ {...props}
145
+ // Strict props
146
+ ref={mergeRefs(inputRef, field.ref)}
147
+ editable={!field.disabled && (props.editable ?? true)}
148
+ onBlur={(evt) => {
149
+ const shouldValidate =
150
+ validationMode === "onBlur" ||
151
+ validationMode === "onTouched" ||
152
+ validationMode === "all";
153
+ methods.setValue(field.name, field.value, {
154
+ shouldDirty: true,
155
+ shouldTouch: true,
156
+ shouldValidate,
157
+ });
158
+ props.onBlur?.(evt);
159
+ }}
160
+ mask={mask}
161
+ maskAutoComplete={!!mask}
162
+ obfuscationCharacter={obfuscationCharacter}
163
+ showObfuscatedValue={!!obfuscateValue}
164
+ value={field.value}
165
+ onChangeText={(masked, unmasked) => {
166
+ const shouldValidate =
167
+ (validationMode === "onTouched" && fieldState.isTouched) ||
168
+ ((validationMode === "onChange" || validationMode === "all") &&
169
+ (!!fieldState.error || fieldState.isTouched));
170
+ methods.setValue(field.name, unmasked, {
171
+ shouldDirty: true,
172
+ shouldValidate,
173
+ });
174
+ }}
175
+ // Remove unwanted props
176
+ defaultValue={undefined}
177
+ onChange={undefined}
152
178
  />
153
179
  );
154
180
  }) as <Values extends Record<string, unknown>>(
@@ -0,0 +1,19 @@
1
+ import { ThreeDSecureSession } from "./types";
2
+
3
+ export type ThreeDSecureEventType = "requestChallenge";
4
+
5
+ export class ThreeDSecureEvent {
6
+ constructor(
7
+ public readonly type: ThreeDSecureEventType,
8
+ public readonly session: ThreeDSecureSession,
9
+ private _defaultPrevented: boolean = false
10
+ ) {}
11
+
12
+ public preventDefault() {
13
+ this._defaultPrevented = true;
14
+ }
15
+
16
+ public get defaultPrevented() {
17
+ return this._defaultPrevented;
18
+ }
19
+ }
@@ -1,4 +1,5 @@
1
1
  import { EV_API_DOMAIN } from "./config";
2
+ import { ThreeDSecureEvent } from "./event";
2
3
  import {
3
4
  pollSession,
4
5
  startSession,
@@ -24,7 +25,7 @@ describe("stopPolling", () => {
24
25
  });
25
26
 
26
27
  describe("startSession", () => {
27
- const callbacks = {
28
+ const options = {
28
29
  onSuccess: vi.fn(),
29
30
  onFailure: vi.fn(),
30
31
  onError: vi.fn(),
@@ -32,6 +33,10 @@ describe("startSession", () => {
32
33
  const intervalRef = { current: null };
33
34
  const setIsVisible = vi.fn();
34
35
 
36
+ beforeEach(() => {
37
+ vi.clearAllMocks();
38
+ });
39
+
35
40
  it("should start a successful session", async () => {
36
41
  const session: ThreeDSecureSession = {
37
42
  cancel: vi.fn(),
@@ -39,10 +44,10 @@ describe("startSession", () => {
39
44
  sessionId: "123",
40
45
  };
41
46
 
42
- await startSession(session, callbacks, intervalRef, setIsVisible);
47
+ await startSession(session, options, intervalRef, setIsVisible);
43
48
 
44
49
  expect(session.get).toHaveBeenCalled();
45
- expect(callbacks.onSuccess).toHaveBeenCalled();
50
+ expect(options.onSuccess).toHaveBeenCalled();
46
51
  });
47
52
 
48
53
  it("should start a failed session", async () => {
@@ -52,10 +57,10 @@ describe("startSession", () => {
52
57
  sessionId: "123",
53
58
  };
54
59
 
55
- await startSession(session, callbacks, intervalRef, setIsVisible);
60
+ await startSession(session, options, intervalRef, setIsVisible);
56
61
 
57
62
  expect(session.get).toHaveBeenCalled();
58
- expect(callbacks.onFailure).toHaveBeenCalledWith(
63
+ expect(options.onFailure).toHaveBeenCalledWith(
59
64
  new Error("3DS session failed")
60
65
  );
61
66
  });
@@ -67,12 +72,98 @@ describe("startSession", () => {
67
72
  sessionId: "123",
68
73
  };
69
74
 
70
- await startSession(session, callbacks, intervalRef, setIsVisible);
75
+ await startSession(session, options, intervalRef, setIsVisible);
71
76
 
72
77
  expect(session.get).toHaveBeenCalled();
73
78
  expect(setIsVisible).toHaveBeenCalledWith(true);
74
79
  });
75
80
 
81
+ it("should fail the session when failOnChallenge is true and a challenge is required", async () => {
82
+ const session: ThreeDSecureSession = {
83
+ cancel: vi.fn(),
84
+ get: vi.fn(() => Promise.resolve({ status: "action-required" }) as any),
85
+ sessionId: "123",
86
+ };
87
+
88
+ await startSession(
89
+ session,
90
+ { ...options, failOnChallenge: true },
91
+ intervalRef,
92
+ setIsVisible
93
+ );
94
+
95
+ expect(session.get).toHaveBeenCalled();
96
+ expect(options.onFailure).toHaveBeenCalledWith(
97
+ new Error("3DS session failed")
98
+ );
99
+ });
100
+
101
+ it("should fail the session when failOnChallenge is a function that returns true and a challenge is required", async () => {
102
+ const session: ThreeDSecureSession = {
103
+ cancel: vi.fn(),
104
+ get: vi.fn(() => Promise.resolve({ status: "action-required" }) as any),
105
+ sessionId: "123",
106
+ };
107
+
108
+ await startSession(
109
+ session,
110
+ { ...options, failOnChallenge: () => Promise.resolve(true) },
111
+ intervalRef,
112
+ setIsVisible
113
+ );
114
+
115
+ expect(session.get).toHaveBeenCalled();
116
+ expect(options.onFailure).toHaveBeenCalledWith(
117
+ new Error("3DS session failed")
118
+ );
119
+ });
120
+
121
+ it("should call onRequestChallenge when a challenge is required", async () => {
122
+ const session: ThreeDSecureSession = {
123
+ cancel: vi.fn(),
124
+ get: vi.fn(() => Promise.resolve({ status: "action-required" }) as any),
125
+ sessionId: "123",
126
+ };
127
+
128
+ const onRequestChallenge = vi.fn();
129
+
130
+ await startSession(
131
+ session,
132
+ { ...options, onRequestChallenge },
133
+ intervalRef,
134
+ setIsVisible
135
+ );
136
+
137
+ expect(session.get).toHaveBeenCalled();
138
+ expect(onRequestChallenge).toHaveBeenCalled();
139
+ expect(options.onFailure).not.toHaveBeenCalled();
140
+ });
141
+
142
+ it("should fail the session when onRequestChallenge is called and default is prevented", async () => {
143
+ const session: ThreeDSecureSession = {
144
+ cancel: vi.fn(),
145
+ get: vi.fn(() => Promise.resolve({ status: "action-required" }) as any),
146
+ sessionId: "123",
147
+ };
148
+
149
+ const onRequestChallenge = vi.fn((event: ThreeDSecureEvent) => {
150
+ event.preventDefault();
151
+ });
152
+
153
+ await startSession(
154
+ session,
155
+ { ...options, onRequestChallenge },
156
+ intervalRef,
157
+ setIsVisible
158
+ );
159
+
160
+ expect(session.get).toHaveBeenCalled();
161
+ expect(onRequestChallenge).toHaveBeenCalled();
162
+ expect(options.onFailure).toHaveBeenCalledWith(
163
+ new Error("3DS session failed")
164
+ );
165
+ });
166
+
76
167
  it("should call onError if the session fails to start", async () => {
77
168
  const session: ThreeDSecureSession = {
78
169
  cancel: vi.fn(),
@@ -84,21 +175,21 @@ describe("startSession", () => {
84
175
  .spyOn(console, "error")
85
176
  .mockImplementation(() => {});
86
177
 
87
- await startSession(session, callbacks, intervalRef, setIsVisible);
178
+ await startSession(session, options, intervalRef, setIsVisible);
88
179
 
89
180
  expect(session.get).toHaveBeenCalled();
90
181
  expect(consoleErrorSpy).toHaveBeenCalledWith(
91
182
  "Error checking session state",
92
183
  new Error("Failed to start session")
93
184
  );
94
- expect(callbacks.onError).toHaveBeenCalledWith(
185
+ expect(options.onError).toHaveBeenCalledWith(
95
186
  new Error("Failed to check 3DS session state")
96
187
  );
97
188
  });
98
189
  });
99
190
 
100
191
  describe("pollSession", () => {
101
- const callbacks = {
192
+ const options = {
102
193
  onSuccess: vi.fn(),
103
194
  onFailure: vi.fn(),
104
195
  onError: vi.fn(),
@@ -106,6 +197,10 @@ describe("pollSession", () => {
106
197
  const intervalRef = { current: null };
107
198
  const setIsVisible = vi.fn();
108
199
 
200
+ beforeEach(() => {
201
+ vi.clearAllMocks();
202
+ });
203
+
109
204
  it("should start an interval", () => {
110
205
  const session: ThreeDSecureSession = {
111
206
  cancel: vi.fn(),
@@ -114,7 +209,7 @@ describe("pollSession", () => {
114
209
  };
115
210
 
116
211
  const intervalSpy = vi.spyOn(global, "setInterval");
117
- pollSession(session, callbacks, intervalRef, setIsVisible);
212
+ pollSession(session, options, intervalRef, setIsVisible);
118
213
 
119
214
  expect(intervalSpy).toHaveBeenCalledWith(expect.any(Function), 3000);
120
215
  });
@@ -127,11 +222,11 @@ describe("pollSession", () => {
127
222
  };
128
223
 
129
224
  const intervalSpy = vi.spyOn(global, "setInterval");
130
- pollSession(session, callbacks, intervalRef, setIsVisible);
225
+ pollSession(session, options, intervalRef, setIsVisible);
131
226
  await intervalSpy.mock.calls[0][0]();
132
227
 
133
228
  expect(session.get).toHaveBeenCalled();
134
- expect(callbacks.onSuccess).toHaveBeenCalled();
229
+ expect(options.onSuccess).toHaveBeenCalled();
135
230
  });
136
231
 
137
232
  it("should poll failed session", async () => {
@@ -142,11 +237,11 @@ describe("pollSession", () => {
142
237
  };
143
238
 
144
239
  const intervalSpy = vi.spyOn(global, "setInterval");
145
- pollSession(session, callbacks, intervalRef, setIsVisible);
240
+ pollSession(session, options, intervalRef, setIsVisible);
146
241
  await intervalSpy.mock.calls[0][0]();
147
242
 
148
243
  expect(session.get).toHaveBeenCalled();
149
- expect(callbacks.onFailure).toHaveBeenCalledWith(
244
+ expect(options.onFailure).toHaveBeenCalledWith(
150
245
  new Error("3DS session failed")
151
246
  );
152
247
  });
@@ -159,13 +254,113 @@ describe("pollSession", () => {
159
254
  };
160
255
 
161
256
  const intervalSpy = vi.spyOn(global, "setInterval");
162
- pollSession(session, callbacks, intervalRef, setIsVisible);
257
+ pollSession(session, options, intervalRef, setIsVisible);
163
258
  await intervalSpy.mock.calls[0][0]();
164
259
 
165
260
  expect(session.get).toHaveBeenCalled();
166
261
  expect(setIsVisible).toHaveBeenCalledWith(true);
167
262
  });
168
263
 
264
+ it("should fail the session when failOnChallenge is true and a challenge is required", async () => {
265
+ const session: ThreeDSecureSession = {
266
+ cancel: vi.fn(),
267
+ get: vi.fn(() => Promise.resolve({ status: "action-required" }) as any),
268
+ sessionId: "123",
269
+ };
270
+
271
+ const intervalSpy = vi.spyOn(global, "setInterval");
272
+ const onRequestChallenge = vi.fn();
273
+ pollSession(
274
+ session,
275
+ { ...options, failOnChallenge: true, onRequestChallenge },
276
+ intervalRef,
277
+ setIsVisible
278
+ );
279
+ await intervalSpy.mock.calls[0][0]();
280
+
281
+ expect(session.get).toHaveBeenCalled();
282
+ expect(onRequestChallenge).not.toHaveBeenCalled();
283
+ expect(options.onFailure).toHaveBeenCalledWith(
284
+ new Error("3DS session failed")
285
+ );
286
+ });
287
+
288
+ it("should fail the session when failOnChallenge is a function that returns true and a challenge is required", async () => {
289
+ const session: ThreeDSecureSession = {
290
+ cancel: vi.fn(),
291
+ get: vi.fn(() => Promise.resolve({ status: "action-required" }) as any),
292
+ sessionId: "123",
293
+ };
294
+
295
+ const intervalSpy = vi.spyOn(global, "setInterval");
296
+ const onRequestChallenge = vi.fn();
297
+ pollSession(
298
+ session,
299
+ {
300
+ ...options,
301
+ onRequestChallenge,
302
+ failOnChallenge: () => Promise.resolve(true),
303
+ },
304
+ intervalRef,
305
+ setIsVisible
306
+ );
307
+ await intervalSpy.mock.calls[0][0]();
308
+
309
+ expect(session.get).toHaveBeenCalled();
310
+ expect(onRequestChallenge).not.toHaveBeenCalled();
311
+ expect(options.onFailure).toHaveBeenCalledWith(
312
+ new Error("3DS session failed")
313
+ );
314
+ });
315
+
316
+ it("should call onRequestChallenge when a challenge is required", async () => {
317
+ const session: ThreeDSecureSession = {
318
+ cancel: vi.fn(),
319
+ get: vi.fn(() => Promise.resolve({ status: "action-required" }) as any),
320
+ sessionId: "123",
321
+ };
322
+
323
+ const intervalSpy = vi.spyOn(global, "setInterval");
324
+ const onRequestChallenge = vi.fn();
325
+ pollSession(
326
+ session,
327
+ { ...options, onRequestChallenge },
328
+ intervalRef,
329
+ setIsVisible
330
+ );
331
+ await intervalSpy.mock.calls[0][0]();
332
+
333
+ expect(session.get).toHaveBeenCalled();
334
+ expect(onRequestChallenge).toHaveBeenCalled();
335
+ expect(options.onFailure).not.toHaveBeenCalled();
336
+ });
337
+
338
+ it("should fail the session when onRequestChallenge is called and default is prevented", async () => {
339
+ const session: ThreeDSecureSession = {
340
+ cancel: vi.fn(),
341
+ get: vi.fn(() => Promise.resolve({ status: "action-required" }) as any),
342
+ sessionId: "123",
343
+ };
344
+
345
+ const intervalSpy = vi.spyOn(global, "setInterval");
346
+ const onRequestChallenge = vi.fn((event: ThreeDSecureEvent) => {
347
+ event.preventDefault();
348
+ });
349
+ pollSession(
350
+ session,
351
+ { ...options, onRequestChallenge },
352
+ intervalRef,
353
+ setIsVisible
354
+ );
355
+ await intervalSpy.mock.calls[0][0]();
356
+
357
+ expect(session.get).toHaveBeenCalled();
358
+ expect(onRequestChallenge).toHaveBeenCalled();
359
+ expect(options.onFailure).toHaveBeenCalledWith(
360
+ new Error("3DS session failed")
361
+ );
362
+ });
363
+
169
364
  it("should call onError if the session fails to poll", async () => {
170
365
  const session: ThreeDSecureSession = {
171
366
  cancel: vi.fn(),
@@ -178,7 +373,7 @@ describe("pollSession", () => {
178
373
  .mockImplementation(() => {});
179
374
 
180
375
  const intervalSpy = vi.spyOn(global, "setInterval");
181
- pollSession(session, callbacks, intervalRef, setIsVisible);
376
+ pollSession(session, options, intervalRef, setIsVisible);
182
377
  await intervalSpy.mock.calls[0][0]();
183
378
 
184
379
  expect(session.get).toHaveBeenCalled();
@@ -186,14 +381,14 @@ describe("pollSession", () => {
186
381
  "Error polling session",
187
382
  new Error("Failed to poll session")
188
383
  );
189
- expect(callbacks.onError).toHaveBeenCalledWith(
384
+ expect(options.onError).toHaveBeenCalledWith(
190
385
  new Error("Error polling 3DS session")
191
386
  );
192
387
  });
193
388
  });
194
389
 
195
390
  describe("threeDSecureSession", () => {
196
- const callbacks = {
391
+ const options = {
197
392
  onSuccess: vi.fn(),
198
393
  onFailure: vi.fn(),
199
394
  onError: vi.fn(),
@@ -205,7 +400,7 @@ describe("threeDSecureSession", () => {
205
400
  const session = threeDSecureSession({
206
401
  sessionId: "123",
207
402
  appId: "app_123",
208
- callbacks,
403
+ options,
209
404
  intervalRef,
210
405
  setIsVisible,
211
406
  });
@@ -220,7 +415,7 @@ describe("threeDSecureSession", () => {
220
415
  const session = threeDSecureSession({
221
416
  sessionId: "123",
222
417
  appId: "app_123",
223
- callbacks,
418
+ options,
224
419
  intervalRef,
225
420
  setIsVisible,
226
421
  });
@@ -250,7 +445,7 @@ describe("threeDSecureSession", () => {
250
445
  const session = threeDSecureSession({
251
446
  sessionId: "123",
252
447
  appId: "app_123",
253
- callbacks,
448
+ options,
254
449
  intervalRef,
255
450
  setIsVisible,
256
451
  });
@@ -275,7 +470,7 @@ describe("threeDSecureSession", () => {
275
470
  const session = threeDSecureSession({
276
471
  sessionId: "123",
277
472
  appId: "app_123",
278
- callbacks,
473
+ options,
279
474
  intervalRef,
280
475
  setIsVisible,
281
476
  });
@@ -297,7 +492,7 @@ describe("threeDSecureSession", () => {
297
492
  }
298
493
  );
299
494
 
300
- expect(callbacks.onFailure).toHaveBeenCalledWith(
495
+ expect(options.onFailure).toHaveBeenCalledWith(
301
496
  new Error("3DS session cancelled by user")
302
497
  );
303
498
  });
@@ -306,7 +501,7 @@ describe("threeDSecureSession", () => {
306
501
  const session = threeDSecureSession({
307
502
  sessionId: "123",
308
503
  appId: "app_123",
309
- callbacks,
504
+ options,
310
505
  intervalRef,
311
506
  setIsVisible,
312
507
  });