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

Commit 103702c

Browse files
authored
fix: no longer require JSX name (#175)
so you can mount divs from tests like this ```js mount(<div>Works</div>) ```
1 parent 9fc4520 commit 103702c

File tree

6 files changed

+45
-17
lines changed

6 files changed

+45
-17
lines changed

cypress/component/advanced/lazy-loaded/lazy-load-spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// https://github.com/bahmutov/cypress-react-unit-test/issues/136
2+
// dynamic imports like this work in example projects, but not inside this repo
3+
// probably due to webpack plugins not set up correctly ☹️
24
describe.skip('dynamic import', () => {
35
it('loads', () => {
46
// cy.wrap(import('./lazy-add'))

cypress/component/advanced/material-ui-example/list-item-spec.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ import { ListItemText } from '@material-ui/core'
66
import SimpleList from './list-demo'
77

88
it('renders a list item', () => {
9-
const Demo = () => (
9+
mount(
1010
<ListItem>
1111
<ListItemText primary={'my example list item'} />
12-
</ListItem>
12+
</ListItem>,
1313
)
14-
mount(<Demo />)
1514
cy.contains('my example list item')
1615
})
1716

cypress/component/advanced/portal/portal-spec.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@ const RenderInPortal = ({ children }) => {
66
return ReactDOM.createPortal(children, document.body)
77
}
88

9-
const MyComponent = () => (
10-
<div id="component">
11-
<p>Hello World!</p>
12-
<RenderInPortal>
13-
<p> I am in portal </p>
14-
</RenderInPortal>
15-
</div>
16-
)
17-
189
describe('Portals', () => {
1910
it('renders content inside the portal', () => {
20-
mount(<MyComponent />)
11+
mount(
12+
<div id="component">
13+
<p>Hello World!</p>
14+
<RenderInPortal>
15+
<p> I am in portal </p>
16+
</RenderInPortal>
17+
</div>,
18+
)
2119

2220
cy.contains('#component', 'Hello World!')
2321
cy.get('#component').should('not.contain', 'I am in portal')
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/// <reference types="cypress" />
2+
/// <reference types="../../lib" />
3+
import React from 'react'
4+
import { mount } from 'cypress-react-unit-test'
5+
6+
function Button() {
7+
return <button>Hello</button>
8+
}
9+
10+
describe('mounting a div', () => {
11+
it('works', () => {
12+
mount(<div className="example">Works</div>)
13+
cy.contains('Works').should('be.visible')
14+
})
15+
16+
// https://github.com/bahmutov/cypress-react-unit-test/issues/98
17+
it('mount multiple components', function() {
18+
mount(
19+
<div>
20+
<Button />
21+
<hr />
22+
<Button />
23+
</div>,
24+
)
25+
cy.get('button').should('have.length', 2)
26+
})
27+
})

lib/getDisplayName.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ export default function getDisplayName(
4949
}
5050
}
5151

52-
cachedDisplayNames.set(type, displayName)
52+
try {
53+
cachedDisplayNames.set(type, displayName)
54+
} catch (e) {}
5355

5456
return displayName
5557
}

lib/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ export const mount = (jsx: React.ReactElement, options: MountOptions = {}) => {
4545

4646
// Get the display name property via the component constructor
4747
// @ts-ignore FIXME
48-
const displayname = getDisplayName(jsx.type, options.alias)
48+
const displayName = getDisplayName(jsx.type, options.alias)
4949
let logInstance: Cypress.Log
5050

5151
return cy
5252
.then(() => {
5353
if (options.log !== false) {
5454
logInstance = Cypress.log({
5555
name: 'mount',
56-
message: [`ReactDOM.render(<${displayname} ... />)`],
56+
message: [`ReactDOM.render(<${displayName} ... />)`],
5757
})
5858
}
5959
})
@@ -99,7 +99,7 @@ export const mount = (jsx: React.ReactElement, options: MountOptions = {}) => {
9999

100100
return cy
101101
.wrap(CypressTestComponent, { log: false })
102-
.as(options.alias || displayname)
102+
.as(options.alias || displayName)
103103
.then(() => {
104104
if (logInstance) {
105105
logInstance.snapshot('mounted')

0 commit comments

Comments
 (0)