Skip to content

Commit 104eef5

Browse files
fix: mount component in [data-cy-root] (#25807)
* fix(angular): mount component in [data-cy-root] * fix e2e test * add changelog entry * changelog [skip ci] * changelog --------- Co-authored-by: Lachlan Miller <lachlan.miller.1990@outlook.com>
1 parent d54fa65 commit 104eef5

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

cli/CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<!-- See the ../guides/writing-the-cypress-changelog.md for details on writing the changelog. -->
2-
32
## 12.6.1
43

54
_Released 03/1/2023 (PENDING)_
@@ -8,6 +7,7 @@ _Released 03/1/2023 (PENDING)_
87

98
- Fixed an issue where cookies were being duplicated with the same hostname, but a prepended dot. Fixed an issue where cookies may not be expiring correctly. Fixes [#25174](https://github.com/cypress-io/cypress/issues/25174), [#25205](https://github.com/cypress-io/cypress/issues/25205) and [#25495](https://github.com/cypress-io/cypress/issues/25495).
109
- Added missing TypeScript type definitions for the [`cy.reload()`](https://docs.cypress.io/api/commands/reload) command. Addressed in [#25779](https://github.com/cypress-io/cypress/pull/25779).
10+
- Ensure Angular components are mounted inside the correct element. Fixes [#24385](https://github.com/cypress-io/cypress/issues/24385)
1111
- Fix a bug where files outside the project root in a monorepo are not correctly served when using Vite. Addressed in [#25801](https://github.com/cypress-io/cypress/pull/25801)
1212

1313
**Misc:**

npm/angular/src/mount.ts

+20
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ import {
1414
getTestBed,
1515
TestModuleMetadata,
1616
TestBed,
17+
TestComponentRenderer,
1718
} from '@angular/core/testing'
1819
import {
1920
BrowserDynamicTestingModule,
2021
platformBrowserDynamicTesting,
2122
} from '@angular/platform-browser-dynamic/testing'
2223
import {
2324
setupHooks,
25+
getContainerEl,
2426
} from '@cypress/mount-utils'
2527

2628
/**
@@ -169,6 +171,22 @@ function bootstrapModule<T> (
169171
return testModuleMetaData
170172
}
171173

174+
@Injectable()
175+
export class CypressTestComponentRenderer extends TestComponentRenderer {
176+
override insertRootElement (rootElId: string) {
177+
this.removeAllRootElements()
178+
179+
const rootElement = getContainerEl()
180+
181+
rootElement.setAttribute('id', rootElId)
182+
document.body.appendChild(rootElement)
183+
}
184+
185+
override removeAllRootElements () {
186+
getContainerEl().innerHTML = ''
187+
}
188+
}
189+
172190
/**
173191
* Initializes the TestBed
174192
*
@@ -186,6 +204,8 @@ function initTestBed<T> (
186204
...bootstrapModule(componentFixture, config),
187205
})
188206

207+
getTestBed().overrideProvider(TestComponentRenderer, { useValue: new CypressTestComponentRenderer() })
208+
189209
return componentFixture
190210
}
191211

packages/app/cypress/e2e/runner/reporter-ct-mount-hover.cy.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,9 @@ for (const { projectName, test } of PROJECTS) {
4242
cy.contains(`${test}`).click()
4343
cy.waitForSpecToFinish(undefined)
4444
cy.get('.command.command-name-mount > .command-wrapper').click().then(() => {
45-
if (`${projectName}` === 'angular-14') {
46-
cy.get('iframe.aut-iframe').its('0.contentDocument.body').children().should('have.length.at.least', 2)
47-
} else {
48-
cy.get('iframe.aut-iframe').its('0.contentDocument.body').then(cy.wrap).within(() => {
49-
cy.get('[data-cy-root]').children().should('have.length.at.least', 1)
50-
})
51-
}
45+
cy.get('iframe.aut-iframe').its('0.contentDocument.body').then(cy.wrap).within(() => {
46+
cy.get('[data-cy-root]').children().should('have.length.at.least', 1)
47+
})
5248
})
5349
}
5450
})

system-tests/project-fixtures/angular/src/app/mount.cy.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -441,12 +441,15 @@ describe('angular mount', () => {
441441
})
442442

443443
describe('teardown', () => {
444+
const cyRootSelector = '[data-cy-root]'
445+
444446
beforeEach(() => {
445-
cy.get('[id^=root]').should('not.exist')
447+
cy.get(cyRootSelector).should('be.empty')
446448
})
447449

448450
it('should mount', () => {
449451
cy.mount(ButtonOutputComponent)
452+
cy.get(cyRootSelector).should('not.be.empty')
450453
})
451454

452455
it('should remove previous mounted component', () => {
@@ -456,7 +459,7 @@ describe('angular mount', () => {
456459
cy.contains('Render 2')
457460

458461
cy.contains('Render 1').should('not.exist')
459-
cy.get('[id^=root]').children().should('have.length', 1)
462+
cy.get(cyRootSelector).children().should('have.length', 1)
460463
})
461464
})
462465

0 commit comments

Comments
 (0)