Skip to content

Commit 7c1ba2b

Browse files
committed
test(): added pact utils
1 parent d382305 commit 7c1ba2b

18 files changed

+212
-154
lines changed

test/client/autosuggest-session.spec.ts renamed to __tests__/client/autosuggest-session.spec.ts

Lines changed: 48 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
import {
2-
Matchers,
3-
Pact,
4-
RequestOptions,
5-
ResponseOptions,
6-
} from '@pact-foundation/pact';
1+
import { Matchers, ResponseOptions } from '@pact-foundation/pact';
72
import { Chance } from 'chance';
8-
import * as path from 'path';
9-
import { ApiVersion, AutosuggestClient } from '../../src';
103
import {
114
BadGatewayError,
125
ForbiddenError,
@@ -15,18 +8,16 @@ import {
158
NotFoundError,
169
ServiceUnavailableError,
1710
} from '../../src/lib/transport/error';
11+
import {
12+
createAutosuggestPact,
13+
createPactMockRequest,
14+
createPactMockResponse,
15+
} from '../pact/utils';
1816

1917
const chance = new Chance();
2018

2119
describe('Autosuggest Session Pact', () => {
22-
const randomApiKey = chance.string({
23-
length: 8,
24-
alpha: true,
25-
numeric: true,
26-
symbols: false,
27-
casing: 'upper',
28-
});
29-
20+
const correlationId = Matchers.uuid().getValue();
3021
const genRandomDigit = () =>
3122
chance.string({
3223
length: 1,
@@ -35,11 +26,6 @@ describe('Autosuggest Session Pact', () => {
3526
symbols: false,
3627
casing: 'lower',
3728
});
38-
const apiKey = Matchers.term({
39-
matcher: '[a-zA-Z0-9]{8}',
40-
generate: randomApiKey,
41-
}).getValue();
42-
const correlationId = Matchers.uuid().getValue();
4329
const randomVersion = [
4430
genRandomDigit(),
4531
genRandomDigit(),
@@ -57,21 +43,7 @@ describe('Autosuggest Session Pact', () => {
5743
chance.pickone(['default', 'inherit'])
5844
).getValue() as never;
5945

60-
const apiVersion = ApiVersion.Version3;
61-
const port = 9000;
62-
const host = `http://localhost:${port}`;
63-
const client = new AutosuggestClient(apiKey, { host, apiVersion, headers: {} });
64-
65-
// Create the Pact object to represent your provider
66-
const provider = new Pact({
67-
consumer: 'w3w-node-wrapper',
68-
provider: 'api-server',
69-
port,
70-
log: path.resolve(process.cwd(), 'logs', 'pact.log'),
71-
dir: path.resolve(process.cwd(), 'pacts'),
72-
logLevel: 'info',
73-
pactfileWriteMode: 'overwrite',
74-
});
46+
const { apiKey, apiVersion, client, provider } = createAutosuggestPact();
7547

7648
// Start the mock server
7749
beforeAll(() => provider.setup());
@@ -88,8 +60,8 @@ describe('Autosuggest Session Pact', () => {
8860
afterAll(() => provider.finalize());
8961

9062
describe('When I do not have a current autosuggest session', () => {
91-
describe('And there is a sucessful request to start a session', () => {
92-
const MOCK_REQUEST: RequestOptions = {
63+
describe('And there is a successful request to start a session', () => {
64+
const MOCK_REQUEST = createPactMockRequest({
9365
method: 'POST' as const,
9466
path: `/${apiVersion}/autosuggest-session`,
9567
query: {
@@ -105,8 +77,9 @@ describe('Autosuggest Session Pact', () => {
10577
variant,
10678
version,
10779
},
108-
};
109-
const MOCK_RESPONSE: ResponseOptions = {
80+
});
81+
82+
const MOCK_RESPONSE = createPactMockResponse({
11083
status: 200,
11184
headers: {
11285
'Content-Type': 'application/json',
@@ -116,7 +89,7 @@ describe('Autosuggest Session Pact', () => {
11689
status: 200,
11790
message: 'ok',
11891
},
119-
};
92+
});
12093

12194
// Add interactions to the Mock Server, as many as required
12295
beforeAll(() =>
@@ -148,7 +121,7 @@ describe('Autosuggest Session Pact', () => {
148121
});
149122

150123
describe('And there is a failed request to update the session', () => {
151-
const MOCK_REQUEST: RequestOptions = {
124+
const MOCK_REQUEST = createPactMockRequest({
152125
method: 'POST' as const,
153126
path: `/${apiVersion}/autosuggest-session`,
154127
query: {
@@ -164,7 +137,7 @@ describe('Autosuggest Session Pact', () => {
164137
variant,
165138
version,
166139
},
167-
};
140+
});
168141

169142
const errors = [
170143
new ForbiddenError(),
@@ -216,7 +189,7 @@ describe('Autosuggest Session Pact', () => {
216189

217190
describe('And there is an unauthorized request', () => {
218191
it('should return a 401 error when apiKey is missing', async () => {
219-
const MOCK_REQUEST: RequestOptions = {
192+
const MOCK_REQUEST = createPactMockRequest({
220193
method: 'POST' as const,
221194
path: `/${apiVersion}/autosuggest-session`,
222195
headers: {
@@ -229,8 +202,8 @@ describe('Autosuggest Session Pact', () => {
229202
variant,
230203
version,
231204
},
232-
};
233-
const MOCK_RESPONSE: ResponseOptions = {
205+
});
206+
const MOCK_RESPONSE = createPactMockResponse({
234207
status: 401,
235208
headers: {
236209
'Content-Type': 'application/json',
@@ -240,7 +213,7 @@ describe('Autosuggest Session Pact', () => {
240213
status: 401,
241214
message: 'Unauthorized',
242215
},
243-
};
216+
});
244217
// Add interactions to the Mock Server, as many as required
245218
await provider.addInteraction({
246219
// The 'state' field specifies a "Provider State"
@@ -267,7 +240,7 @@ describe('Autosuggest Session Pact', () => {
267240

268241
describe('And there is bad request ', () => {
269242
it('should return a 400 error when returnCoordinates is missing', async () => {
270-
const MOCK_REQUEST: RequestOptions = {
243+
const MOCK_REQUEST = createPactMockRequest({
271244
method: 'POST' as const,
272245
path: `/${apiVersion}/autosuggest-session`,
273246
headers: {
@@ -279,8 +252,8 @@ describe('Autosuggest Session Pact', () => {
279252
variant,
280253
version,
281254
},
282-
};
283-
const MOCK_RESPONSE: ResponseOptions = {
255+
});
256+
const MOCK_RESPONSE = createPactMockResponse({
284257
status: 400,
285258
headers: {
286259
'Content-Type': 'application/json',
@@ -290,7 +263,7 @@ describe('Autosuggest Session Pact', () => {
290263
status: 400,
291264
message: 'Bad Request',
292265
},
293-
};
266+
});
294267
// Add interactions to the Mock Server, as many as required
295268
await provider.addInteraction({
296269
// The 'state' field specifies a "Provider State"
@@ -315,7 +288,7 @@ describe('Autosuggest Session Pact', () => {
315288
});
316289

317290
it('should return a 400 error when typeheadDelay is missing', async () => {
318-
const MOCK_REQUEST: RequestOptions = {
291+
const MOCK_REQUEST = createPactMockRequest({
319292
method: 'POST' as const,
320293
path: `/${apiVersion}/autosuggest-session`,
321294
headers: {
@@ -327,8 +300,8 @@ describe('Autosuggest Session Pact', () => {
327300
variant,
328301
version,
329302
},
330-
};
331-
const MOCK_RESPONSE: ResponseOptions = {
303+
});
304+
const MOCK_RESPONSE = createPactMockResponse({
332305
status: 400,
333306
headers: {
334307
'Content-Type': 'application/json',
@@ -338,7 +311,7 @@ describe('Autosuggest Session Pact', () => {
338311
status: 400,
339312
message: 'Bad Request',
340313
},
341-
};
314+
});
342315
// Add interactions to the Mock Server, as many as required
343316
await provider.addInteraction({
344317
// The 'state' field specifies a "Provider State"
@@ -366,7 +339,7 @@ describe('Autosuggest Session Pact', () => {
366339

367340
describe('When I have a current autosuggest session', () => {
368341
describe('And there is a request to update the session', () => {
369-
const MOCK_REQUEST: RequestOptions = {
342+
const MOCK_REQUEST = createPactMockRequest({
370343
method: 'PUT' as const,
371344
path: `/${apiVersion}/autosuggest-session`,
372345
query: {
@@ -382,8 +355,8 @@ describe('Autosuggest Session Pact', () => {
382355
variant,
383356
version,
384357
},
385-
};
386-
const MOCK_RESPONSE: ResponseOptions = {
358+
});
359+
const MOCK_RESPONSE = createPactMockResponse({
387360
status: 200,
388361
headers: {
389362
'Content-Type': 'application/json',
@@ -392,7 +365,7 @@ describe('Autosuggest Session Pact', () => {
392365
body: {
393366
status: 200,
394367
},
395-
};
368+
});
396369

397370
beforeAll(() =>
398371
provider.addInteraction({
@@ -422,7 +395,7 @@ describe('Autosuggest Session Pact', () => {
422395

423396
describe('And there is an unauthorized request', () => {
424397
it('should return a 401 error when apiKey is missing', async () => {
425-
const MOCK_REQUEST: RequestOptions = {
398+
const MOCK_REQUEST = createPactMockRequest({
426399
method: 'PUT' as const,
427400
path: `/${apiVersion}/autosuggest-session`,
428401
headers: {
@@ -435,8 +408,8 @@ describe('Autosuggest Session Pact', () => {
435408
variant,
436409
version,
437410
},
438-
};
439-
const MOCK_RESPONSE: ResponseOptions = {
411+
});
412+
const MOCK_RESPONSE = createPactMockResponse({
440413
status: 401,
441414
headers: {
442415
'Content-Type': 'application/json',
@@ -446,7 +419,7 @@ describe('Autosuggest Session Pact', () => {
446419
status: 401,
447420
message: 'Unauthorized',
448421
},
449-
};
422+
});
450423
// Add interactions to the Mock Server, as many as required
451424
await provider.addInteraction({
452425
// The 'state' field specifies a "Provider State"
@@ -473,7 +446,7 @@ describe('Autosuggest Session Pact', () => {
473446

474447
describe('And there is bad request ', () => {
475448
it('should return a 400 error when returnCoordinates is missing', async () => {
476-
const MOCK_REQUEST: RequestOptions = {
449+
const MOCK_REQUEST = createPactMockRequest({
477450
method: 'PUT' as const,
478451
path: `/${apiVersion}/autosuggest-session`,
479452
headers: {
@@ -485,8 +458,8 @@ describe('Autosuggest Session Pact', () => {
485458
variant,
486459
version,
487460
},
488-
};
489-
const MOCK_RESPONSE: ResponseOptions = {
461+
});
462+
const MOCK_RESPONSE = createPactMockResponse({
490463
status: 400,
491464
headers: {
492465
'Content-Type': 'application/json',
@@ -496,7 +469,7 @@ describe('Autosuggest Session Pact', () => {
496469
status: 400,
497470
message: 'Bad Request',
498471
},
499-
};
472+
});
500473
// Add interactions to the Mock Server, as many as required
501474
await provider.addInteraction({
502475
// The 'state' field specifies a "Provider State"
@@ -521,7 +494,7 @@ describe('Autosuggest Session Pact', () => {
521494
});
522495

523496
it('should return a 400 error when typeheadDelay is missing', async () => {
524-
const MOCK_REQUEST: RequestOptions = {
497+
const MOCK_REQUEST = createPactMockRequest({
525498
method: 'PUT' as const,
526499
path: `/${apiVersion}/autosuggest-session`,
527500
headers: {
@@ -533,8 +506,8 @@ describe('Autosuggest Session Pact', () => {
533506
variant,
534507
version,
535508
},
536-
};
537-
const MOCK_RESPONSE: ResponseOptions = {
509+
});
510+
const MOCK_RESPONSE = createPactMockResponse({
538511
status: 400,
539512
headers: {
540513
'Content-Type': 'application/json',
@@ -544,7 +517,7 @@ describe('Autosuggest Session Pact', () => {
544517
status: 400,
545518
message: 'Bad Request',
546519
},
547-
};
520+
});
548521
// Add interactions to the Mock Server, as many as required
549522
await provider.addInteraction({
550523
// The 'state' field specifies a "Provider State"
@@ -570,7 +543,7 @@ describe('Autosuggest Session Pact', () => {
570543
});
571544

572545
describe('And there is a failed request to update the session', () => {
573-
const MOCK_REQUEST: RequestOptions = {
546+
const MOCK_REQUEST = createPactMockRequest({
574547
method: 'PUT' as const,
575548
path: `/${apiVersion}/autosuggest-session`,
576549
query: {
@@ -586,7 +559,7 @@ describe('Autosuggest Session Pact', () => {
586559
variant,
587560
version,
588561
},
589-
};
562+
});
590563

591564
const errors = [
592565
new ForbiddenError(),
@@ -600,7 +573,7 @@ describe('Autosuggest Session Pact', () => {
600573
errors.forEach(err => {
601574
// Write your test(s)
602575
it(`should return a ${err.status} error`, async () => {
603-
const MOCK_RESPONSE: ResponseOptions = {
576+
const MOCK_RESPONSE = createPactMockResponse({
604577
status: err.status,
605578
headers: {
606579
'Content-Type': 'application/json',
@@ -610,7 +583,7 @@ describe('Autosuggest Session Pact', () => {
610583
status: err.status,
611584
message: err.message,
612585
},
613-
};
586+
});
614587
// Add interactions to the Mock Server, as many as required
615588
await provider.addInteraction({
616589
// The 'state' field specifies a "Provider State"

test/client/autosuggest.spec.ts renamed to __tests__/client/autosuggest.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('Autosuggest Client', () => {
3030
body: {} as never,
3131
})
3232
);
33-
client = AutosuggestClient.init(apiKey, config, transportSpy);
33+
client = AutosuggestClient.init(apiKey, config, transportSpy as never);
3434
});
3535

3636
it('should instantiate an Autosuggest Client instance', () => {

test/client/available-languages.spec.ts renamed to __tests__/client/available-languages.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ describe('Available Languages Client', () => {
2828
body: {} as never,
2929
})
3030
);
31-
client = AvailableLanguagesClient.init(apiKey, config, transportSpy);
31+
client = AvailableLanguagesClient.init(
32+
apiKey,
33+
config,
34+
transportSpy as never
35+
);
3236
});
3337

3438
it('should return instantiate an Available Languages Client instance', () => {

test/client/grid-section.spec.ts renamed to __tests__/client/grid-section.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('Grid Section Client', () => {
2929
body: {} as never,
3030
})
3131
);
32-
client = GridSectionClient.init(apiKey, config, transportSpy);
32+
client = GridSectionClient.init(apiKey, config, transportSpy as never);
3333
});
3434

3535
it('should return instantiate an Grid Section Client instance', () => {
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)