-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
153 lines (134 loc) · 5.05 KB
/
index.test.ts
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
import nock from 'nock';
import { create } from '.';
import { BASE_URL, endpoint } from '@lib/client';
import pino from 'pino';
import { HttpRequestError, MaxRetriesExceededError } from '@lib/client/errors';
export const logger = pino({
name: 'mls-ingest',
level: 'error',
});
describe('Create Property', () => {
const input = {
mls_name: 'Example MLS',
mls_id: 123456,
street_address: '123 Main St',
city: 'Beverly Hills',
state: 'CA',
zip_code: 90210,
list_price: 4_000_000,
list_date: 1525143600,
bedrooms: 3,
full_baths: 2,
half_baths: 1,
size: 1500,
};
const output = { id: 1, ...input };
beforeEach(() => {
nock.cleanAll();
});
afterEach(() => {
nock.cleanAll();
nock.enableNetConnect();
jest.clearAllMocks();
});
describe('When successful', () => {
it('should return the property data on successful creation', async () => {
nock(BASE_URL).post(endpoint(1)).reply(201, output);
const response = await create({ customerId: 1, input });
expect(response).toStrictEqual(output);
});
});
describe('When unsuccessful', () => {
describe('Vendor Configuration Errors', () => {
it('should throw an error indicating a problem with vendor configuration', async () => {
try {
await create({ customerId: 1, input, vendor: 'invalid_vendor' });
fail('Expected to throw an error but did not.');
} catch (e) {
const error = e as Error;
expect(error.message).toContain("Error loading config for vendor 'invalid_vendor'");
}
});
});
describe('Input Validation Errors', () => {
it('should indicate an error with input validation', async () => {
try {
await create({ customerId: 1, input: { ...input, list_price: 'four million' } });
fail('Expected to throw an error but did not.');
} catch (e) {
const error = e as Error;
expect(error.message).toContain('Error parsing input schema.');
}
});
});
describe('API Errors', () => {
const retryConfig = {
initialDelay: 1,
maxRetries: 2,
multiplier: 2,
retryableStatusCodes: [500, 502, 503, 504],
};
describe('when retryable', () => {
beforeEach(() => {
nock(BASE_URL).persist().post(endpoint(1)).reply(500, 'Internal Server Error');
});
it('should throw MaxRetriesExceededError', async () => {
try {
await create({ customerId: 1, input, vendor: 'default', retryConfig });
fail('Expected to throw an error but did not.');
} catch (e) {
const error = e as Error;
expect(error.message).toContain('Maximum retry attempts exceeded');
}
});
it('should invoke the provided error handler with detailed error information on retryable failures', async () => {
nock(BASE_URL).persist().post(endpoint(1)).reply(500, 'Internal Server Error');
const errorHandler = jest.fn();
errorHandler.mockImplementation((error: HttpRequestError | MaxRetriesExceededError) => {
logger.error(error.message);
});
await create({
customerId: 1,
input,
vendor: 'default',
retryConfig: {
initialDelay: 1,
maxRetries: 2,
multiplier: 2,
retryableStatusCodes: [500],
},
errorHandler,
});
expect(errorHandler).toHaveBeenCalled();
const errorHandlerCallArg = errorHandler.mock.calls[0][0];
expect(errorHandlerCallArg.message).toContain('Maximum retry attempts exceeded');
expect(errorHandlerCallArg).toHaveProperty('count', 2);
expect(errorHandlerCallArg.lastError).toHaveProperty('status', 500);
});
});
describe('when not retryable', () => {
beforeEach(() => {
nock(BASE_URL).persist().post(endpoint(1)).reply(422, 'Unprocessable Entity');
});
it('should throw HttpRequestError', async () => {
try {
await create({ customerId: 1, input, vendor: 'default', retryConfig });
fail('Expected to throw an error but did not.');
} catch (e) {
const error = e as Error;
expect(error.message).toContain('Request failed with status code');
}
});
it('should invoke the provided error handler with detailed error information on non-retryable failures', async () => {
nock(BASE_URL).persist().post(endpoint(1)).reply(422, 'Unprocessable Entity');
const errorHandler = jest.fn();
await create({ customerId: 1, input, vendor: 'default', errorHandler });
expect(errorHandler).toHaveBeenCalled();
const errorHandlerCallArg = errorHandler.mock.calls[0][0];
expect(errorHandlerCallArg.message).toContain('Request failed with status code 422');
expect(errorHandlerCallArg).toHaveProperty('status', 422);
});
});
});
});
});