Skip to content

Commit

Permalink
feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
sainthkh committed Mar 18, 2022
1 parent 2d4191c commit 4052c73
Show file tree
Hide file tree
Showing 19 changed files with 496 additions and 510 deletions.
94 changes: 47 additions & 47 deletions packages/driver/src/cy/commands/actions/focus.ts
Expand Up @@ -22,85 +22,85 @@ interface InternalBlurOptions extends Partial<Cypress.BlurOptions> {

export default (Commands, Cypress, cy) => {
return Commands.addAll({ prevSubject: ['element', 'window'] }, {
focus (subject, options: Partial<Cypress.Loggable & Cypress.Timeoutable> = {}) {
focus (subject, userOptions: Partial<Cypress.Loggable & Cypress.Timeoutable> = {}) {
// we should throw errors by default!
// but allow them to be silenced
const _options: InternalFocusOptions = _.defaults({}, options, {
const options: InternalFocusOptions = _.defaults({}, userOptions, {
$el: subject,
error: true,
log: true,
verify: true,
})

const isWin = $dom.isWindow(_options.$el)
const isWin = $dom.isWindow(options.$el)

if (isWin) {
// get this into a jquery object
_options.$el = $dom.wrap(_options.$el)
options.$el = $dom.wrap(options.$el)
}

if (_options.log) {
_options._log = Cypress.log({
$el: _options.$el,
timeout: _options.timeout,
if (options.log) {
options._log = Cypress.log({
$el: options.$el,
timeout: options.timeout,
consoleProps () {
return { 'Applied To': $dom.getElements(_options.$el) }
return { 'Applied To': $dom.getElements(options.$el) }
},
})
}

const el = _options.$el.get(0)
const el = options.$el.get(0)

// the body is not really focusable, but it
// can have focus on initial page load.
// this is instead a noop.
// TODO: throw on body instead (breaking change)
const isBody = $dom.isJquery(_options.$el) &&
$elements.isElement(_options.$el.get(0)) &&
$elements.isBody(_options.$el.get(0))
const isBody = $dom.isJquery(options.$el) &&
$elements.isElement(options.$el.get(0)) &&
$elements.isBody(options.$el.get(0))

// http://www.w3.org/$R/html5/editing.html#specially-focusable
// ensure there is only 1 dom element in the subject
// make sure its allowed to be focusable
if (!(isWin || isBody || $dom.isFocusable(_options.$el))) {
if (_options.error === false) {
if (!(isWin || isBody || $dom.isFocusable(options.$el))) {
if (options.error === false) {
return
}

const node = $dom.stringify(_options.$el)
const node = $dom.stringify(options.$el)

$errUtils.throwErrByPath('focus.invalid_element', {
onFail: _options._log,
onFail: options._log,
args: { node },
})
}

if (_options.$el.length && _options.$el.length > 1) {
if (_options.error === false) {
if (options.$el.length && options.$el.length > 1) {
if (options.error === false) {
return
}

$errUtils.throwErrByPath('focus.multiple_elements', {
onFail: _options._log,
args: { num: _options.$el.length },
onFail: options._log,
args: { num: options.$el.length },
})
}

cy.fireFocus(el)

const verifyAssertions = () => {
return cy.verifyUpcomingAssertions(_options.$el, _options, {
return cy.verifyUpcomingAssertions(options.$el, options, {
onRetry: verifyAssertions,
})
}

return verifyAssertions()
},

blur (subject, options: Partial<Cypress.BlurOptions> = {}) {
blur (subject, userOptions: Partial<Cypress.BlurOptions> = {}) {
// we should throw errors by default!
// but allow them to be silenced
const _options: InternalBlurOptions = _.defaults({}, options, {
const options: InternalBlurOptions = _.defaults({}, userOptions, {
$el: subject,
$focused: cy.getFocused(),
error: true,
Expand All @@ -109,78 +109,78 @@ export default (Commands, Cypress, cy) => {
force: false,
})

const { $focused } = _options
const { $focused } = options

const isWin = $dom.isWindow(_options.$el)
const isWin = $dom.isWindow(options.$el)

if (isWin) {
// get this into a jquery object
_options.$el = $dom.wrap(_options.$el)
options.$el = $dom.wrap(options.$el)
}

const isBody = _options.$el.is('body')
const isBody = options.$el.is('body')

if (_options.log) {
if (options.log) {
// figure out the options which actually change the behavior of clicks
const deltaOptions = $utils.filterOutOptions(_options)
const deltaOptions = $utils.filterOutOptions(options)

_options._log = Cypress.log({
$el: _options.$el,
options._log = Cypress.log({
$el: options.$el,
message: deltaOptions,
timeout: _options.timeout,
timeout: options.timeout,
consoleProps () {
return { 'Applied To': $dom.getElements(_options.$el) }
return { 'Applied To': $dom.getElements(options.$el) }
},
})
}

if (_options.$el.length && _options.$el.length > 1) {
if (_options.error === false) {
if (options.$el.length && options.$el.length > 1) {
if (options.error === false) {
return
}

$errUtils.throwErrByPath('blur.multiple_elements', {
onFail: _options._log,
args: { num: _options.$el.length },
onFail: options._log,
args: { num: options.$el.length },
})
}

// if we haven't forced the blur, and we don't currently
// have a focused element OR we aren't the window or body then error
if (_options.force !== true && !$focused && !isWin && !isBody) {
if (_options.error === false) {
if (options.force !== true && !$focused && !isWin && !isBody) {
if (options.error === false) {
return
}

$errUtils.throwErrByPath('blur.no_focused_element', { onFail: _options._log })
$errUtils.throwErrByPath('blur.no_focused_element', { onFail: options._log })
}

// if we're currently window dont check for the wrong
// focused element because window will not show up
// as $focused
if (_options.force !== true &&
if (options.force !== true &&
!isWin &&
!isBody &&
_options.$el.get(0) !== $focused.get(0)
options.$el.get(0) !== $focused.get(0)
) {
if (_options.error === false) {
if (options.error === false) {
return
}

const node = $dom.stringify($focused)

$errUtils.throwErrByPath('blur.wrong_focused_element', {
onFail: _options._log,
onFail: options._log,
args: { node },
})
}

const el = _options.$el.get(0)
const el = options.$el.get(0)

cy.fireBlur(el)

const verifyAssertions = () => {
return cy.verifyUpcomingAssertions(_options.$el, _options, {
return cy.verifyUpcomingAssertions(options.$el, options, {
onRetry: verifyAssertions,
})
}
Expand Down

0 comments on commit 4052c73

Please sign in to comment.