Skip to content

Commit 3791c6b

Browse files
committed
pact test
1 parent cb72318 commit 3791c6b

File tree

9 files changed

+1283
-11
lines changed

9 files changed

+1283
-11
lines changed

.pre-commit-config.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
repos:
4+
- repo: https://github.com/pre-commit/pre-commit-hooks
5+
rev: v3.2.0
6+
hooks:
7+
- id: trailing-whitespace
8+
- id: end-of-file-fixer
9+
- id: check-yaml
10+
- id: check-added-large-files

frontend/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@
6464
"tsx": "^4.19.1",
6565
"typescript": "^5.5.4",
6666
"wait-on": "^8.0.0",
67-
"whatwg-fetch": "^3.6.20"
67+
"whatwg-fetch": "^3.6.20",
68+
"@pact-foundation/pact": "13.1.4"
6869
},
6970
"overrides": {
7071
"pa11y-ci": {
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{
2+
"consumer": {
3+
"name": "FrontendWebsite"
4+
},
5+
"interactions": [
6+
{
7+
"description": "get all templates",
8+
"providerState": "templates exist",
9+
"request": {
10+
"method": "GET",
11+
"path": "/v1/templates"
12+
},
13+
"response": {
14+
"body": {
15+
"templates": [
16+
{
17+
"id": "id",
18+
"message": "message",
19+
"name": "name",
20+
"templateStatus": "SUBMITTED",
21+
"templateType": "NHS_APP"
22+
}
23+
]
24+
},
25+
"headers": {
26+
"Content-Type": "application/json; charset=utf-8"
27+
},
28+
"matchingRules": {
29+
"$.body": {
30+
"match": "type"
31+
}
32+
},
33+
"status": 200
34+
}
35+
},
36+
{
37+
"description": "get template with ID 10",
38+
"providerState": "template with ID 10 exists",
39+
"request": {
40+
"method": "GET",
41+
"path": "/v1/template/10"
42+
},
43+
"response": {
44+
"body": {
45+
"template": {
46+
"id": "id",
47+
"message": "message",
48+
"name": "name",
49+
"templateStatus": "NOT_YET_SUBMITTED",
50+
"templateType": "NHS_APP"
51+
}
52+
},
53+
"headers": {
54+
"Content-Type": "application/json; charset=utf-8"
55+
},
56+
"matchingRules": {
57+
"$.body": {
58+
"match": "type"
59+
}
60+
},
61+
"status": 200
62+
}
63+
}
64+
],
65+
"metadata": {
66+
"pact-js": {
67+
"version": "13.2.0"
68+
},
69+
"pactRust": {
70+
"ffi": "0.4.22",
71+
"models": "1.2.3"
72+
},
73+
"pactSpecification": {
74+
"version": "2.0.0"
75+
}
76+
},
77+
"provider": {
78+
"name": "ProductService"
79+
}
80+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{
2+
"consumer": {
3+
"name": "TemplateClient"
4+
},
5+
"interactions": [
6+
{
7+
"description": "get all templates",
8+
"providerState": "templates exist",
9+
"request": {
10+
"method": "GET",
11+
"path": "/v1/templates"
12+
},
13+
"response": {
14+
"body": {
15+
"templates": [
16+
{
17+
"id": "id",
18+
"message": "message",
19+
"name": "name",
20+
"templateStatus": "SUBMITTED",
21+
"templateType": "NHS_APP"
22+
}
23+
]
24+
},
25+
"headers": {
26+
"Content-Type": "application/json; charset=utf-8"
27+
},
28+
"matchingRules": {
29+
"$.body": {
30+
"match": "type"
31+
}
32+
},
33+
"status": 200
34+
}
35+
},
36+
{
37+
"description": "get template with ID 10",
38+
"providerState": "template with ID 10 exists",
39+
"request": {
40+
"method": "GET",
41+
"path": "/v1/template/10"
42+
},
43+
"response": {
44+
"body": {
45+
"template": {
46+
"id": "id",
47+
"message": "message",
48+
"name": "name",
49+
"templateStatus": "NOT_YET_SUBMITTED",
50+
"templateType": "NHS_APP"
51+
}
52+
},
53+
"headers": {
54+
"Content-Type": "application/json; charset=utf-8"
55+
},
56+
"matchingRules": {
57+
"$.body": {
58+
"match": "type"
59+
}
60+
},
61+
"status": 200
62+
}
63+
}
64+
],
65+
"metadata": {
66+
"pact-js": {
67+
"version": "13.2.0"
68+
},
69+
"pactRust": {
70+
"ffi": "0.4.22",
71+
"models": "1.2.3"
72+
},
73+
"pactSpecification": {
74+
"version": "2.0.0"
75+
}
76+
},
77+
"provider": {
78+
"name": "TemplateService"
79+
}
80+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import path from 'node:path';
2+
// eslint-disable-next-line import/no-extraneous-dependencies
3+
import {
4+
PactV3,
5+
MatchersV3,
6+
SpecificationVersion,
7+
} from '@pact-foundation/pact';
8+
import { TemplateClient } from 'nhs-notify-backend-client/src/template-api-client';
9+
import { TemplateStatus, TemplateType } from 'nhs-notify-backend-client';
10+
11+
const { eachLike, like } = MatchersV3;
12+
13+
const provider = new PactV3({
14+
consumer: 'TemplateClient',
15+
provider: 'TemplateService',
16+
// log: path.resolve(process.cwd(), 'logs', 'pact.log'),
17+
logLevel: 'warn',
18+
dir: path.resolve(process.cwd(), 'pacts'),
19+
spec: SpecificationVersion.SPECIFICATION_VERSION_V2,
20+
host: '127.0.0.1',
21+
port: 8080,
22+
});
23+
24+
const testToken = 'abc';
25+
process.env.API_BASE_URL = 'http://127.0.0.1:8080';
26+
describe('API Pact test', () => {
27+
describe('getting all templates', () => {
28+
test('templates exists', async () => {
29+
// set up Pact interactions
30+
await provider.addInteraction({
31+
states: [{ description: 'templates exist' }],
32+
uponReceiving: 'get all templates',
33+
withRequest: {
34+
method: 'GET',
35+
path: '/v1/templates',
36+
},
37+
willRespondWith: {
38+
status: 200,
39+
headers: {
40+
'Content-Type': 'application/json; charset=utf-8',
41+
},
42+
body: like({
43+
templates: [
44+
{
45+
id: 'id',
46+
name: 'name',
47+
message: 'message',
48+
templateStatus: TemplateStatus.SUBMITTED,
49+
templateType: TemplateType.NHS_APP,
50+
},
51+
],
52+
}),
53+
},
54+
});
55+
56+
await provider.executeTest(async (mockService) => {
57+
const client = TemplateClient(testToken);
58+
59+
// make request to Pact mock server
60+
const templates = await client.listTemplates();
61+
console.log('Returned Templates', templates);
62+
expect(templates.data[0]).toStrictEqual({
63+
id: 'id',
64+
name: 'name',
65+
message: 'message',
66+
templateStatus: TemplateStatus.SUBMITTED,
67+
templateType: TemplateType.NHS_APP,
68+
});
69+
});
70+
});
71+
});
72+
73+
describe('getting one template', () => {
74+
test('ID 10 exists', async () => {
75+
// set up Pact interactions
76+
await provider.addInteraction({
77+
states: [{ description: 'template with ID 10 exists' }],
78+
uponReceiving: 'get template with ID 10',
79+
withRequest: {
80+
method: 'GET',
81+
path: '/v1/template/10',
82+
},
83+
willRespondWith: {
84+
status: 200,
85+
headers: {
86+
'Content-Type': 'application/json; charset=utf-8',
87+
},
88+
body: like({
89+
template: {
90+
id: 'id',
91+
name: 'name',
92+
message: 'message',
93+
templateStatus: TemplateStatus.NOT_YET_SUBMITTED,
94+
templateType: TemplateType.NHS_APP,
95+
},
96+
}),
97+
},
98+
});
99+
100+
await provider.executeTest(async (mockService) => {
101+
const client = TemplateClient(testToken);
102+
103+
// make request to Pact mock server
104+
const response = await client.getTemplate('10');
105+
106+
expect(response.data).toStrictEqual({
107+
id: 'id',
108+
name: 'name',
109+
message: 'message',
110+
templateStatus: TemplateStatus.NOT_YET_SUBMITTED,
111+
templateType: TemplateType.NHS_APP,
112+
});
113+
});
114+
});
115+
});
116+
});

lambdas/backend-client/src/template-api-client.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ export class TemplateApiClient implements ITemplateClient {
6666
};
6767
}
6868

69+
console.log('get template', response.data.template.templateStatus);
70+
6971
return {
7072
data: response.data.template,
7173
};
@@ -82,6 +84,8 @@ export class TemplateApiClient implements ITemplateClient {
8284
};
8385
}
8486

87+
console.log('first template', response.data);
88+
8589
return {
8690
data: response.data.templates,
8791
};

0 commit comments

Comments
 (0)