This repository has been archived by the owner on May 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathclient-spec.js
149 lines (124 loc) · 3.99 KB
/
client-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
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
(function() {
describe("Client", function() {
var client, helloProvider;
// ugly but works... guess would be good to bring jasmine-beforeAll
beforeEach(function() {
client = example.createClient('http://localhost:1234');
helloProvider = Pact.mockService({
consumer: 'Hello Consumer',
provider: 'Hello Provider',
port: 1234,
done: function (error) {
expect(error).toBe(null);
}
});
});
describe("sayHello", function () {
it("should say hello", function(done) {
helloProvider
.uponReceiving("a request for hello")
.withRequest("get", "/sayHello")
.willRespondWith(200, {
"Content-Type": "application/json"
}, {
reply: "Hello"
});
//Run the tests
helloProvider.run(done, function(runComplete) {
expect(client.sayHello()).toEqual("Hello");
runComplete();
});
});
});
describe("findFriendsByAgeAndChildren", function () {
it("should return some friends", function(done) {
//Add interaction
helloProvider
.uponReceiving("a request friends")
.withRequest({
method: 'get',
path: '/friends',
query: {
age: Pact.Match.term({generate: '30', matcher: '\\d+'}), //remember query params are always strings
children: ['Mary Jane', 'James'] // specify params with multiple values in an array
},
headers: {
'Accept': 'application/json'
}
})
.willRespondWith({
status: 200,
headers: {
"Content-Type": "application/json"
},
body: {
friends: Pact.Match.eachLike({
name: Pact.Match.somethingLike('Sue') // Doesn't tie the Provider to a particular friend such as 'Sue'
}, {min: 1})
}
});
//Run the tests
helloProvider.run(done, function(runComplete) {
expect(client.findFriendsByAgeAndChildren('33', ['Mary Jane', 'James'])).toEqual([{
name: 'Sue'
}]);
runComplete();
});
});
});
describe("unfriendMe", function () {
it("should unfriend me", function(done) {
//Add interaction
helloProvider
.given("I am friends with Fred")
.uponReceiving("a request to unfriend")
.withRequest("put", "/unfriendMe")
.willRespondWith({
status: 200,
headers: {
"Content-Type": "application/json"
},
body: {
reply: "Bye"
}
});
//Run the tests
helloProvider.run(done, function(runComplete) {
function success(message) {
expect(message).toEqual("Bye");
runComplete();
}
function error(response) {
runComplete();
expect(true).toEqual(false);
}
client.unfriendMe(success, error);
});
});
describe("when there are no friends", function () {
it("returns an error message", function (done) {
//Add interaction
helloProvider
.given("I have no friends")
.uponReceiving("a request to unfriend")
.withRequest("put", "/unfriendMe")
.willRespondWith(404);
//Run the tests
helloProvider.run(done, function(runComplete) {
function success(message) {
//The success callback should *not* be invoked!
expect(true).toEqual(false);
runComplete();
}
function error(message) {
//The error callback *should* be invoked
expect(message).toEqual("No friends :(");
runComplete();
}
client.unfriendMe(success, error);
});
});
});
});
});
})();