Skip to content

Commit

Permalink
update types + tests
Browse files Browse the repository at this point in the history
fix ts lint
  • Loading branch information
flotwig committed Mar 2, 2020
1 parent 2e87c00 commit c886346
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 42 deletions.
21 changes: 10 additions & 11 deletions cli/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -143,15 +139,17 @@ declare namespace Cypress {
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 }
}

/**
Expand Down Expand Up @@ -433,7 +431,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.
*/
Expand All @@ -445,7 +443,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.
Expand All @@ -467,6 +465,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
Expand Down
23 changes: 15 additions & 8 deletions packages/driver/test/cypress/integration/dom/coordinates_spec.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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 = $('<button style=\'position: absolute; top: 25px; left: 50px; width: 100px; line-height: 50px; padding: 10px; margin: 10px; border: 10px solid black\'>foo</button>')
.appendTo(cy.$$('body'))
Expand All @@ -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,
Expand Down Expand Up @@ -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
})
})

Expand Down
18 changes: 12 additions & 6 deletions packages/driver/test/cypress/integration/dom/elements_spec.ts
Original file line number Diff line number Diff line change
@@ -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
})
Expand Down Expand Up @@ -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<Document>

const $btn = $doc.find('button')
const $btn = $doc.find('button') as unknown as JQuery<HTMLButtonElement>

expect($btn.length).to.eq(1)

Expand All @@ -84,7 +90,7 @@ describe('src/dom/elements', () => {
done()
})

const win = $doc.get(0).defaultView
const win = $doc.get(0).defaultView!

win.location.reload()
})
Expand All @@ -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
})
Expand Down
6 changes: 4 additions & 2 deletions packages/driver/test/cypress/integration/dom/jquery_spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const { $ } = Cypress
declare interface Window {
jquery: Function
}

describe('src/dom/jquery', () => {
context('.isJquery', () => {
Expand All @@ -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
})
})
})
30 changes: 15 additions & 15 deletions packages/driver/test/cypress/integration/dom/visibility_spec.ts
Original file line number Diff line number Diff line change
@@ -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\'')
Expand All @@ -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\'')
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
})

Expand Down

0 comments on commit c886346

Please sign in to comment.