chore: update readme
[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
32       // Get Root Request
33       .uponReceiving("get root")
34       .withRequest({
35         path: "/",
36       })
37       .willRespondWith({
38         status: 200,
39         headers: {
40           'Content-Type': 'application/vnd.siren+json'
41         },
42         body: {
43           class:[ "representation"],
44           links:[{"rel":["orders"], "href":  url("http://localhost:9000", ["orders"]) }]
45         }
46       })
47
48       // Get Orders Request
49       .uponReceiving("get all orders")
50       .withRequest({
51         path: "/orders",
52       })
53       .willRespondWith({
54         status: 200,
55         headers: {
56           'Content-Type': 'application/vnd.siren+json'
57         },
58         body: {
59           class: [ "entity" ],
60           entities: eachLike({
61             class: [ "entity" ],
62             rel: [ "item" ],
63             properties: {
64               "id": integer(1234)
65             },
66             links: [
67               {
68                 "rel": [ "self" ],
69                 "href": url("http://localhost:9000", ["orders", regex("\\d+", "1234")])
70               }
71             ],
72             "actions": [
73               {
74                 "name": "update",
75                 "method": "PUT",
76                 "href": url("http://localhost:9000", ["orders", regex("\\d+", "1234")])
77               },
78               {
79                 "name": "delete",
80                 "method": "DELETE",
81                 "href": url("http://localhost:9000", ["orders", regex("\\d+", "1234")])
82               }
83             ]
84           }, 2),
85           links: [
86             {
87               rel: [ "self" ],
88               href: url("http://localhost:9000", ["orders"])
89             }
90           ]
91         }
92       })
93
94       // Delete Order Request
95       .uponReceiving("delete order")
96       .withRequest({
97         method: "DELETE",
98         path: regex("/orders/\\d+", "/orders/1234"),
99       })
100       .willRespondWith({
101         status: 200
102       })
103
104     return provider.executeTest(mockserver => {
105       return expect(deleteFirstOrder(mockserver.url)).to.eventually.be.true
106     })
107   })
108 })