Skip to content

Commit 54e31e3

Browse files
sainthkhBlue Fryanthemanuelflotwig
authored
chore: Improve pkg/driver types part 2 (#21610)
* keyboard.ts * mouse.ts * chai.ts * xhr.ts * snapshots.ts * overrides.ts * command_queue.ts * fix errors. * fix tests. Co-authored-by: Blue F <[email protected]> Co-authored-by: Ryan Manuel <[email protected]> Co-authored-by: Zach Bloomquist <[email protected]>
1 parent c0ea9bd commit 54e31e3

File tree

20 files changed

+133
-88
lines changed

20 files changed

+133
-88
lines changed

packages/driver/cypress/e2e/commands/actions/click.cy.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ describe('src/cy/commands/actions/click', () => {
745745
cy.get('#three-buttons button').click({ multiple: true }).then(() => {
746746
const calls = cy.timeout.getCalls()
747747

748-
const num = _.filter(calls, (call) => _.isEqual(call.args, [50, true, 'click']))
748+
const num = _.filter(calls, (call) => _.isEqual(call.args, [50, true]))
749749

750750
expect(num.length).to.eq(count)
751751
})
@@ -3239,7 +3239,7 @@ describe('src/cy/commands/actions/click', () => {
32393239
cy.get('#three-buttons button').dblclick().then(() => {
32403240
const calls = cy.timeout.getCalls()
32413241

3242-
const num = _.filter(calls, (call) => _.isEqual(call.args, [50, true, 'dblclick']))
3242+
const num = _.filter(calls, (call) => _.isEqual(call.args, [50, true]))
32433243

32443244
expect(num.length).to.eq(count)
32453245
})

packages/driver/cypress/e2e/cypress/command_queue.cy.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import _ from 'lodash'
2+
import type { IStability } from '../../../src/cy/stability'
23
import $Command from '../../../src/cypress/command'
34
import { CommandQueue } from '../../../src/cypress/command_queue'
5+
import type { StateFunc } from '../../../src/cypress/state'
46

57
const createCommand = (props = {}) => {
68
return $Command.create(_.extend({
@@ -22,15 +24,16 @@ const log = (props = {}) => {
2224

2325
describe('src/cypress/command_queue', () => {
2426
let queue
25-
const state = () => {}
27+
const state = (() => {}) as StateFunc
2628
const timeout = () => {}
27-
const whenStable = () => {}
28-
const cleanup = () => {}
29+
const whenStable = {} as IStability
30+
const cleanup = () => 0
2931
const fail = () => {}
30-
const isCy = () => {}
32+
const isCy = () => true
33+
const clearTimeout = () => {}
3134

3235
beforeEach(() => {
33-
queue = new CommandQueue(state, timeout, whenStable, cleanup, fail, isCy)
36+
queue = new CommandQueue(state, timeout, whenStable, cleanup, fail, isCy, clearTimeout)
3437

3538
queue.add(createCommand({
3639
name: 'get',

packages/driver/src/cy/actionability.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Promise from 'bluebird'
44

55
import debugFn from 'debug'
66
import $dom from '../dom'
7-
import type { ElWindowPostion, ElViewportPostion } from '../dom/coordinates'
7+
import type { ElWindowPostion, ElViewportPostion, ElementPositioning } from '../dom/coordinates'
88
import $elements from '../dom/elements'
99
import $errUtils from '../cypress/error_utils'
1010
const debug = debugFn('cypress:driver:actionability')
@@ -290,7 +290,12 @@ const ensureNotAnimating = function (cy, $el, coordsHistory, animationDistanceTh
290290
cy.ensureElementIsNotAnimating($el, coordsHistory, animationDistanceThreshold)
291291
}
292292

293-
const verify = function (cy, $el, config, options, callbacks) {
293+
interface VerifyCallbacks {
294+
onReady?: ($el: any, coords: ElementPositioning) => any
295+
onScroll?: ($el: any, type: 'element' | 'window' | 'container') => any
296+
}
297+
298+
const verify = function (cy, $el, config, options, callbacks: VerifyCallbacks) {
294299
_.defaults(options, {
295300
scrollBehavior: config('scrollBehavior'),
296301
ensure: {

packages/driver/src/cy/chai.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ const imageMarkdown = /!\[.*?\]\(.*?\)/g
3535
const doubleslashRe = /\\\\/g
3636
const escapedDoubleslashRe = /__double_slash__/g
3737

38-
type CreateFunc = ((specWindow, state, assertFn) => ({
38+
type CreateFunc = (specWindow: SpecWindow, state: StateFunc, assertFn: $Cy['assert']) => ({
3939
chai: Chai.ChaiStatic
4040
expect: (val: any, message?: string) => Chai.Assertion
4141
assert: any
42-
}))
42+
})
4343
export let create: CreateFunc | null = null
4444

4545
chai.use(sinonChai)
@@ -439,7 +439,7 @@ chai.use((chai, u) => {
439439
})
440440
}
441441

442-
const captureUserInvocationStack = (specWindow, state: StateFunc, ssfi) => {
442+
const captureUserInvocationStack = (specWindow: SpecWindow, state: StateFunc, ssfi) => {
443443
// we need a user invocation stack with the top line being the point where
444444
// the error occurred for the sake of the code frame
445445
// in chrome, stack lines from another frame don't appear in the

packages/driver/src/cy/commands/actions/click.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import $dom from '../../../dom'
55
import $utils from '../../../cypress/utils'
66
import $errUtils from '../../../cypress/error_utils'
77
import $actionability from '../../actionability'
8+
import type { ElViewportPostion } from '../../../dom/coordinates'
9+
import type { $Cy } from '../../../cypress/cy'
10+
import type { ForceEl } from '../../mouse'
811

912
const formatMouseEvents = (events) => {
1013
return _.map(events, (val, key) => {
@@ -39,12 +42,12 @@ type MouseActionOptions = {
3942
positionOrX: string | number
4043
y: number
4144
userOptions: Record<string, any>
42-
onReady: Function
45+
onReady: (fromElViewport: ElViewportPostion, forceEl: ForceEl) => any
4346
onTable: Function
4447
defaultOptions?: Record<string, any>
4548
}
4649

47-
export default (Commands, Cypress, cy, state, config) => {
50+
export default (Commands, Cypress, cy: $Cy, state, config) => {
4851
const { mouse, keyboard } = cy.devices
4952

5053
const mouseAction = (eventName, { subject, positionOrX, y, userOptions, onReady, onTable, defaultOptions }: MouseActionOptions) => {
@@ -128,7 +131,7 @@ export default (Commands, Cypress, cy, state, config) => {
128131

129132
// add this delay delta to the runnables timeout because we delay
130133
// by it below before performing each click
131-
cy.timeout($actionability.delay, true, eventName)
134+
cy.timeout($actionability.delay, true)
132135

133136
const createLog = (domEvents, fromElWindow, fromAutWindow) => {
134137
let consoleObj

packages/driver/src/cy/commands/xhr.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ const setRequest = (state, xhr, alias) => {
6060
return state('requests', requests)
6161
}
6262

63+
export type XHRRequest = {
64+
xhr: any
65+
alias: any
66+
}
67+
6368
export interface XHRResponse {
6469
xhr: any
6570
alias: any

packages/driver/src/cy/focused.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,4 @@ export const create = (state: StateFunc) => ({
237237
},
238238
})
239239

240-
export interface IFocused extends Omit<
241-
ReturnType<typeof create>,
242-
'documentHasFocus' | 'interceptFocus' | 'interceptBlur'
243-
> {}
240+
export interface IFocused extends ReturnType<typeof create> {}

packages/driver/src/cy/jquery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const remoteJQueryisNotSameAsGlobal = (remoteJQuery) => {
1010

1111
// eslint-disable-next-line @cypress/dev/arrow-body-multiline-braces
1212
export const create = (state: StateFunc) => ({
13-
$$ (selector, context) {
13+
$$ (selector, context?) {
1414
if (context == null) {
1515
context = state('document')
1616
}

packages/driver/src/cy/keyboard.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import $selection from '../dom/selection'
1313
import $utils from '../cypress/utils'
1414
import $window from '../dom/window'
1515
import type { Log } from '../cypress/log'
16+
import type { StateFunc } from '../cypress/state'
1617

1718
const debug = Debug('cypress:driver:keyboard')
1819

@@ -23,17 +24,6 @@ export interface KeyboardModifiers {
2324
shift: boolean
2425
}
2526

26-
export interface KeyboardState {
27-
keyboardModifiers?: KeyboardModifiers
28-
}
29-
30-
export interface ProxyState<T> {
31-
<K extends keyof T>(arg: K): T[K] | undefined
32-
<K extends keyof T>(arg: K, arg2: T[K] | null): void
33-
}
34-
35-
export type State = ProxyState<KeyboardState>
36-
3727
interface KeyDetailsPartial extends Partial<KeyDetails> {
3828
key: string
3929
}
@@ -169,11 +159,11 @@ const joinKeyArrayToString = (keyArr: KeyInfo[]) => {
169159
}).join('')
170160
}
171161

172-
type modifierKeyDetails = KeyDetails & {
162+
type KeyModifiers = {
173163
key: keyof typeof keyToModifierMap
174164
}
175165

176-
const isModifier = (details: KeyInfo): details is modifierKeyDetails => {
166+
const isModifier = (details: KeyInfo): details is KeyDetails & KeyModifiers => {
177167
return details.type === 'key' && !!keyToModifierMap[details.key]
178168
}
179169

@@ -697,7 +687,7 @@ export interface typeOptions {
697687
}
698688

699689
export class Keyboard {
700-
constructor (private state: State) {}
690+
constructor (private state: StateFunc) {}
701691

702692
type (opts: typeOptions) {
703693
const options = _.defaults({}, opts, {
@@ -1108,7 +1098,7 @@ export class Keyboard {
11081098
return details
11091099
}
11101100

1111-
flagModifier (key: modifierKeyDetails, setTo = true) {
1101+
flagModifier (key: KeyModifiers, setTo = true) {
11121102
debug('handleModifier', key.key)
11131103
const modifier = keyToModifierMap[key.key]
11141104

packages/driver/src/cy/mouse.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,22 @@ import debugFn from 'debug'
88
import type { StateFunc } from '../cypress/state'
99
import type { IFocused } from './focused'
1010
import type { ICypress } from '../cypress'
11+
import type { ElViewportPostion } from '../dom/coordinates'
1112

1213
const debug = debugFn('cypress:driver:mouse')
1314

15+
export type ForceEl = false | HTMLElement
16+
17+
export type MouseCoords = { x?: number, y?: number}
18+
1419
/**
1520
* @typedef Coords
1621
* @property {number} x
1722
* @property {number} y
1823
* @property {Document} doc
1924
*/
2025

21-
const getLastHoveredEl = (state): HTMLElement | null => {
26+
const getLastHoveredEl = (state: StateFunc): HTMLElement | null => {
2227
let lastHoveredEl = state('mouseLastHoveredEl')
2328
const lastHoveredElAttached = lastHoveredEl && $elements.isAttachedEl(lastHoveredEl)
2429

@@ -40,7 +45,7 @@ const defaultPointerDownUpOptions = {
4045
pressure: 0.5,
4146
}
4247

43-
const getMouseCoords = (state) => {
48+
const getMouseCoords = (state: StateFunc) => {
4449
return state('mouseCoords')
4550
}
4651

@@ -205,11 +210,7 @@ export const create = (state: StateFunc, keyboard: Keyboard, focused: IFocused,
205210
}, modifiersEventOptions, coordsEventOptions)
206211
},
207212

208-
/**
209-
* @param {Coords} coords
210-
* @param {HTMLElement} forceEl
211-
*/
212-
move (fromElViewport, forceEl?) {
213+
move (fromElViewport: ElViewportPostion, forceEl?: ForceEl) {
213214
debug('mouse.move', fromElViewport)
214215

215216
const lastHoveredEl = getLastHoveredEl(state)
@@ -241,7 +242,7 @@ export const create = (state: StateFunc, keyboard: Keyboard, focused: IFocused,
241242
* - send move events to elToHover (bubbles)
242243
* - elLastHovered = elToHover
243244
*/
244-
_moveEvents (el, coords) {
245+
_moveEvents (el: HTMLElement, coords: ElViewportPostion) {
245246
// events are not fired on disabled elements, so we don't have to take that into account
246247
const win = $dom.getWindowByElement(el)
247248
const { x, y } = coords
@@ -386,7 +387,7 @@ export const create = (state: StateFunc, keyboard: Keyboard, focused: IFocused,
386387
* @param {Coords} coords
387388
* @returns {HTMLElement}
388389
*/
389-
getElAtCoords ({ x, y, doc }) {
390+
getElAtCoords ({ x, y, doc }: ElViewportPostion) {
390391
const el = $dom.elementFromPoint(doc, x, y)
391392

392393
return el

packages/driver/src/cy/overrides.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import _ from 'lodash'
2-
// @ts-ignore
32
import { registerFetch } from 'unfetch'
43
import $selection from '../dom/selection'
54

6-
export const create = (state, config, focused, snapshots) => {
5+
import type { ICypress } from '../cypress'
6+
import type { StateFunc } from '../cypress/state'
7+
import type { IFocused } from './focused'
8+
import type { ISnapshots } from './snapshots'
9+
10+
export const create = (state: StateFunc, config: ICypress['config'], focused: IFocused, snapshots: ISnapshots) => {
711
const wrapNativeMethods = function (contentWindow) {
812
try {
913
// return null to trick contentWindow into thinking

packages/driver/src/cy/snapshots.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import $ from 'jquery'
22
import _ from 'lodash'
3+
import type { $Cy } from '../cypress/cy'
4+
import type { StateFunc } from '../cypress/state'
35
import $dom from '../dom'
46
import { create as createSnapshotsCSS } from './snapshots_css'
57

68
export const HIGHLIGHT_ATTR = 'data-cypress-el'
79

810
export const FINAL_SNAPSHOT_NAME = 'final state'
911

10-
export const create = ($$, state) => {
12+
export const create = ($$: $Cy['$$'], state: StateFunc) => {
1113
const snapshotsCss = createSnapshotsCSS($$, state)
1214
const snapshotsMap = new WeakMap()
1315
const snapshotDocument = new Document()
@@ -296,7 +298,4 @@ export const create = ($$, state) => {
296298
}
297299
}
298300

299-
export interface ISnapshots extends Omit<
300-
ReturnType<typeof create>,
301-
'onCssModified' | 'onBeforeWindowLoad'
302-
> {}
301+
export interface ISnapshots extends ReturnType<typeof create> {}

packages/driver/src/cy/stability.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export const create = (Cypress: ICypress, state: StateFunc) => ({
5858
state('anticipatingCrossOriginResponse', request)
5959
},
6060

61-
whenStableOrAnticipatingCrossOriginResponse (fn, command) {
61+
whenStableOrAnticipatingCrossOriginResponse (fn, command?) {
6262
const commandIsOrigin = command?.get('name') === 'origin'
6363
const commandIsEndLogGroup = command?.get('name') === 'end-logGroup'
6464

packages/driver/src/cy/xhrs.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
/* globals cy */
21
import _ from 'lodash'
32

43
import $errUtils from '../cypress/error_utils'
54
import type { StateFunc } from '../cypress/state'
65

76
const validAliasApiRe = /^(\d+|all)$/
87

9-
const xhrNotWaitedOnByIndex = (state, alias, index, prop) => {
8+
const xhrNotWaitedOnByIndex = (state: StateFunc, alias: string, index: number, prop: 'requests' | 'responses') => {
109
// find the last request or response
1110
// which hasnt already been used.
1211
let xhrs = state(prop) || []
@@ -28,7 +27,7 @@ const xhrNotWaitedOnByIndex = (state, alias, index, prop) => {
2827

2928
// eslint-disable-next-line @cypress/dev/arrow-body-multiline-braces
3029
export const create = (state: StateFunc) => ({
31-
getIndexedXhrByAlias (alias, index) {
30+
getIndexedXhrByAlias (alias: string, index: number) {
3231
let prop
3332
let str
3433

@@ -60,7 +59,7 @@ export const create = (state: StateFunc) => ({
6059
getRequestsByAlias (alias) {
6160
let prop
6261

63-
if (_.indexOf(alias, '.') === -1 || _.keys(cy.state('aliases')).includes(alias)) {
62+
if (_.indexOf(alias, '.') === -1 || _.keys(state('aliases')).includes(alias)) {
6463
prop = null
6564
} else {
6665
// potentially valid prop

0 commit comments

Comments
 (0)