diff --git a/packages/driver/test/cypress/integration/commands/xhr_spec.coffee b/packages/driver/test/cypress/integration/commands/xhr_spec.coffee index e33ac3b0e32e..bf79dfbb34f9 100644 --- a/packages/driver/test/cypress/integration/commands/xhr_spec.coffee +++ b/packages/driver/test/cypress/integration/commands/xhr_spec.coffee @@ -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 @@ -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") @@ -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