Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/driver/cypress/integration/cy/snapshot_css_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { $ } = Cypress
const $SnapshotsCss = require('../../../src/cy/snapshots_css').default
const { create } = require('../../../src/cy/snapshots_css')

const normalizeStyles = (styles) => {
return styles
Expand All @@ -17,7 +17,7 @@ describe('driver/src/cy/snapshots_css', () => {
let snapshotCss

beforeEach(() => {
snapshotCss = $SnapshotsCss.create(cy.$$, cy.state)
snapshotCss = create(cy.$$, cy.state)

cy.viewport(400, 600)
cy.visit('/fixtures/generic.html').then(() => {
Expand Down
8 changes: 4 additions & 4 deletions packages/driver/cypress/integration/cy/timeouts_spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Timeouts from '@packages/driver/src/cy/timeouts'
import { create } from '@packages/driver/src/cy/timeouts'

describe('driver/src/cy/timeouts', () => {
beforeEach(() => {
Expand All @@ -7,7 +7,7 @@ describe('driver/src/cy/timeouts', () => {

it('creates timeout and clearTimeout props', () => {
const state = cy.state('window')
const timeouts = Timeouts.create(state)
const timeouts = create(state)

expect(timeouts).to.have.property('timeout')
expect(timeouts).to.have.property('clearTimeout')
Expand All @@ -16,7 +16,7 @@ describe('driver/src/cy/timeouts', () => {
context('timeout', () => {
it('throws when no runnable', () => {
const state = () => { }
const timeouts = Timeouts.create(state)
const timeouts = create(state)

const fn = () => {
return timeouts.timeout(0)
Expand All @@ -31,7 +31,7 @@ describe('driver/src/cy/timeouts', () => {
context('clearTimeout', () => {
it('throws when no runnable', () => {
const state = () => { }
const timeouts = Timeouts.create(state)
const timeouts = create(state)

const fn = () => {
return timeouts.clearTimeout()
Expand Down
175 changes: 77 additions & 98 deletions packages/driver/src/cy/aliases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,133 +12,112 @@ const aliasDisplayName = (name) => {
return name.replace(aliasDisplayRe, '')
}

const getXhrTypeByAlias = (alias) => {
if (requestXhrRe.test(alias)) {
return 'request'
}
// eslint-disable-next-line @cypress/dev/arrow-body-multiline-braces
export const create = (cy) => ({
addAlias (ctx, aliasObj) {
const { alias, subject } = aliasObj

return 'response'
}

const validateAlias = (alias: string) => {
if (!_.isString(alias)) {
$errUtils.throwErrByPath('as.invalid_type')
}

if (aliasDisplayRe.test(alias)) {
$errUtils.throwErrByPath('as.invalid_first_token', {
args: {
alias,
suggestedName: alias.replace(aliasDisplayRe, ''),
},
})
}

if (_.isEmpty(alias)) {
$errUtils.throwErrByPath('as.empty_string')
}

if (reserved.includes(alias)) {
return $errUtils.throwErrByPath('as.reserved_word', { args: { alias } })
}

return null
}
const aliases = cy.state('aliases') || {}

export default {
create: (cy) => {
const addAlias = (ctx, aliasObj) => {
const { alias, subject } = aliasObj
aliases[alias] = aliasObj
cy.state('aliases', aliases)

const aliases = cy.state('aliases') || {}
const remoteSubject = cy.getRemotejQueryInstance(subject)

aliases[alias] = aliasObj
cy.state('aliases', aliases)
ctx[alias] = remoteSubject ?? subject
},

const remoteSubject = cy.getRemotejQueryInstance(subject)
getAlias (name, cmd, log) {
const aliases = cy.state('aliases') || {}

ctx[alias] = remoteSubject ?? subject
// bail if the name doesnt reference an alias
if (!aliasRe.test(name)) {
return
}

const getNextAlias = () => {
const next = cy.state('current').get('next')
const alias = aliases[name.slice(1)]

if (next && (next.get('name') === 'as')) {
return next.get('args')[0]
}
// slice off the '@'
if (!alias) {
this.aliasNotFoundFor(name, cmd, log)
}

const getAlias = (name, cmd, log) => {
const aliases = cy.state('aliases') || {}
return alias
},

// bail if the name doesnt reference an alias
if (!aliasRe.test(name)) {
return
}
// below are public because its expected other commands
// know about them and are expected to call them

const alias = aliases[name.slice(1)]
getNextAlias () {
const next = cy.state('current').get('next')

// slice off the '@'
if (!alias) {
aliasNotFoundFor(name, cmd, log)
}
if (next && (next.get('name') === 'as')) {
return next.get('args')[0]
}
},

return alias
validateAlias (alias: string) {
if (!_.isString(alias)) {
$errUtils.throwErrByPath('as.invalid_type')
}

const getAvailableAliases = () => {
const aliases = cy.state('aliases')
if (aliasDisplayRe.test(alias)) {
$errUtils.throwErrByPath('as.invalid_first_token', {
args: {
alias,
suggestedName: alias.replace(aliasDisplayRe, ''),
},
})
}

if (!aliases) {
return []
}
if (_.isEmpty(alias)) {
$errUtils.throwErrByPath('as.empty_string')
}

return _.keys(aliases)
if (reserved.includes(alias)) {
$errUtils.throwErrByPath('as.reserved_word', { args: { alias } })
}

const aliasNotFoundFor = (name, cmd, log) => {
let displayName
const availableAliases = getAvailableAliases()

// throw a very specific error if our alias isnt in the right
// format, but its word is found in the availableAliases
if (!aliasRe.test(name) && availableAliases.includes(name)) {
displayName = aliasDisplayName(name)
$errUtils.throwErrByPath('alias.invalid', {
onFail: log,
args: { name, displayName },
})
}

cmd = cmd ?? ((log && log.get('name')) || cy.state('current').get('name'))
displayName = aliasDisplayName(name)
return null
},

aliasNotFoundFor (name, cmd, log) {
const availableAliases = cy.state('aliases')
? _.keys(cy.state('aliases'))
: []

const errPath = availableAliases.length
? 'alias.not_registered_with_available'
: 'alias.not_registered_without_available'
let displayName

return $errUtils.throwErrByPath(errPath, {
// throw a very specific error if our alias isnt in the right
// format, but its word is found in the availableAliases
if (!aliasRe.test(name) && availableAliases.includes(name)) {
displayName = aliasDisplayName(name)
$errUtils.throwErrByPath('alias.invalid', {
onFail: log,
args: { cmd, displayName, availableAliases: availableAliases.join(', ') },
args: { name, displayName },
})
}

return {
getAlias,

addAlias,

// these are public because its expected other commands
// know about them and are expected to call them
getNextAlias,
cmd = cmd ?? ((log && log.get('name')) || cy.state('current').get('name'))
displayName = aliasDisplayName(name)

validateAlias,
const errPath = availableAliases.length
? 'alias.not_registered_with_available'
: 'alias.not_registered_without_available'

aliasNotFoundFor,

getXhrTypeByAlias,
$errUtils.throwErrByPath(errPath, {
onFail: log,
args: { cmd, displayName, availableAliases: availableAliases.join(', ') },
})
},

getAvailableAliases,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was this export used in other places?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was an internal function used only inside aliases.ts. So, I removed it.

getXhrTypeByAlias (alias) {
if (requestXhrRe.test(alias)) {
return 'request'
}

return 'response'
},
}
})

export interface IAliases extends ReturnType<typeof create> {}
Loading