Skip to content

Commit

Permalink
Alternative approach using extractPayload
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonello Caboni committed Jun 4, 2019
1 parent b09545b commit d32add9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 45 deletions.
54 changes: 9 additions & 45 deletions examples/typescript/test/get-dog.spec.ts
Expand Up @@ -3,13 +3,13 @@ import * as chai from "chai"
import * as chaiAsPromised from "chai-as-promised"
import path = require("path")
import * as sinonChai from "sinon-chai"
import { Pact, Interaction, Matchers } from "../../../src/pact"
import { Dog, Kennel } from "./kennel";
import { Pact, Interaction } from "../../../src/pact"
import { Kennel } from "./kennel";

const expect = chai.expect
import { DogService } from "../index"
import { like } from "../../../src/dsl/matchers";
const { eachLike } = Matchers
import { like, eachLike } from "./matchers";
import { extractPayload } from "../../../src/dsl/matchers";

chai.use(sinonChai)
chai.use(chaiAsPromised)
Expand All @@ -28,17 +28,11 @@ describe("The Dog API", () => {
pactfileWriteMode: "merge",
})

const kennel: Kennel =
const kennel: Kennel = like(
{
name: "my kennel",
dogs: [{name: "my dog"}]
};

const kennelWithMatchers = like(
{
...kennel,
dogs: eachLike(kennel.dogs[0])
});
dogs: eachLike({name: "my dog"})
});

before(() =>
provider.setup().then(opts => {
Expand Down Expand Up @@ -67,45 +61,15 @@ describe("The Dog API", () => {
headers: {
"Content-Type": "application/json",
},
body: kennelWithMatchers,
body: kennel,
})

return provider.addInteraction(interaction)
})

it("returns the correct response", done => {
dogService.getMeDogs().then((response: any) => {
expect(response.data[0]).to.deep.eq(kennel)
done()
}, done)
})
})

describe("get /dogs using object pattern", () => {
before(() => {
return provider.addInteraction({
state: "i have a list of dogs",
uponReceiving: "a request for all dogs with the object pattern",
withRequest: {
method: "GET",
path: "/dogs",
headers: {
Accept: "application/json",
},
},
willRespondWith: {
status: 200,
headers: {
"Content-Type": "application/json",
},
body: EXPECTED_BODY,
},
})
})

it("returns the correct response", done => {
dogService.getMeDogs().then((response: any) => {
expect(response.data[0]).to.deep.eq(dogExample)
expect(response.data[0]).to.deep.eq(extractPayload(kennel))
done()
}, done)
})
Expand Down
9 changes: 9 additions & 0 deletions examples/typescript/test/matchers.ts
@@ -0,0 +1,9 @@
import { like as pactLike, eachLike as pactEachLike } from '@pact-foundation/pact/dsl/matchers';

export function like<T>(value: T): T {
return pactLike(value) as unknown as T;
}

export function eachLike<T>(value: T): T[] {
return pactEachLike(value) as unknown as T[];
}

0 comments on commit d32add9

Please sign in to comment.