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