Skip to content

Commit

Permalink
feat: swap the #__cy_root id selector to become data-cy-root for comp…
Browse files Browse the repository at this point in the history
…onent mounting (#20951)

* Change #__cy_root selector for CT mounting point to be a data-cy-root selector

* moving all of the gets for the container element into the mount-utils

* Force HTMLElement instead of Element

* unskip vue2 tests

Co-authored-by: Lachlan Miller <lachlan.miller.1990@outlook.com>
Co-authored-by: Tim Griesser <tgriesser10@gmail.com>
  • Loading branch information
3 people authored Apr 13, 2022
1 parent 17905a7 commit 0e7b555
Show file tree
Hide file tree
Showing 137 changed files with 6,697 additions and 106,770 deletions.
4 changes: 2 additions & 2 deletions npm/angular/cypress/support/component-index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>Components App</title>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
</html>
2 changes: 1 addition & 1 deletion npm/design-system/cypress/support/component-index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>Components App</title>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
14 changes: 12 additions & 2 deletions npm/mount-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@ export interface StyleOptions {
cssFile: string | string[]
}

export const ROOT_ID = '__cy_root'
export const ROOT_SELECTOR = '[data-cy-root]'

export const getContainerEl = (): HTMLElement => {
const el = document.querySelector<HTMLElement>(ROOT_SELECTOR)

if (el) {
return el
}

throw Error(`No element found that matches selector ${ROOT_SELECTOR}. Please use the mount utils to mount it properly`)
}

/**
* Remove any style or extra link elements from the iframe placeholder
Expand Down Expand Up @@ -131,7 +141,7 @@ export const injectStylesBeforeElement = (
options: Partial<StyleOptions & { log: boolean }>,
document: Document,
el: HTMLElement | null,
) => {
): HTMLElement => {
if (!el) return

// first insert all stylesheets as Link elements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ it('Todo - should create snapshot', () => {
// expect(tree).toMatchSnapshot();

// entire test area
cy.get('#__cy_root')
cy.get('[data-cy-root]')
.invoke('html')
.then(pretty)
.should(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('Renderless component', () => {
const onMoved = cy.stub()

mount(<MouseMovement onMoved={onMoved} />)
cy.get('#__cy_root').should('be.empty')
cy.get('[data-cy-root]').should('be.empty')
cy.document()
.trigger('mousemove')
.then(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import pretty from 'pretty'
it('says hello world', () => {
mount(<Hello name="world" />)
cy.contains('Hello, world!').should('be.visible')
cy.get('#__cy_root')
cy.get('[data-cy-root]')
.invoke('html')
.then(pretty)
.should('equal', '<h1>Hello, world!</h1>')
Expand Down
2 changes: 1 addition & 1 deletion npm/react/cypress/support/component-index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>Components App</title>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>Components App</title>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
2 changes: 1 addition & 1 deletion npm/react/examples/find-webpack/cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default defineConfig({
},
'env': {
'cypress-react-selector': {
'root': '#__cy_root',
'root': '[data-cy-root]',
},
},
})
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>Components App</title>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<div id="__next_css__DO_NOT_USE__"></div>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<div id="__next_css__DO_NOT_USE__"></div>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>Components App</title>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>Components App</title>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>Components App</title>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>Components App</title>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>Components App</title>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>Components App</title>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>Components App</title>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>Components App</title>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>Components App</title>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>Components App</title>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>Components App</title>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>Components App</title>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>Components App</title>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
2 changes: 1 addition & 1 deletion npm/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"watch": "yarn build --watch --watch.exclude ./dist/**/*"
},
"dependencies": {
"@cypress/mount-utils": "*",
"@cypress/mount-utils": "file:../mount-utils",
"debug": "^4.3.2",
"find-webpack": "2.2.1",
"find-yarn-workspace-root": "2.0.0"
Expand Down
17 changes: 8 additions & 9 deletions npm/react/src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import getDisplayName from './getDisplayName'
import {
injectStylesBeforeElement,
StyleOptions,
ROOT_ID,
getContainerEl,
ROOT_SELECTOR,
setupHooks,
} from '@cypress/mount-utils'

/**
* Inject custom style text or CSS file or 3rd party style resources
*/
const injectStyles = (options: MountOptions) => {
return () => {
const el = document.getElementById(ROOT_ID)
return (): HTMLElement => {
const el = getContainerEl()

return injectStylesBeforeElement(options, document, el)
}
Expand Down Expand Up @@ -66,12 +67,12 @@ const _mount = (type: 'mount' | 'rerender', jsx: React.ReactNode, options: Mount

lastMountedReactDom = reactDomToUse

const el = document.getElementById(ROOT_ID)
const el = getContainerEl()

if (!el) {
throw new Error(
[
'[@cypress/react] 🔥 Hmm, cannot find root element to mount the component.',
`[@cypress/react] 🔥 Hmm, cannot find root element to mount the component. Searched for ${ROOT_SELECTOR}`,
].join(' '),
)
}
Expand Down Expand Up @@ -156,9 +157,7 @@ export const unmount = (options = { log: true }): globalThis.Cypress.Chainable<J

const _unmount = (options: { boundComponentMessage?: string, log: boolean }) => {
return cy.then(() => {
const selector = `#${ROOT_ID}`

return cy.get(selector, { log: false }).then(($el) => {
return cy.get(ROOT_SELECTOR, { log: false }).then(($el) => {
if (lastMountedReactDom) {
const wasUnmounted = lastMountedReactDom.unmountComponentAtNode($el[0])

Expand All @@ -185,7 +184,7 @@ const _unmount = (options: { boundComponentMessage?: string, log: boolean }) =>
// NOTE: we cannot use unmount here because
// we are not in the context of a test
const preMountCleanup = () => {
const el = document.getElementById(ROOT_ID)
const el = getContainerEl()

if (el && lastMountedReactDom) {
lastMountedReactDom.unmountComponentAtNode(el)
Expand Down
2 changes: 1 addition & 1 deletion npm/vite-dev-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Install `@cypress/vue` or `@cypress/react` to get this package working properly
- Responds to every query with the prefix `__cypress/src/` (base path should be this prefix).
- Responds to `__cypress/src/index.html` with an html page.
This page
- will contain an element `<div id="__cy_root"></div>`. Tis will be used by mount function to mount the app containing the components we want.
- will contain an element `<div data-cy-root></div>`. Tis will be used by mount function to mount the app containing the components we want.
- will load support files
- will load the current spec from the url
- will start the test when both files are done loading
Expand Down
2 changes: 1 addition & 1 deletion npm/vite-dev-server/cypress/support/component-index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>Components App</title>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
2 changes: 1 addition & 1 deletion npm/vite-dev-server/src/plugins/cypress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const Cypress = (

// insert the script in the end of the body
return `${indexHtmlContent.substring(0, endOfBody)
}<script>
}<script type="module">
${loader}
</script>${
indexHtmlContent.substring(endOfBody)
Expand Down
2 changes: 1 addition & 1 deletion npm/vue/cypress/support/component-index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>Components App</title>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>Components App</title>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>Components App</title>
</head>
<body>
<div id="__cy_root"></div>
<div data-cy-root></div>
</body>
</html>
2 changes: 1 addition & 1 deletion npm/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"test:ci:ct": "node ../../scripts/run-ct-examples.js --examplesList=./examples.env"
},
"dependencies": {
"@cypress/mount-utils": "*",
"@cypress/mount-utils": "file:../mount-utils",
"@vue/test-utils": "2.0.0-rc.19"
},
"devDependencies": {
Expand Down
Loading

2 comments on commit 0e7b555

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 0e7b555 Apr 13, 2022

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/10.0.0/linux-x64/10.0-release-0e7b555f93fb403f431c5de4a07ae7ad6ac89ba2/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 0e7b555 Apr 13, 2022

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/10.0.0/darwin-x64/10.0-release-0e7b555f93fb403f431c5de4a07ae7ad6ac89ba2/cypress.tgz

Please sign in to comment.