/
ClientsConsumer.spec.js
99 lines (91 loc) · 2.94 KB
/
ClientsConsumer.spec.js
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
"use strict"
const { Matchers } = require("@pact-foundation/pact")
const { getClients, postClient } = require("../../../src/consumer")
describe("Clients Service", () => {
const GET_EXPECTED_BODY = [{
"firstName": "Lisa",
"lastName": "Simpson",
"age": 8,
"id": 1
},
{
"firstName": "Wonder",
"lastName": "Woman",
"age": 30,
"id": 2
},
{
"firstName": "Homer",
"lastName": "Simpson",
"age": 39,
"id": 3
}]
afterEach(() => provider.verify())
describe("GET Clients", () => {
beforeEach(() => {
const interaction = {
state: "i have a list of clients",
uponReceiving: "a request for all clients",
withRequest: {
method: "GET",
path: "/clients",
headers: {
Accept: "application/json, text/plain, */*",
},
},
willRespondWith: {
status: 200,
headers: {
"Content-Type": "application/json; charset=utf-8",
},
body: GET_EXPECTED_BODY,
},
}
return provider.addInteraction(interaction)
})
test("returns correct body, header and statusCode", async () => {
const response = await getClients()
expect(response.headers['content-type']).toBe("application/json; charset=utf-8")
expect(response.data).toEqual(GET_EXPECTED_BODY)
expect(response.status).toEqual(200)
})
})
const POST_BODY = {
firstName: "John",
lastName: "Smith",
age: 34
}
const POST_EXPECTED_BODY = {
firstName: POST_BODY.firstName,
lastName: POST_BODY.lastName,
age: POST_BODY.age,
id: 3
}
describe("POST Client", () => {
beforeEach(() => {
const interaction = {
state: "i create a new client",
uponReceiving: "a request to create client with firstname and lastname",
withRequest: {
method: "POST",
path: "/clients",
headers: {
"Content-Type": "application/json;charset=utf-8"
},
body: POST_BODY,
},
willRespondWith: {
status: 200,
body: Matchers.like(POST_EXPECTED_BODY).contents,
},
}
return provider.addInteraction(interaction)
})
test("returns correct body, header and statusCode", async () => {
const response = await postClient(POST_BODY)
console.log(response.data)
expect(response.data.id).toEqual(3)
expect(response.status).toEqual(200)
})
})
})