Skip to content

Commit

Permalink
Merge 3d2e360 into 3d2e7dd
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyJones committed May 3, 2020
2 parents 3d2e7dd + 3d2e360 commit efd8d96
Show file tree
Hide file tree
Showing 8 changed files with 321 additions and 211 deletions.
55 changes: 0 additions & 55 deletions examples/jest/__tests__/catAPI.spec.js

This file was deleted.

110 changes: 77 additions & 33 deletions examples/jest/__tests__/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,95 @@
"use strict"

const getMeDogs = require("../index").getMeDogs
const { pactWith } = require("jest-pact")
const { Matchers } = require("@pact-foundation/pact")

describe("Dog's API", () => {
let url = "http://localhost"
const { getMeDogs, getMeCats } = require("../index")

const EXPECTED_BODY = [
{
dog: 1,
},
]
pactWith({ consumer: "MyConsumer", provider: "MyProvider" }, provider => {
describe("Dogs API", () => {
const DOGS_DATA = [
{
dog: 1,
},
]

const dogsSuccessResponse = {
status: 200,
headers: {
"Content-Type": "application/json",
},
body: DOGS_DATA,
}

const dogsListRequest = {
uponReceiving: "a request for dogs",
withRequest: {
method: "GET",
path: "/dogs",
headers: {
Accept: "application/json",
},
},
}

describe("works", () => {
beforeEach(() => {
const interaction = {
state: "i have a list of projects",
uponReceiving: "a request for projects",
withRequest: {
method: "GET",
path: "/dogs",
headers: {
Accept: "application/json",
},
},
willRespondWith: {
status: 200,
headers: {
"Content-Type": "application/json",
},
body: EXPECTED_BODY,
},
state: "i have a list of dogs",
...dogsListRequest,
willRespondWith: dogsSuccessResponse,
}
return provider.addInteraction(interaction)
})

// add expectations
it("returns a sucessful body", () => {
return getMeDogs({
url,
port,
url: provider.mockService.baseUrl,
}).then(dogs => {
expect(dogs).toEqual(DOGS_DATA)
})
})
})

describe("Cats API", () => {
const CATS_DATA = [{ cat: 2 }, { cat: 3 }]

const catsSuccessResponse = {
status: 200,
headers: {
"Content-Type": "application/json",
},
body: CATS_DATA,
}

const catsListRequest = {
uponReceiving: "a request for cats with given catId",
withRequest: {
method: "GET",
path: "/cats",
query: {
"catId[]": Matchers.eachLike("1"),
},
headers: {
Accept: "application/json",
},
},
}

beforeEach(() => {
return provider.addInteraction({
state: "i have a list of cats",
...catsListRequest,
willRespondWith: catsSuccessResponse,
})
})

it("returns a sucessful body", () => {
return getMeCats({
url: provider.mockService.baseUrl,
}).then(cats => {
expect(cats).toEqual(CATS_DATA)
})
.then(response => {
expect(response.headers["content-type"]).toEqual("application/json")
expect(response.data).toEqual(EXPECTED_BODY)
expect(response.status).toEqual(200)
})
.then(() => provider.verify())
})
})
})
89 changes: 0 additions & 89 deletions examples/jest/__tests__/multipleSpecs.spec.js

This file was deleted.

30 changes: 16 additions & 14 deletions examples/jest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ const axios = require("axios")

exports.getMeDogs = endpoint => {
const url = endpoint.url
const port = endpoint.port

return axios.request({
method: "GET",
baseURL: `${url}:${port}`,
url: "/dogs",
headers: { Accept: "application/json" },
})
return axios
.request({
method: "GET",
baseURL: url,
url: "/dogs",
headers: { Accept: "application/json" },
})
.then(response => response.data)
}

exports.getMeCats = endpoint => {
const url = endpoint.url
const port = endpoint.port

return axios.request({
method: "GET",
baseURL: `${url}:${port}`,
url: "/cats?catId[]=2&catId[]=3",
headers: { Accept: "application/json" },
})
return axios
.request({
method: "GET",
baseURL: url,
url: "/cats?catId[]=2&catId[]=3",
headers: { Accept: "application/json" },
})
.then(response => response.data)
}
Loading

0 comments on commit efd8d96

Please sign in to comment.