Skip to content

Commit 3496ff9

Browse files
feat: add "snapshot" function for wrapper elements (#198)
1 parent 20841b8 commit 3496ff9

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

src/components/Expandable.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ function Expandable({ children, className, variant }) {
5959
{expanded || !children ? (
6060
<div>&nbsp;</div>
6161
) : (
62-
<div className="truncate mr-8">{children}</div>
62+
<div className="truncate mr-8 w-full flex justify-between direction">
63+
{children}
64+
</div>
6365
)}
6466

6567
<IconButton

src/components/PreviewHint.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,25 @@ function PreviewHint({ roles, suggestion }) {
55
const expression = suggestion.expression ? (
66
`> ${suggestion.expression}`
77
) : (
8-
<>
8+
<div>
99
<span className="font-bold">accessible roles: </span>
1010
{roles.join(', ')}
11-
</>
11+
</div>
12+
);
13+
14+
const snapshot = suggestion.snapshot && (
15+
<div className="snapshot">
16+
<div className="py-1">&nbsp;</div>
17+
<span className="font-bold">Snapshot </span>
18+
<div className="py-1">&nbsp;</div>
19+
<div>{suggestion.snapshot}</div>
20+
</div>
1221
);
1322

1423
return (
1524
<Expandable className="bg-gray-200 text-gray-800 font-mono text-xs rounded fle">
1625
{expression}
26+
{snapshot}
1727
</Expandable>
1828
);
1929
}

src/lib/queryAdvise.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,29 @@ export function getData({ rootNode, element }) {
3838
};
3939
}
4040

41+
function flattenDOM(node) {
42+
return [
43+
node,
44+
...Array.from(node.children).reduce(
45+
(acc, child) => [...acc, ...flattenDOM(child)],
46+
[],
47+
),
48+
];
49+
}
50+
51+
function getSnapshot(element) {
52+
const innerItems = flattenDOM(element);
53+
const snapshot = innerItems
54+
.map((el) => {
55+
const suggestion = getSuggestedQuery(el);
56+
return suggestion && `screen.${suggestion.toString()};`;
57+
})
58+
.filter(Boolean)
59+
.join('\n');
60+
61+
return snapshot;
62+
}
63+
4164
// TODO:
4265
// TestingLibraryDom.getSuggestedQuery($0, 'get').toString()
4366
export const emptyResult = { data: {}, suggestion: {} };
@@ -61,6 +84,7 @@ export function getQueryAdvise({ rootNode, element }) {
6184
suggestion: {
6285
level: 3,
6386
expression: `container.querySelector('${path}')`,
87+
snapshot: getSnapshot(element),
6488
method: '',
6589
...messages[3],
6690
},

src/styles/app.pcss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,11 @@ button:disabled {
177177
.preview button:hover {
178178
@apply bg-gray-400;
179179
}
180+
181+
.expanded .snapshot {
182+
@apply w-full;
183+
}
184+
185+
.collapsed .snapshot div {
186+
display: none;
187+
}

0 commit comments

Comments
 (0)