Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: swap the #__cy_root id selector to become data-cy-root for component mounting #20951

Merged
merged 35 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
7f8b134
Change #__cy_root selector for CT mounting point to be a data-cy-root…
JessicaSachs Apr 6, 2022
1a76b25
moving all of the gets for the container element into the mount-utils
JessicaSachs Apr 6, 2022
2f2e200
Fixing types
JessicaSachs Apr 6, 2022
ba3fb41
merge in 10.0-release, fix bug in mount utils
lmiller1990 Apr 11, 2022
218535d
Merge remote-tracking branch 'origin/10.0-release' into feat/swap-cy-…
JessicaSachs Apr 11, 2022
bb876dd
trying file protocol to link dep
JessicaSachs Apr 11, 2022
75147d8
trying file protocol to link dep
JessicaSachs Apr 11, 2022
28d9127
Force HTMLElement instead of Element
JessicaSachs Apr 11, 2022
88a3a3b
use unreleased versions of vue
JessicaSachs Apr 11, 2022
d4ffdcc
yarn lock
JessicaSachs Apr 12, 2022
d407440
yarn lock
JessicaSachs Apr 12, 2022
e72f288
yarn lock
JessicaSachs Apr 12, 2022
fe3ddf0
maybe this will fix it
JessicaSachs Apr 12, 2022
cb6b591
maybe this will fix it
JessicaSachs Apr 12, 2022
b865dcc
removing some of the older examples
JessicaSachs Apr 12, 2022
8be0f86
removing the custom index html only
JessicaSachs Apr 12, 2022
1b0c2e3
fs allow
JessicaSachs Apr 12, 2022
3997374
update deps
lmiller1990 Apr 13, 2022
50c6075
use bundled vue/react in tests
lmiller1990 Apr 13, 2022
c7af6a0
revert
lmiller1990 Apr 13, 2022
5e87da8
webpack 5 system tests are broken
JessicaSachs Apr 13, 2022
616dd22
Merge branch 'feat/swap-cy-root-id-for-data-attribute' of github.com:…
JessicaSachs Apr 13, 2022
c62eddc
Update HelloWorld.cy.ts
JessicaSachs Apr 13, 2022
0fa120f
Update mount.ts
JessicaSachs Apr 13, 2022
0e15505
Merge branch '10.0-release' into feat/swap-cy-root-id-for-data-attribute
tgriesser Apr 13, 2022
a9ef1ca
linting
JessicaSachs Apr 13, 2022
8d35667
Merge branch 'feat/swap-cy-root-id-for-data-attribute' of github.com:…
JessicaSachs Apr 13, 2022
1501c6a
fixing vue2 to use getContainerEl
JessicaSachs Apr 13, 2022
b43b6df
linter
JessicaSachs Apr 13, 2022
a26b221
unskip vue2 tests
tgriesser Apr 13, 2022
56981ed
fixes for build, incorrect cy root
tgriesser Apr 13, 2022
818bc84
fixing nuxt issue w sys tests
JessicaSachs Apr 13, 2022
753dc65
Merge branch '10.0-release' into feat/swap-cy-root-id-for-data-attribute
tgriesser Apr 13, 2022
9afb13f
Hopefully the final test to fix
tgriesser Apr 13, 2022
59eb082
Merge branch 'feat/swap-cy-root-id-for-data-attribute' of github.com:…
tgriesser Apr 13, 2022
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 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