Skip to content

Commit

Permalink
Route typescript fix 1831 (#1874)
Browse files Browse the repository at this point in the history
* types: add object to routes, close #1831

* types: remove Response type from cy.route, it belongs only in cy.request

* add example to cy.route doc

* types: add PATCH method, same as pull request #1778
  • Loading branch information
bahmutov authored and brian-mann committed Jun 5, 2018
1 parent a77e725 commit 39b31fd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
9 changes: 6 additions & 3 deletions cli/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ declare const assert: Chai.AssertStatic
declare namespace Cypress {
type FileContents = string | any[] | object
type HistoryDirection = "back" | "forward"
type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "OPTIONS" | "HEAD" | "TRACE" | "CONNECT"
type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "OPTIONS" | "HEAD" | "TRACE" | "CONNECT" | "PATCH"
type RequestBody = string | object
type ViewportOrientation = "portrait" | "landscape"
type PrevSubject = "optional" | "element" | "document" | "window"
Expand Down Expand Up @@ -746,9 +746,12 @@ declare namespace Cypress {
* Use `cy.route()` to manage the behavior of network requests.
*
* @see https://on.cypress.io/route
* @example
* cy.server()
* cy.route('https://localhost:7777/users', [{id: 1, name: 'Pat'}])
*/
route(url: string | RegExp, response?: string | Response): Chainable<null>
route(method: string, url: string | RegExp, response?: string | Response): Chainable<null>
route(url: string | RegExp, response?: string | object): Chainable<null>
route(method: string, url: string | RegExp, response?: string | object): Chainable<null>
route(fn: () => RouteOptions): Chainable<null>
route(options: Partial<RouteOptions>): Chainable<null>

Expand Down
16 changes: 15 additions & 1 deletion cli/types/tests/kitchen-sink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,10 @@ describe('Kitchen Sink', function() {
expect(response).to.have.property('headers')
expect(response).to.have.property('duration')
})

// accepts method, including "PATCH"
cy.request('POST', 'http://somewhere.com', {foo: 'bar'})
cy.request('PATCH', 'http://somewhere.com', {foo: 'bar'})
})

it('cy.route() - route responses to matching requests', function() {
Expand Down Expand Up @@ -986,7 +990,7 @@ describe('Kitchen Sink', function() {
url: /comments\/\d+/,
status: 404,
response: { error: message },
delay: 500,
delay: 500
}).as('putComment')

// we have code that puts a comment when
Expand All @@ -998,6 +1002,16 @@ describe('Kitchen Sink', function() {
// our 404 statusCode logic in scripts.js executed
cy.get('.network-put-comment').should('contain', message)
})

it('has type for arbitrary response object', () => {
// https://github.com/cypress-io/cypress/issues/1831
const response = [{
id: 1,
name: 'Pat'
}]
cy.route('https://localhost:7777/users', response)
cy.route('GET', 'https://localhost:7777/more-users', response)
})
})

context('Files', function() {
Expand Down

0 comments on commit 39b31fd

Please sign in to comment.