Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit 52d883f

Browse files
committed
feat: move alias to options object
BREAKING CHANGE: instead of element, alias, then options object the new syntax has only an element and optional options object. ```js cy.mount(<HelloX name='World' />, {alias: 'HelloX'}) ```
1 parent a84de0e commit 52d883f

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

cypress/integration/alias-spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference types="cypress" />
2+
/// <reference types="../../lib" />
3+
import {HelloX} from '../../src/hello-x.jsx'
4+
import React from 'react'
5+
6+
/* eslint-env mocha */
7+
describe('Alias', () => {
8+
it('uses default display name', () => {
9+
cy.mount(<HelloX name='World' />, {alias: 'HelloX'})
10+
.then(function () {
11+
expect(this.HelloX).to.be.an('object')
12+
})
13+
cy.get('@HelloX').should('be.an', 'object')
14+
})
15+
})

cypress/integration/inject-style-spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ describe('Injecting style', () => {
3636
color: white;
3737
}
3838
`
39-
cy.mount(<Button name='Orange' orange />, null, { style })
39+
cy.mount(<Button name='Orange' orange />, { style })
4040
cy.get('.orange button')
4141
.should('have.css', 'background-color', 'rgb(245, 146, 62)')
4242
})
4343

4444
it('read CSS file and pass as style', () => {
4545
cy.readFile('cypress/integration/Button.css')
4646
.then(style => {
47-
cy.mount(<Button name='Orange' orange />, null, { style })
47+
cy.mount(<Button name='Orange' orange />, { style })
4848
})
4949

5050
cy.get('.component-button')
@@ -55,7 +55,7 @@ describe('Injecting style', () => {
5555

5656
it('can be read automatically', () => {
5757
const cssFile = 'cypress/integration/Button.css'
58-
cy.mount(<Button name='Orange' orange />, null, { cssFile })
58+
cy.mount(<Button name='Orange' orange />, { cssFile })
5959
cy.get('.orange button')
6060
.should('have.css', 'background-color', 'rgb(245, 146, 62)')
6161
})
@@ -70,14 +70,14 @@ describe('Injecting style', () => {
7070
it('is orange', function () {
7171
// notice we use "function () {}" callback
7272
// to get the test context "this" to be able to use "this.style"
73-
cy.mount(<Button name='Orange' orange />, null, { style: this.style })
73+
cy.mount(<Button name='Orange' orange />, { style: this.style })
7474

7575
cy.get('.orange button')
7676
.should('have.css', 'background-color', 'rgb(245, 146, 62)')
7777
})
7878

7979
it('is orange again', function () {
80-
cy.mount(<Button name='Orange' orange />, null, { style: this.style })
80+
cy.mount(<Button name='Orange' orange />, { style: this.style })
8181

8282
cy.get('.orange button')
8383
.should('have.css', 'background-color', 'rgb(245, 146, 62)')

lib/getDisplayName.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ const cachedDisplayNames: WeakMap<JSX, string> = new WeakMap();
99
* @link https://github.com/facebook/react-devtools/blob/master/backend/getDisplayName.js
1010
*/
1111
export default function getDisplayName(type: JSX, fallbackName: string = 'Unknown'): string {
12+
if (type === undefined) {
13+
// something is terrible wrong with this JSX element
14+
throw new Error(`Something is wrong with this element "${type}" with fallback "${fallbackName}"`)
15+
}
16+
1217
const nameFromCache = cachedDisplayNames.get(type)
1318

1419
if (nameFromCache != null) {

lib/index.d.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ interface JSXElement {
2727
type filepath = string
2828

2929
interface MountOptions {
30+
/**
31+
* How to refer to this component later on.
32+
* Usually automatic.
33+
*/
34+
alias: string,
3035
/**
3136
* CSS string to inject as a style element
3237
* before mounting the component.
@@ -65,7 +70,7 @@ declare namespace Cypress {
6570
```
6671
import Hello from './hello.jsx'
6772
// mount and access by alias
68-
cy.mount(<Hello />, 'Hello')
73+
cy.mount(<Hello />, {alias: 'Hello'})
6974
// using default alias
7075
cy.get('@Component')
7176
// using specified alias
@@ -74,7 +79,7 @@ declare namespace Cypress {
7479
cy.get(Hello)
7580
```
7681
**/
77-
mount: (component: JSXElement, alias?: string, options?: Partial<MountOptions>) => Chainable<void>
82+
mount: (component: JSXElement, options?: Partial<MountOptions>) => Chainable<void>
7883
get<S = any>(alias: string | symbol | Function, options?: Partial<Loggable & Timeoutable>): Chainable<any>
7984
/**
8085
* Utility method to re-render a new / updated component

lib/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ const injectStyle = (options?: Partial<MountOptions>) => (w: Window) => {
128128
cy.get(Hello)
129129
```
130130
**/
131-
export const mount = (jsx: JSXElement, alias?: string, options?: Partial<MountOptions>) => {
131+
export const mount = (jsx: JSXElement, options: Partial<MountOptions> = {}) => {
132132
// Get the display name property via the component constructor
133133
const jsxType = typeof jsx.type === 'string' ? jsx as unknown as JSX : jsx.type
134-
const displayname = getDisplayName(jsxType, alias)
134+
const displayname = getDisplayName(jsxType, options.alias)
135135

136136
let cmd: Cypress.Log;
137137

@@ -163,7 +163,7 @@ export const mount = (jsx: JSXElement, alias?: string, options?: Partial<MountOp
163163
render(jsx)
164164
}
165165
const component = render(jsx);
166-
cy.wrap(component, { log: false }).as(alias || displayname)
166+
cy.wrap(component, { log: false }).as(options.alias || displayname)
167167
})
168168
cy.copyComponentStyles(jsx)
169169
.then(() => {

0 commit comments

Comments
 (0)