From 9e754d526b01a090c11fb692ad93360d3132eb93 Mon Sep 17 00:00:00 2001 From: Kukhyeon Heo Date: Mon, 6 Jul 2020 22:38:02 +0900 Subject: [PATCH] TS: separate Window type for application under test (#7806) Co-authored-by: Oliver Joseph Ash --- cli/types/cypress.d.ts | 34 +++++++++++++++++++++----------- cli/types/index.d.ts | 2 +- cli/types/tests/actions.ts | 4 ++-- cli/types/tests/cypress-tests.ts | 18 +++++++++++++++++ cli/types/tests/kitchen-sink.ts | 6 ++++++ 5 files changed, 49 insertions(+), 15 deletions(-) diff --git a/cli/types/cypress.d.ts b/cli/types/cypress.d.ts index affe28640cd6..380e89e3cace 100644 --- a/cli/types/cypress.d.ts +++ b/cli/types/cypress.d.ts @@ -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. * @@ -1038,7 +1048,7 @@ declare namespace Cypress { * * @see https://on.cypress.io/go */ - go(direction: HistoryDirection | number, options?: Partial): Chainable + go(direction: HistoryDirection | number, options?: Partial): Chainable /** * Get the current URL hash of the page that is currently active. @@ -1375,7 +1385,7 @@ declare namespace Cypress { * @example * cy.reload() */ - reload(options?: Partial): Chainable + reload(options?: Partial): Chainable /** * Reload the page without cache * @@ -1386,7 +1396,7 @@ declare namespace Cypress { * cy.visit('http://localhost:3000/admin') * cy.reload(true) */ - reload(forceReload: boolean): Chainable + reload(forceReload: boolean): Chainable /** * Make an HTTP GET request. @@ -1929,8 +1939,8 @@ declare namespace Cypress { * }) * */ - visit(url: string, options?: Partial): Chainable - visit(options: Partial & { url: string }): Chainable + visit(url: string, options?: Partial): Chainable + visit(options: Partial & { url: string }): Chainable /** * Wait for a number of milliseconds. @@ -2001,7 +2011,7 @@ declare namespace Cypress { }) ``` */ - window(options?: Partial): Chainable + window(options?: Partial): Chainable /** * Scopes all subsequent cy commands to within this element. @@ -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 @@ -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 diff --git a/cli/types/index.d.ts b/cli/types/index.d.ts index 8da316edd19d..64600eb51430 100644 --- a/cli/types/index.d.ts +++ b/cli/types/index.d.ts @@ -4,7 +4,7 @@ // Mike Woudenberg // Robbert van Markus // Nicholas Boll -// TypeScript Version: 3.0 +// TypeScript Version: 3.4 // Updated by the Cypress team: https://www.cypress.io/about/ /// diff --git a/cli/types/tests/actions.ts b/cli/types/tests/actions.ts index ae407691ce85..b022c1a8af6a 100644 --- a/cli/types/tests/actions.ts +++ b/cli/types/tests/actions.ts @@ -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) => { diff --git a/cli/types/tests/cypress-tests.ts b/cli/types/tests/cypress-tests.ts index ccc8e1cba734..75a5c01ba75a 100644 --- a/cli/types/tests/cypress-tests.ts +++ b/cli/types/tests/cypress-tests.ts @@ -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 diff --git a/cli/types/tests/kitchen-sink.ts b/cli/types/tests/kitchen-sink.ts index ae6ae2b5c9d8..85a1509d2dfc 100644 --- a/cli/types/tests/kitchen-sink.ts +++ b/cli/types/tests/kitchen-sink.ts @@ -142,3 +142,9 @@ namespace BlobTests { dateUrl // $ExpectType string }) } + +cy.window().then(window => { + window // $ExpectType AUTWindow + + window.eval('1') +})