Skip to content

Commit d28bc0e

Browse files
authored
internal: fix assertion menu not displayed in the correct location (#32172)
* handle 0 offsets for selector highlight styles * use the child element for the rest of the calculations * add test * add percy snapshot * does the placement of the percysnapshot show the menu? * assert position of the menu
1 parent ed8987d commit d28bc0e

File tree

4 files changed

+84
-5
lines changed

4 files changed

+84
-5
lines changed

packages/app/cypress/e2e/studio/studio.cy.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,29 @@ cy.get('#increment').click();
422422
})
423423
})
424424
})
425+
426+
it('shows the assertions menu for an element inside an invisible wrapper', () => {
427+
launchStudio({ specName: 'spec-w-invisible-wrapper.cy.js' })
428+
429+
cy.getAutIframe().within(() => {
430+
// Show menu
431+
cy.contains('Increment').realClick({
432+
button: 'right',
433+
})
434+
435+
cy.get('.__cypress-studio-assertions-menu').shadow()
436+
.find('.assertions-menu').should('be.visible').should('have.css', 'transform', 'matrix(1, 0, 0, 1, 0, 141)')
437+
438+
// Show submenu
439+
cy.get('.__cypress-studio-assertions-menu').shadow()
440+
.find('.assertion-type-text:first').realHover()
441+
442+
cy.get('.__cypress-studio-assertions-menu').shadow()
443+
.find('.assertion-option')
444+
.contains('Increment')
445+
.should('be.visible')
446+
})
447+
})
425448
})
426449

427450
it('removes pending commands if the page is reloaded', () => {

packages/app/src/runner/dom.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,34 @@ export function getSelectorHighlightStyles (elements) {
7777
const borderSize = 2
7878

7979
return elements.map((el) => {
80-
const offset = getOffset(el)
80+
let offset = getOffset(el)
81+
let targetElement = el
82+
83+
if (offset.top === 0 && offset.left === 0 && el.children.length > 0) {
84+
// Try to find the first child with non-zero offset
85+
// This is needed to handle cases where the element is a wrapper that has 0 width and 0 height (for example an <astro-slot>)
86+
// so the highlight styles should be on the children and not the wrapper parent
87+
for (let i = 0; i < el.children.length; i++) {
88+
const childOffset = getOffset(el.children[i])
89+
90+
if (childOffset.top !== 0 || childOffset.left !== 0) {
91+
offset = childOffset
92+
targetElement = el.children[i]
93+
break
94+
}
95+
}
96+
}
8197

8298
return {
8399
position: 'absolute',
84100
margin: `0px`,
85101
padding: `0px`,
86-
width: `${el.offsetWidth}px`,
87-
height: `${el.offsetHeight}px`,
102+
width: `${targetElement.offsetWidth}px`,
103+
height: `${targetElement.offsetHeight}px`,
88104
top: `${offset.top - borderSize}px`,
89105
left: `${offset.left - borderSize}px`,
90-
transform: getComputedStyle(el, null).transform,
91-
zIndex: getZIndex(el),
106+
transform: getComputedStyle(targetElement, null).transform,
107+
zIndex: getZIndex(targetElement),
92108
}
93109
})
94110
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<title>Hello Studio</title>
9+
</head>
10+
11+
<body>
12+
<script>
13+
let count = 0
14+
15+
function increment() {
16+
count++
17+
document.querySelector('p').innerText = `Count is ${count}`
18+
}
19+
</script>
20+
21+
<h1>Hello, Studio!</h1>
22+
<hr />
23+
24+
<p>Count is 0</p>
25+
<button onclick="increment()" id="increment">
26+
<div style="width: 0; height: 0; display: contents;">
27+
<svg width="8" height="8" viewBox="0 0 8 8" fill="none" xmlns="http://www.w3.org/2000/svg">
28+
<path d="M4 2V6M6 4H2" stroke="#5A5F7A" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
29+
</svg>
30+
Increment
31+
</div>
32+
</button>
33+
</body>
34+
35+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('studio functionality', () => {
2+
it('visits a basic html page with an invisible wrapper', function () {
3+
cy.visit('cypress/e2e/invisible-wrapper.html')
4+
})
5+
})

0 commit comments

Comments
 (0)