Skip to content

Commit

Permalink
TS: separate Window type for application under test (#7806)
Browse files Browse the repository at this point in the history
Co-authored-by: Oliver Joseph Ash <[email protected]>
  • Loading branch information
sainthkh and OliverJAsh authored Jul 6, 2020
1 parent 2e35094 commit 9e754d5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 15 deletions.
34 changes: 22 additions & 12 deletions cli/types/cypress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ declare namespace Cypress {
fromAutWindow: WindowPosition & { x: number, y: number }
}

/**
* Window type for Application Under Test(AUT)
*/
type AUTWindow = Window & typeof globalThis & ApplicationWindow

/**
* The interface for user-defined properties in Window object under test.
*/
interface ApplicationWindow {} // tslint:disable-line

/**
* Several libraries are bundled with Cypress by default.
*
Expand Down Expand Up @@ -1038,7 +1048,7 @@ declare namespace Cypress {
*
* @see https://on.cypress.io/go
*/
go(direction: HistoryDirection | number, options?: Partial<Loggable & Timeoutable>): Chainable<Window>
go(direction: HistoryDirection | number, options?: Partial<Loggable & Timeoutable>): Chainable<AUTWindow>

/**
* Get the current URL hash of the page that is currently active.
Expand Down Expand Up @@ -1375,7 +1385,7 @@ declare namespace Cypress {
* @example
* cy.reload()
*/
reload(options?: Partial<Loggable & Timeoutable>): Chainable<Window>
reload(options?: Partial<Loggable & Timeoutable>): Chainable<AUTWindow>
/**
* Reload the page without cache
*
Expand All @@ -1386,7 +1396,7 @@ declare namespace Cypress {
* cy.visit('http://localhost:3000/admin')
* cy.reload(true)
*/
reload(forceReload: boolean): Chainable<Window>
reload(forceReload: boolean): Chainable<AUTWindow>

/**
* Make an HTTP GET request.
Expand Down Expand Up @@ -1929,8 +1939,8 @@ declare namespace Cypress {
* })
*
*/
visit(url: string, options?: Partial<VisitOptions>): Chainable<Window>
visit(options: Partial<VisitOptions> & { url: string }): Chainable<Window>
visit(url: string, options?: Partial<VisitOptions>): Chainable<AUTWindow>
visit(options: Partial<VisitOptions> & { url: string }): Chainable<AUTWindow>

/**
* Wait for a number of milliseconds.
Expand Down Expand Up @@ -2001,7 +2011,7 @@ declare namespace Cypress {
})
```
*/
window(options?: Partial<Loggable & Timeoutable>): Chainable<Window>
window(options?: Partial<Loggable & Timeoutable>): Chainable<AUTWindow>

/**
* Scopes all subsequent cy commands to within this element.
Expand Down Expand Up @@ -2715,16 +2725,16 @@ declare namespace Cypress {
/**
* Called before your page has loaded all of its resources.
*
* @param {Window} contentWindow the remote page's window object
* @param {AUTWindow} contentWindow the remote page's window object
*/
onBeforeLoad(win: Window): void
onBeforeLoad(win: AUTWindow): void

/**
* Called once your page has fired its load event.
*
* @param {Window} contentWindow the remote page's window object
* @param {AUTWindow} contentWindow the remote page's window object
*/
onLoad(win: Window): void
onLoad(win: AUTWindow): void

/**
* Cypress will automatically apply the right authorization headers
Expand Down Expand Up @@ -4632,12 +4642,12 @@ declare namespace Cypress {
* Fires as the page begins to load, but before any of your applications JavaScript has executed. This fires at the exact same time as `cy.visit()` `onBeforeLoad` callback. Useful to modify the window on a page transition.
* @see https://on.cypress.io/catalog-of-events#App-Events
*/
(action: 'window:before:load', fn: (win: Window) => void): void
(action: 'window:before:load', fn: (win: AUTWindow) => void): void
/**
* Fires after all your resources have finished loading after a page transition. This fires at the exact same time as a `cy.visit()` `onLoad` callback.
* @see https://on.cypress.io/catalog-of-events#App-Events
*/
(action: 'window:load', fn: (win: Window) => void): void
(action: 'window:load', fn: (win: AUTWindow) => void): void
/**
* Fires when your application is about to navigate away. The real event object is provided to you. Your app may have set a `returnValue` on the event, which is useful to assert on.
* @see https://on.cypress.io/catalog-of-events#App-Events
Expand Down
2 changes: 1 addition & 1 deletion cli/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Mike Woudenberg <https://github.com/mikewoudenberg>
// Robbert van Markus <https://github.com/rvanmarkus>
// Nicholas Boll <https://github.com/nicholasboll>
// TypeScript Version: 3.0
// TypeScript Version: 3.4
// Updated by the Cypress team: https://www.cypress.io/about/

/// <reference path="./cy-blob-util.d.ts" />
Expand Down
4 changes: 2 additions & 2 deletions cli/types/tests/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ Cypress.on('window:alert', (text) => {
})

Cypress.on('window:before:load', (win) => {
win // $ExpectType Window
win // $ExpectType AUTWindow
})

Cypress.on('window:load', (win) => {
win // $ExpectType Window
win // $ExpectType AUTWindow
})

Cypress.on('window:before:unload', (event) => {
Expand Down
18 changes: 18 additions & 0 deletions cli/types/tests/cypress-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,24 @@ cy
subject // $ExpectType undefined
})

namespace CypressAUTWindowTests {
cy.go(2).then((win) => {
win // $ExpectType AUTWindow
})

cy.reload().then((win) => {
win // $ExpectType AUTWindow
})

cy.visit('https://google.com').then(win => {
win // $ExpectType AUTWindow
})

cy.window().then(win => {
win // $ExpectType AUTWindow
})
}

namespace CypressOnTests {
Cypress.on('uncaught:exception', (error, runnable) => {
error // $ExpectType Error
Expand Down
6 changes: 6 additions & 0 deletions cli/types/tests/kitchen-sink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,9 @@ namespace BlobTests {
dateUrl // $ExpectType string
})
}

cy.window().then(window => {
window // $ExpectType AUTWindow

window.eval('1')
})

0 comments on commit 9e754d5

Please sign in to comment.