-
-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy patherrorNormalization.test.ts
More file actions
97 lines (85 loc) · 2.68 KB
/
Copy patherrorNormalization.test.ts
File metadata and controls
97 lines (85 loc) · 2.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
import {
extractExplicitTypedError,
getErrorMessage,
normalizeToTypedError,
} from './errorNormalization';
type Code = 'NO_QUOTES' | 'QUOTE_FAILED' | 'UNKNOWN';
const isValidCode = (value: unknown): value is Code =>
value === 'NO_QUOTES' || value === 'QUOTE_FAILED' || value === 'UNKNOWN';
describe('getErrorMessage', () => {
it('reads Error.message', () => {
expect(getErrorMessage(new Error('boom'))).toBe('boom');
});
it('reads a record message', () => {
expect(getErrorMessage({ message: 'nope' })).toBe('nope');
});
it('returns a string value directly', () => {
expect(getErrorMessage('raw')).toBe('raw');
});
it('returns undefined when no message can be derived', () => {
expect(getErrorMessage(42)).toBeUndefined();
expect(getErrorMessage(null)).toBeUndefined();
});
});
describe('extractExplicitTypedError', () => {
it('reads a valid code from the default `code` property', () => {
expect(
extractExplicitTypedError(
{ code: 'NO_QUOTES', message: 'none' },
{ isValidCode },
),
).toStrictEqual({ code: 'NO_QUOTES', message: 'none', details: undefined });
});
it('honours codeProperties precedence order', () => {
expect(
extractExplicitTypedError(
{ headlessCode: 'QUOTE_FAILED', code: 'NO_QUOTES' },
{ isValidCode, codeProperties: ['headlessCode', 'code'] },
),
).toStrictEqual({
code: 'QUOTE_FAILED',
message: undefined,
details: undefined,
});
});
it('passes through a record details object', () => {
expect(
extractExplicitTypedError(
{ code: 'NO_QUOTES', details: { providerId: 'moonpay' } },
{ isValidCode },
),
).toStrictEqual({
code: 'NO_QUOTES',
message: undefined,
details: { providerId: 'moonpay' },
});
});
it('returns undefined when no valid code is present', () => {
expect(
extractExplicitTypedError({ code: 'NOT_A_CODE' }, { isValidCode }),
).toBeUndefined();
expect(extractExplicitTypedError('boom', { isValidCode })).toBeUndefined();
});
});
describe('normalizeToTypedError', () => {
it('returns the explicit typed error when present', () => {
expect(
normalizeToTypedError(
{ code: 'QUOTE_FAILED', message: 'x' },
{ isValidCode, fallbackCode: 'UNKNOWN' },
),
).toStrictEqual({
code: 'QUOTE_FAILED',
message: 'x',
details: undefined,
});
});
it('falls back with the derived message when no valid code is present', () => {
expect(
normalizeToTypedError(new Error('boom'), {
isValidCode,
fallbackCode: 'UNKNOWN',
}),
).toStrictEqual({ code: 'UNKNOWN', message: 'boom' });
});
});