Skip to content

Commit

Permalink
add test for matcher receiving request body
Browse files Browse the repository at this point in the history
  • Loading branch information
flotwig committed May 7, 2019
1 parent c992bdd commit 5e6e23a
Showing 1 changed file with 79 additions and 52 deletions.
131 changes: 79 additions & 52 deletions packages/driver/test/cypress/integration/commands/xhr_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1204,58 +1204,6 @@ describe "src/cy/commands/xhr", ->
response: {}
})

it "accepts matcher option", ->
matcher = () => true
response = { foo: true }

cy.route({
matcher,
response,
}).then ->
@expectOptionsToBe({
matcher,
status: 200,
response
})

it "throws an error when matcher option is not a function", ->
cy.route({
matcher: 'foo',
});
cy.on "fail", (err) -> expect(err.message).to.eq(
"cy.route() was called with an invalid matcher option: matcher must be a Function that returns a Boolean."
)

it "passes both xhr and options arguments to matcher option function", ->
cy
.window()
.then (win) ->
options = {
matcher: (xhr, _options) =>
expect(xhr instanceof win.XMLHttpRequest).to.be.true
expect(_options.matcher).to.eq(options.matcher)
expect(_options.response).to.eq(options.response)
response: 'foo',
}
cy
.route(options)
.window().then (win) ->
win.$.get("/foo")

it "ignores url when matcher option is present", ->
cy
.route({
url: "/bar"
matcher: (xhr, options) => /foo/.test(xhr.url)
response: 'foo',
}).as("getFoo")
.window().then (win) ->
win.$.get("/bar").catch((response) -> expect(response.status == 404))
.window().then (win) ->
win.$.get("/foo")
.wait("@getFoo").then (xhr) ->
expect(xhr.responseBody).to.eq 'foo'

## FIXME
it.skip "can explicitly done() in onRequest function from options", (done) ->
cy
Expand Down Expand Up @@ -1384,6 +1332,77 @@ describe "src/cy/commands/xhr", ->
url: /foo/
respond: false

describe "matcher option", ->
it "accepts matcher option", ->
matcher = () => true
response = { foo: true }

cy.route({
matcher,
response,
}).then ->
@expectOptionsToBe({
matcher,
status: 200,
response
})

it "passes both xhr and options arguments to matcher option function", ->
cy
.window()
.then (win) ->
options = {
matcher: (xhr, _options) =>
expect(xhr instanceof win.XMLHttpRequest).to.be.true
expect(_options.matcher).to.eq(options.matcher)
expect(_options.response).to.eq(options.response)
response: 'foo',
}
cy
.route(options)
.window().then (win) ->
win.$.get("/foo")

it "receives request body in matcher", ->
options = {
matcher: (xhr) =>
debugger
expect(xhr.body).to.eq('baz')
}

cy
.route(options)
.window().then (win) ->
win.$.post("/foo", "baz")

it "ignores url when matcher option is present", ->
cy
.route({
url: "/bar"
matcher: (xhr, options) => /foo/.test(xhr.url)
response: 'foo',
}).as("getFoo")
.window().then (win) ->
win.$.get("/bar").catch((response) -> expect(response.status == 404))
.window().then (win) ->
win.$.get("/foo")
.wait("@getFoo").then (xhr) ->
expect(xhr.responseBody).to.eq 'foo'

it "ignores url when matcher option is present", ->
cy
.route({
url: "/bar"
matcher: (xhr, options) => /foo/.test(xhr.url)
response: 'foo',
}).as("getFoo")
.window().then (win) ->
win.$.get("/bar").catch((response) -> expect(response.status == 404))
.window().then (win) ->
win.$.get("/foo")
.wait("@getFoo").then (xhr) ->
expect(xhr.responseBody).to.eq 'foo'

describe "deprecations", ->
beforeEach ->
@warn = cy.spy(window.top.console, "warn")
Expand Down Expand Up @@ -1494,6 +1513,14 @@ describe "src/cy/commands/xhr", ->

cy.route(getUrl)

it "fails when matcher option is not a function", ->
cy.route({
matcher: 'foo',
})

cy.on "fail", (err) ->
expect(err.message).to.eq "cy.route() was called with an invalid matcher option: matcher must be a Function that returns a Boolean."

it "fails when functions reject", (done) ->
error = new Error

Expand Down

0 comments on commit 5e6e23a

Please sign in to comment.