feat: navigate from root
[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 } = MatchersV3
16
17 describe("Siren Pact test", () => {
18   let provider
19
20   beforeEach(() => {
21     provider = new PactV3({
22       consumer: "Siren Order Provider",
23       provider: "Siren Order Service",
24       port: 9000,
25       dir: path.resolve(process.cwd(), "pacts")
26     })
27   })
28
29   it('deletes the first order using the delete action', () => {
30     provider
31       .uponReceiving("get root")
32       .withRequest({
33         path: "/",
34       })
35       .willRespondWith({
36         status: 200,
37         headers: {
38           'Content-Type': 'application/vnd.siren+json'
39         },
40         body: {
41           class:[ "representation"],
42           links:[{"rel":["orders"], "href":  url("http://localhost:9000", ["orders"]) }]
43         }
44       })
45       .uponReceiving("get all orders")
46       .withRequest({
47         path: "/orders",
48       })
49       .willRespondWith({
50         status: 200,
51         headers: {
52           'Content-Type': 'application/vnd.siren+json'
53         },
54         body: {
55           class: [ "entity" ],
56           entities: eachLike({
57             class: [ "entity" ],
58             rel: [ "item" ],
59             properties: {
60               "id": integer(1234)
61             },
62             links: [
63               {
64                 "rel": [ "self" ],
65                 "href": url("http://localhost:9000", ["orders", regex("\\d+", "1234")])
66               }
67             ],
68             "actions": [
69               {
70                 "name": "update",
71                 "method": "PUT",
72                 "href": url("http://localhost:9000", ["orders", regex("\\d+", "1234")])
73               },
74               {
75                 "name": "delete",
76                 "method": "DELETE",
77                 "href": url("http://localhost:9000", ["orders", regex("\\d+", "1234")])
78               }
79             ]
80           }, 2),
81           links: [
82             {
83               rel: [ "self" ],
84               href: url("http://localhost:9000", ["orders"])
85             }
86           ]
87         }
88       })
89       .uponReceiving("delete order")
90       .withRequest({
91         method: "DELETE",
92         path: regex("/orders/\\d+", "/orders/1234"),
93       })
94       .willRespondWith({
95         status: 200
96       })
97
98     return provider.executeTest(mockserver => {
99       return expect(deleteFirstOrder(mockserver.url)).to.eventually.be.true
100     })
101   })
102 })