Skip to content

Commit

Permalink
feat: pact-web provider.addInteraction to allow Interaction instance [#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Wei Jie committed Feb 22, 2019
1 parent 6acc1d8 commit 0814d68
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
30 changes: 29 additions & 1 deletion src/pact-web.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as chaiAsPromised from "chai-as-promised"
import * as sinon from "sinon"
import * as sinonChai from "sinon-chai"
import { HTTPMethod } from "./common/request"
import { InteractionObject } from "./dsl/interaction"
import { Interaction, InteractionObject } from "./dsl/interaction"
import { MockService } from "./dsl/mockService"
import { PactOptionsComplete } from "./dsl/options"
import { PactWeb } from "./pact-web"
Expand Down Expand Up @@ -98,6 +98,34 @@ describe("PactWeb", () => {
.to.eventually.not.have.property("providerState")
.notify(done)
})

describe("when given an Interaction as a builder", () => {
it("creates interaction", () => {
const interaction2 = new Interaction()
.given("i have a list of projects")
.uponReceiving("a request for projects")
.withRequest({
method: HTTPMethod.GET,
path: "/projects",
headers: { Accept: "application/json" },
})
.willRespondWith({
status: 200,
headers: { "Content-Type": "application/json" },
body: {},
})

const pact = (Object.create(PactWeb.prototype) as any) as PactWeb
pact.opts = fullOpts
pact.mockService = ({
addInteraction: (int: Interaction): Promise<Interaction> =>
Promise.resolve(int),
} as any) as MockService
return expect(
pact.addInteraction(interaction2)
).to.eventually.have.property("given")
})
})
})
})

Expand Down
7 changes: 5 additions & 2 deletions src/pact-web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ export class PactWeb {
* @param {Interaction} interactionObj
* @returns {Promise}
*/
public addInteraction(interactionObj: InteractionObject): Promise<string> {
const interaction = new Interaction()
public addInteraction(interactionObj: InteractionObject | Interaction): Promise<string> {
if (interactionObj instanceof Interaction) {
return this.mockService.addInteraction(interactionObj)
}

const interaction = new Interaction()
if (interactionObj.state) {
interaction.given(interactionObj.state)
}
Expand Down

0 comments on commit 0814d68

Please sign in to comment.