988f84a6b678f7eb31f40bb2428c65a3616655fe
[demos/example-siren] / consumer / src / __tests__ / delete-order.spec.js
1 const path = require("path")
2 const chai = require("chai")
3 const chaiAsPromised = require("chai-as-promised")
4 const expect = chai.expect
5 const { PactV3, MatchersV3 } = require("@pact-foundation/pact/v3")
6 const { deleteFirstOrder } = require('../consumer')
7
8 chai.use(chaiAsPromised)
9
10 const {
11   eachLike,
12   url,
13   integer,
14   regex,
15   arrayContaining
16 } = MatchersV3
17
18 describe("Siren Pact test", () => {
19   let provider
20
21   beforeEach(() => {
22     provider = new PactV3({
23       consumer: "Siren Order Provider",
24       provider: "Siren Order Service",
25       port: 9000,
26       dir: path.resolve(process.cwd(), "pacts")
27     })
28   })
29
30   it('deletes the first order using the delete action', () => {
31     provider
32
33       // Get Root Request
34       .uponReceiving("get root")
35       .withRequest({
36         path: "/",
37       })
38       .willRespondWith({
39         status: 200,
40         headers: {
41           'Content-Type': 'application/vnd.siren+json'
42         },
43         body: {
44           class:[ "representation"],
45           links:[{"rel":["orders"], "href":  url("http://localhost:9000", ["orders"]) }]
46         }
47       })
48
49       // Get Orders Request
50       .uponReceiving("get all orders")
51       .withRequest({
52         path: "/orders",
53       })
54       .willRespondWith({
55         status: 200,
56         headers: {
57           'Content-Type': 'application/vnd.siren+json'
58         },
59         body: {
60           class: [ "entity" ],
61           entities: eachLike({
62             class: [ "entity" ],
63             rel: [ "item" ],
64             properties: {
65               "id": integer(1234)
66             },
67             links: [
68               {
69                 "rel": [ "self" ],
70                 "href": url("http://localhost:9000", ["orders", regex("\\d+", "1234")])
71               }
72             ],
73             "actions": arrayContaining(
74               {
75                 "name": "update",
76                 "method": "PUT",
77                 "href": url("http://localhost:9000", ["orders", regex("\\d+", "1234")])
78               },
79               {
80                 "name": "delete",
81                 "method": "DELETE",
82                 "href": url("http://localhost:9000", ["orders", regex("\\d+", "1234")])
83               }
84             )
85           }),
86           links: [
87             {
88               rel: [ "self" ],
89               href: url("http://localhost:9000", ["orders"])
90             }
91           ]
92         }
93       })
94
95       // Delete Order Request
96       .uponReceiving("delete order")
97       .withRequest({
98         method: "DELETE",
99         path: regex("/orders/\\d+", "/orders/1234"),
100       })
101       .willRespondWith({
102         status: 200
103       })
104
105     return provider.executeTest(mockserver => {
106       return expect(deleteFirstOrder(mockserver.url)).to.eventually.be.true
107     })
108   })
109 })