Skip to content

Commit

Permalink
chore: refactor create into class $Cy (#18715)
Browse files Browse the repository at this point in the history
  • Loading branch information
sainthkh committed Nov 17, 2021
1 parent ce63723 commit 3817e50
Show file tree
Hide file tree
Showing 28 changed files with 1,679 additions and 1,672 deletions.
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,
getXhrTypeByAlias (alias) {
if (requestXhrRe.test(alias)) {
return 'request'
}

return 'response'
},
}
})

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

3 comments on commit 3817e50

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 3817e50 Nov 17, 2021

Choose a reason for hiding this comment

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

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/9.0.1/circle-develop-3817e507169b01eb0a29966a77be765fba605cdf/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 3817e50 Nov 17, 2021

Choose a reason for hiding this comment

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

AppVeyor has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/9.0.1/appveyor-develop-3817e507169b01eb0a29966a77be765fba605cdf/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 3817e50 Nov 17, 2021

Choose a reason for hiding this comment

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

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/9.0.1/circle-develop-3817e507169b01eb0a29966a77be765fba605cdf/cypress.tgz

Please sign in to comment.