Skip to content

Commit

Permalink
misc: update a few cypress types + convert internal driver tests to T…
Browse files Browse the repository at this point in the history
…ypeScript (cypress-io#31055)

* misc: update a few cypress types + convert internal driver tests to TypeScript

* Update packages/driver/cypress/e2e/commands/actions/scroll.cy.ts

* add changelog entry

* bump circle cache?

* More type fixes

* fix some more

* Make the values numbers

* Update cli/CHANGELOG.md

Co-authored-by: Bill Glesias <[email protected]>

---------

Co-authored-by: Bill Glesias <[email protected]>
  • Loading branch information
jennifer-shehane and AtofStryker authored Feb 17, 2025
1 parent a224a79 commit 5bf4273
Show file tree
Hide file tree
Showing 29 changed files with 869 additions and 452 deletions.
4 changes: 4 additions & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ _Released 2/11/2025_
- Upgraded `@cypress/request` from `3.0.6` to `3.0.7`. Addressed in [#31063](https://github.com/cypress-io/cypress/pull/31063).
- Upgraded `compression` from `1.7.4` to `1.7.5`. Addressed in [#31004](https://github.com/cypress-io/cypress/pull/31004).

**Misc:**

- Updated types around `.readFile()` and `.scrollTo()` arguments and `Cypress.dom` methods. Addressed in [#31055](https://github.com/cypress-io/cypress/pull/31055).

## 14.0.2

_Released 2/05/2025_
Expand Down
37 changes: 20 additions & 17 deletions cli/types/cypress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -756,8 +756,10 @@ declare namespace Cypress {
getCoordsByPosition(left: number, top: number, xPosition?: string, yPosition?: string): number
getElementPositioning(element: JQuery | HTMLElement): ElementPositioning
getElementAtPointFromViewport(doc: Document, x: number, y: number): Element | null
getElementCoordinatesByPosition(element: JQuery | HTMLElement, position: string): ElementCoordinates
getElementCoordinatesByPosition(element: JQuery | HTMLElement, position?: string): ElementCoordinates
getElementCoordinatesByPositionRelativeToXY(element: JQuery | HTMLElement, x: number, y: number): ElementPositioning
getHostContenteditable(element: HTMLElement): HTMLElement
getSelectionBounds(element: HTMLElement): { start: number, end: number }
}

/**
Expand Down Expand Up @@ -874,7 +876,7 @@ declare namespace Cypress {
* // Check first radio element
* cy.get('[type="radio"]').first().check()
*/
check(options?: Partial<CheckOptions>): Chainable<Subject>
check(options?: Partial<CheckClearOptions>): Chainable<Subject>
/**
* Check checkbox(es) or radio(s). This element must be an `<input>` with type `checkbox` or `radio`.
*
Expand All @@ -885,7 +887,7 @@ declare namespace Cypress {
* // Check the checkboxes with the values 'ga' and 'ca'
* cy.get('[type="checkbox"]').check(['ga', 'ca'])
*/
check(value: string | string[], options?: Partial<CheckOptions>): Chainable<Subject>
check(value: string | string[], options?: Partial<CheckClearOptions>): Chainable<Subject>

/**
* Get the children of each DOM element within a set of DOM elements.
Expand All @@ -902,7 +904,7 @@ declare namespace Cypress {
*
* @see https://on.cypress.io/clear
*/
clear(options?: Partial<ClearOptions>): Chainable<Subject>
clear(options?: Partial<CheckClearOptions>): Chainable<Subject>

/**
* Clear a specific browser cookie for a domain.
Expand Down Expand Up @@ -1806,7 +1808,7 @@ declare namespace Cypress {
*
* @see https://on.cypress.io/readfile
*/
readFile<Contents = any>(filePath: string, options?: Partial<Loggable & Timeoutable>): Chainable<Contents>
readFile<Contents = any>(filePath: string, options?: Partial<ReadFileOptions & Loggable & Timeoutable>): Chainable<Contents>
/**
* Read a file with given encoding and yield its contents.
*
Expand Down Expand Up @@ -1930,7 +1932,7 @@ declare namespace Cypress {
*
* @see https://on.cypress.io/scrollto
*/
scrollTo(position: PositionType, options?: Partial<ScrollToOptions>): Chainable<Subject>
scrollTo(positionOrX: PositionType | number | string, options?: Partial<ScrollToOptions>): Chainable<Subject>
/**
* Scroll to a specific X,Y position.
*
Expand Down Expand Up @@ -2326,7 +2328,7 @@ declare namespace Cypress {
* // Uncheck the checkbox with the value of 'ga'
* cy.get('input[type="checkbox"]').uncheck(['ga'])
*/
uncheck(options?: Partial<CheckOptions>): Chainable<Subject>
uncheck(options?: Partial<CheckClearOptions>): Chainable<Subject>
/**
* Uncheck specific checkbox.
*
Expand All @@ -2335,7 +2337,7 @@ declare namespace Cypress {
* // Uncheck the checkbox with the value of 'ga'
* cy.get('input[type="checkbox"]').uncheck('ga')
*/
uncheck(value: string, options?: Partial<CheckOptions>): Chainable<Subject>
uncheck(value: string, options?: Partial<CheckClearOptions>): Chainable<Subject>
/**
* Uncheck specific checkboxes.
*
Expand All @@ -2344,7 +2346,7 @@ declare namespace Cypress {
* // Uncheck the checkbox with the value of 'ga', 'ma'
* cy.get('input[type="checkbox"]').uncheck(['ga', 'ma'])
*/
uncheck(values: string[], options?: Partial<CheckOptions>): Chainable<Subject>
uncheck(values: string[], options?: Partial<CheckClearOptions>): Chainable<Subject>

/**
* Get the current URL of the page that is currently active.
Expand Down Expand Up @@ -2556,6 +2558,8 @@ declare namespace Cypress {
* expect(s).to.have.been.calledOnce
*/
withArgs(...args: any[]): Omit<A, 'withArgs'> & Agent<A>

callsFake(func: (...args: any[]) => any): Omit<A, 'withArgs'> & Agent<A>
}

type Agent<T extends sinon.SinonSpy> = SinonSpyAgent<T> & T
Expand Down Expand Up @@ -2726,13 +2730,7 @@ declare namespace Cypress {

interface BlurOptions extends Loggable, Timeoutable, Forceable { }

interface CheckOptions extends Loggable, Timeoutable, ActionableOptions {
interval: number
}

interface ClearOptions extends Loggable, Timeoutable, ActionableOptions {
interval: number
}
interface CheckClearOptions extends Loggable, Timeoutable, ActionableOptions { }

/**
* Object to change the default behavior of .click().
Expand Down Expand Up @@ -3695,7 +3693,7 @@ declare namespace Cypress {
*
* @default 0
*/
duration: number
duration: number | string
/**
* Will scroll with the easing animation
*
Expand Down Expand Up @@ -3986,6 +3984,11 @@ declare namespace Cypress {
decode: boolean
}

/** Options to change the default behavior of .readFile */
interface ReadFileOptions extends Loggable {
encoding: Encodings
}

/** Options to change the default behavior of .writeFile */
interface WriteFileOptions extends Loggable {
flag: string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { assertLogLength } = require('../../../support/utils')
import { assertLogLength } from '../../../support/utils'

const { _, Promise, $ } = Cypress

describe('src/cy/commands/actions/check', () => {
Expand Down Expand Up @@ -167,7 +168,14 @@ describe('src/cy/commands/actions/check', () => {
it('can forcibly click even when being covered by another element', () => {
const checkbox = $('<input type=\'checkbox\' />').attr('id', 'checkbox-covered-in-span').prependTo($('body'))

$('<span>span on checkbox</span>').css({ position: 'absolute', left: checkbox.offset().left, top: checkbox.offset().top, padding: 5, display: 'inline-block', backgroundColor: 'yellow' }).prependTo($('body'))
$('<span>span on checkbox</span>')
.css('position', 'absolute')
.css('left', `${checkbox.offset()?.left }px`)
.css('top', `${checkbox.offset()?.top }px`)
.css('padding', '5px')
.css('display', 'inline-block')
.css('background-color', 'yellow')
.prependTo($('body'))

let clicked = false

Expand All @@ -186,14 +194,12 @@ describe('src/cy/commands/actions/check', () => {
.prependTo($('body'))

$('<span>span on checkbox</span>')
.css({
position: 'absolute',
left: checkbox.offset().left,
top: checkbox.offset().top,
padding: 5,
display: 'inline-block',
backgroundColor: 'yellow',
})
.css('position', 'absolute')
.css('left', `${checkbox.offset()?.left }px`)
.css('top', `${checkbox.offset()?.top }px`)
.css('padding', '5px')
.css('display', 'inline-block')
.css('background-color', 'yellow')
.prependTo($('body'))

cy.on('command:retry', _.once((options) => {
Expand All @@ -203,6 +209,7 @@ describe('src/cy/commands/actions/check', () => {
done()
}))

// @ts-expect-error: TODO: Internal types for check should accept interval
cy.get('#checkbox-covered-in-span').check({ timeout: 1000, interval: 60 })
})

Expand Down Expand Up @@ -558,7 +565,14 @@ describe('src/cy/commands/actions/check', () => {
it('throws when input cannot be clicked', function (done) {
const checkbox = $('<input type=\'checkbox\' />').attr('id', 'checkbox-covered-in-span').prependTo($('body'))

$('<span>span on button</span>').css({ position: 'absolute', left: checkbox.offset().left, top: checkbox.offset().top, padding: 5, display: 'inline-block', backgroundColor: 'yellow' }).prependTo($('body'))
$('<span>span on button</span>')
.css('position', 'absolute')
.css('left', `${checkbox.offset()?.left }px`)
.css('top', `${checkbox.offset()?.top }px`)
.css('padding', '5px')
.css('display', 'inline-block')
.css('background-color', 'yellow')
.prependTo($('body'))

cy.on('fail', (err) => {
assertLogLength(this.logs, 2)
Expand Down Expand Up @@ -691,8 +705,8 @@ describe('src/cy/commands/actions/check', () => {
})

it('logs only 1 check event on click of 1 checkbox', () => {
const logs = []
const checks = []
const logs: any[] = []
const checks: any[] = []

cy.on('log:added', (attrs, log) => {
logs.push(log)
Expand All @@ -708,8 +722,8 @@ describe('src/cy/commands/actions/check', () => {
})

it('logs only 1 check event on click of 1 radio', () => {
const logs = []
const radios = []
const logs: any[] = []
const radios: any[] = []

cy.on('log:added', (attrs, log) => {
logs.push(log)
Expand All @@ -725,8 +739,8 @@ describe('src/cy/commands/actions/check', () => {
})

it('logs only 1 check event on checkbox with 1 matching value arg', () => {
const logs = []
const checks = []
const logs: any[] = []
const checks: any[] = []

cy.on('log:added', (attrs, log) => {
logs.push(log)
Expand All @@ -742,8 +756,8 @@ describe('src/cy/commands/actions/check', () => {
})

it('logs only 1 check event on radio with 1 matching value arg', () => {
const logs = []
const radios = []
const logs: any[] = []
const radios: any[] = []

cy.on('log:added', (attrs, log) => {
logs.push(log)
Expand Down Expand Up @@ -988,7 +1002,14 @@ describe('src/cy/commands/actions/check', () => {
let clicked = false
const checkbox = $('<input type=\'checkbox\' />').attr('id', 'checkbox-covered-in-span').prop('checked', true).prependTo($('body'))

$('<span>span on checkbox</span>').css({ position: 'absolute', left: checkbox.offset().left, top: checkbox.offset().top, padding: 5, display: 'inline-block', backgroundColor: 'yellow' }).prependTo($('body'))
$('<span>span on checkbox</span>')
.css('position', 'absolute')
.css('left', `${checkbox.offset()?.left }px`)
.css('top', `${checkbox.offset()?.top }px`)
.css('padding', '5px')
.css('display', 'inline-block')
.css('background-color', 'yellow')
.prependTo($('body'))

checkbox.on('click', () => {
clicked = true
Expand All @@ -1002,7 +1023,14 @@ describe('src/cy/commands/actions/check', () => {
it('passes timeout and interval down to click', (done) => {
const checkbox = $('<input type=\'checkbox\' />').attr('id', 'checkbox-covered-in-span').prop('checked', true).prependTo($('body'))

$('<span>span on checkbox</span>').css({ position: 'absolute', left: checkbox.offset().left, top: checkbox.offset().top, padding: 5, display: 'inline-block', backgroundColor: 'yellow' }).prependTo($('body'))
$('<span>span on checkbox</span>')
.css('position', 'absolute')
.css('left', `${checkbox.offset()?.left }px`)
.css('top', `${checkbox.offset()?.top }px`)
.css('padding', '5px')
.css('display', 'inline-block')
.css('background-color', 'yellow')
.prependTo($('body'))

cy.on('command:retry', (options) => {
expect(options.timeout).to.eq(1000)
Expand All @@ -1011,6 +1039,7 @@ describe('src/cy/commands/actions/check', () => {
done()
})

// @ts-expect-error: TODO: Internal types for uncheck should accept interval
cy.get('#checkbox-covered-in-span').uncheck({ timeout: 1000, interval: 60 })
})

Expand Down Expand Up @@ -1171,7 +1200,14 @@ describe('src/cy/commands/actions/check', () => {
it('throws when input cannot be clicked', function (done) {
const checkbox = $('<input type=\'checkbox\' />').attr('id', 'checkbox-covered-in-span').prop('checked', true).prependTo($('body'))

$('<span>span on button</span>').css({ position: 'absolute', left: checkbox.offset().left, top: checkbox.offset().top, padding: 5, display: 'inline-block', backgroundColor: 'yellow' }).prependTo($('body'))
$('<span>span on button</span>')
.css('position', 'absolute')
.css('left', `${checkbox.offset()?.left }px`)
.css('top', `${checkbox.offset()?.top }px`)
.css('padding', '5px')
.css('display', 'inline-block')
.css('background-color', 'yellow')
.prependTo($('body'))

cy.on('fail', (err) => {
assertLogLength(this.logs, 2)
Expand Down Expand Up @@ -1330,8 +1366,8 @@ describe('src/cy/commands/actions/check', () => {
})

it('logs only 1 uncheck event', () => {
const logs = []
const unchecks = []
const logs: any[] = []
const unchecks: any[] = []

cy.on('log:added', (attrs, log) => {
logs.push(log)
Expand All @@ -1347,8 +1383,8 @@ describe('src/cy/commands/actions/check', () => {
})

it('logs only 1 uncheck event on uncheck with 1 matching value arg', () => {
const logs = []
const unchecks = []
const logs: any[] = []
const unchecks: any[] = []

cy.on('log:added', (attrs, log) => {
logs.push(log)
Expand Down
Loading

0 comments on commit 5bf4273

Please sign in to comment.