From e522ccf0e58881aa89eac1ee0ca3b81a23184c1e Mon Sep 17 00:00:00 2001 From: Zach Bloomquist Date: Mon, 2 Mar 2020 10:48:43 -0500 Subject: [PATCH] update types + tests --- cli/types/index.d.ts | 25 +++++++--------- .../integration/dom/coordinates_spec.ts | 23 +++++++++----- .../cypress/integration/dom/elements_spec.ts | 18 +++++++---- .../cypress/integration/dom/jquery_spec.ts | 6 ++-- .../integration/dom/visibility_spec.ts | 30 +++++++++---------- 5 files changed, 57 insertions(+), 45 deletions(-) diff --git a/cli/types/index.d.ts b/cli/types/index.d.ts index 40ab030fb64a..243704a2ba6c 100644 --- a/cli/types/index.d.ts +++ b/cli/types/index.d.ts @@ -122,13 +122,9 @@ declare namespace Cypress { clear: (keys?: string[]) => void } - interface ViewportPosition { - top: number - left: number + interface ViewportPosition extends WindowPosition { right: number bottom: number - topCenter: number - leftCenter: number } interface WindowPosition { @@ -138,20 +134,20 @@ declare namespace Cypress { leftCenter: number } - interface ElementPositioning { + interface ElementPositioning extends ElementCoordinates { scrollTop: number scrollLeft: number - width: number - height: number - fromViewport: ViewportPosition - fromWindow: WindowPosition + fromElViewport: ViewportPosition + fromElWindow: WindowPosition + fromAutWindow: WindowPosition } interface ElementCoordinates { width: number height: number - fromViewport: ViewportPosition - fromWindow: WindowPosition + fromElViewport: ViewportPosition & { x: number, y: number } + fromElWindow: WindowPosition & { x: number, y: number } + fromAutWindow: WindowPosition & { x: number, y: number } } /** @@ -433,7 +429,7 @@ declare namespace Cypress { /** * Returns a boolean indicating whether an element is scrollable. */ - isScrollable(element: JQuery | HTMLElement): boolean + isScrollable(element: Window | JQuery | HTMLElement): boolean /** * Returns a boolean indicating whether an element currently has focus. */ @@ -445,7 +441,7 @@ declare namespace Cypress { /** * Returns a boolean indicating whether an element is attached to the DOM. */ - isAttached(element: JQuery | HTMLElement): boolean + isAttached(element: JQuery | HTMLElement | Window | Document): boolean isSelector(element: JQuery | HTMLElement, selector: JQuery.Selector): boolean /** * Returns a boolean indicating whether an element is a descendent of another element. @@ -467,6 +463,7 @@ declare namespace Cypress { * Returns a boolean indicating whether an object is a jQuery object. */ isJquery(obj: any): boolean + isInputType(element: JQuery | HTMLElement, type: string | string[]): boolean stringify(element: JQuery | HTMLElement, form: string): string getElements(element: JQuery): JQuery | HTMLElement[] getContainsSelector(text: string, filter?: string): JQuery.Selector diff --git a/packages/driver/test/cypress/integration/dom/coordinates_spec.ts b/packages/driver/test/cypress/integration/dom/coordinates_spec.ts index 74a2098f233b..ae95411674aa 100644 --- a/packages/driver/test/cypress/integration/dom/coordinates_spec.ts +++ b/packages/driver/test/cypress/integration/dom/coordinates_spec.ts @@ -1,6 +1,13 @@ -const $ = Cypress.$.bind(Cypress) +declare namespace Cypress { + interface cy { + state(key: 'document'): Document + } +} describe('src/dom/coordinates', () => { + const $ = Cypress.$.bind(Cypress) + let doc: Document + before(() => { return cy .visit('/fixtures/generic.html') @@ -10,9 +17,9 @@ describe('src/dom/coordinates', () => { }) beforeEach(function () { - this.doc = cy.state('document') + doc = cy.state('document') - $(this.doc.body).empty().html(this.body) + $(doc.body).empty().html(this.body) this.$button = $('') .appendTo(cy.$$('body')) @@ -24,8 +31,8 @@ describe('src/dom/coordinates', () => { it('returns the leftCenter and topCenter normalized', function () { const win = Cypress.dom.getWindowByElement(this.$button.get(0)) - const scrollY = Object.getOwnPropertyDescriptor(win, 'scrollY') - const scrollX = Object.getOwnPropertyDescriptor(win, 'scrollX') + const scrollY = Object.getOwnPropertyDescriptor(win, 'scrollY')! + const scrollX = Object.getOwnPropertyDescriptor(win, 'scrollX')! Object.defineProperty(win, 'scrollY', { value: 10, @@ -63,17 +70,17 @@ describe('src/dom/coordinates', () => { context('.getElementAtPointFromViewport', () => { it('returns same element based on x/y coords', function () { - expect(Cypress.dom.getElementAtPointFromViewport(this.doc, 100, 60)).to.eq(this.$button.get(0)) + expect(Cypress.dom.getElementAtPointFromViewport(doc, 100, 60)).to.eq(this.$button.get(0)) }) it('does not return if element is hidden', function () { this.$button.hide() - expect(Cypress.dom.getElementAtPointFromViewport(this.doc, 100, 60)).not.to.eq(this.$button.get(0)) + expect(Cypress.dom.getElementAtPointFromViewport(doc, 100, 60)).not.to.eq(this.$button.get(0)) }) it('returns null if no element was found', function () { - expect(Cypress.dom.getElementAtPointFromViewport(this.doc, 1e9, 1e9)).to.be.null + expect(Cypress.dom.getElementAtPointFromViewport(doc, 1e9, 1e9)).to.be.null }) }) diff --git a/packages/driver/test/cypress/integration/dom/elements_spec.ts b/packages/driver/test/cypress/integration/dom/elements_spec.ts index 70575505a850..d537f2d7b6fc 100644 --- a/packages/driver/test/cypress/integration/dom/elements_spec.ts +++ b/packages/driver/test/cypress/integration/dom/elements_spec.ts @@ -1,13 +1,19 @@ -const $ = Cypress.$.bind(Cypress) +declare namespace Cypress { + interface cy { + state(key: 'window'): Window + } +} describe('src/dom/elements', () => { + const $ = Cypress.$.bind(Cypress) + context('.isAttached', () => { beforeEach(() => { cy.visit('/fixtures/iframe-outer.html') }) it('no elements', () => { - const $el = $(null) + const $el = $(null!) expect(Cypress.dom.isAttached($el)).to.be.false }) @@ -68,9 +74,9 @@ describe('src/dom/elements', () => { it('element in iframe', (done) => { cy.get('iframe').then(($iframe) => { - const $doc = $iframe.contents() + const $doc = $iframe.contents() as JQuery - const $btn = $doc.find('button') + const $btn = $doc.find('button') as unknown as JQuery expect($btn.length).to.eq(1) @@ -84,7 +90,7 @@ describe('src/dom/elements', () => { done() }) - const win = $doc.get(0).defaultView + const win = $doc.get(0).defaultView! win.location.reload() }) @@ -93,7 +99,7 @@ describe('src/dom/elements', () => { context('.isDetached', () => { it('opposite of attached', () => { - const $el = $(null) + const $el = $(null!) expect(Cypress.dom.isDetached($el)).to.be.true }) diff --git a/packages/driver/test/cypress/integration/dom/jquery_spec.ts b/packages/driver/test/cypress/integration/dom/jquery_spec.ts index ce18f6cb7fe5..012288c5d3b8 100644 --- a/packages/driver/test/cypress/integration/dom/jquery_spec.ts +++ b/packages/driver/test/cypress/integration/dom/jquery_spec.ts @@ -1,4 +1,6 @@ -const { $ } = Cypress +declare interface Window { + jquery: Function +} describe('src/dom/jquery', () => { context('.isJquery', () => { @@ -9,7 +11,7 @@ describe('src/dom/jquery', () => { }) it('is true for actual jquery instances', () => { - expect(Cypress.dom.isJquery($(':first'))).to.be.true + expect(Cypress.dom.isJquery(Cypress.$(':first'))).to.be.true }) }) }) diff --git a/packages/driver/test/cypress/integration/dom/visibility_spec.ts b/packages/driver/test/cypress/integration/dom/visibility_spec.ts index 4bbcb1f28e3b..edfa306e84fb 100644 --- a/packages/driver/test/cypress/integration/dom/visibility_spec.ts +++ b/packages/driver/test/cypress/integration/dom/visibility_spec.ts @@ -1,19 +1,18 @@ -const $dom = Cypress.dom -const $ = Cypress.$.bind(Cypress) - describe('src/cypress/dom/visibility', () => { + const $ = Cypress.$.bind(Cypress) + beforeEach(() => { cy.visit('/fixtures/generic.html') }) context('isHidden', () => { it('exposes isHidden', () => { - expect($dom.isHidden).to.be.a('function') + expect(Cypress.dom.isHidden).to.be.a('function') }) it('throws when not passed a DOM element', () => { const fn = () => { - $dom.isHidden(null) + Cypress.dom.isHidden(null!) } expect(fn).to.throw('Cypress.dom.isHidden() failed because it requires a DOM element. The subject received was: \'null\'') @@ -22,12 +21,13 @@ describe('src/cypress/dom/visibility', () => { context('isVisible', () => { it('exposes isVisible', () => { - expect($dom.isVisible).to.be.a('function') + expect(Cypress.dom.isVisible).to.be.a('function') }) it('throws when not passed a DOM element', () => { const fn = () => { - $dom.isVisible('form') + // @ts-ignore + Cypress.dom.isVisible('form') } expect(fn).to.throw('Cypress.dom.isVisible() failed because it requires a DOM element. The subject received was: \'form\'') @@ -46,7 +46,7 @@ describe('src/cypress/dom/visibility', () => { const win = cy.state('window') const fn = () => { - return $dom.isScrollable(win) + return Cypress.dom.isScrollable(win) } expect(fn()).to.be.true @@ -58,7 +58,7 @@ describe('src/cypress/dom/visibility', () => { const win = cy.state('window') const fn = () => { - return $dom.isScrollable(win) + return Cypress.dom.isScrollable(win) } expect(fn()).to.be.false @@ -72,7 +72,7 @@ describe('src/cypress/dom/visibility', () => { `) const fn = () => { - return $dom.isScrollable(noScroll) + return Cypress.dom.isScrollable(noScroll) } expect(fn()).to.be.false @@ -88,7 +88,7 @@ describe('src/cypress/dom/visibility', () => { `) const fn = () => { - return $dom.isScrollable(noOverflow) + return Cypress.dom.isScrollable(noOverflow) } expect(fn()).to.be.false @@ -102,7 +102,7 @@ describe('src/cypress/dom/visibility', () => { `) const fn = () => { - return $dom.isScrollable(vertScrollable) + return Cypress.dom.isScrollable(vertScrollable) } expect(fn()).to.be.true @@ -116,7 +116,7 @@ describe('src/cypress/dom/visibility', () => { `) const fn = () => { - return $dom.isScrollable(horizScrollable) + return Cypress.dom.isScrollable(horizScrollable) } expect(fn()).to.be.true @@ -130,7 +130,7 @@ describe('src/cypress/dom/visibility', () => { `) const fn = () => { - return $dom.isScrollable(forcedScroll) + return Cypress.dom.isScrollable(forcedScroll) } expect(fn()).to.be.true @@ -971,7 +971,7 @@ describe('src/cypress/dom/visibility', () => { describe('#getReasonIsHidden', () => { beforeEach(function () { this.reasonIs = ($el, str) => { - expect($dom.getReasonIsHidden($el)).to.eq(str) + expect(Cypress.dom.getReasonIsHidden($el)).to.eq(str) } })