Skip to content

Commit 052be4b

Browse files
committed
fix(utils): un-break current htmlTreeAsString keyAttrs usages
1 parent 7f7d78d commit 052be4b

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

packages/utils/src/browser.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const DEFAULT_MAX_STRING_LENGTH = 80;
1414
*/
1515
export function htmlTreeAsString(
1616
elem: unknown,
17-
options: { keyAttrs?: string[]; maxStringLength?: number } = {},
17+
options: string[] | { keyAttrs?: string[]; maxStringLength?: number } = {},
1818
): string {
1919
type SimpleNode = {
2020
parentNode: SimpleNode;
@@ -33,7 +33,8 @@ export function htmlTreeAsString(
3333
const separator = ' > ';
3434
const sepLength = separator.length;
3535
let nextStr;
36-
const { keyAttrs, maxStringLength = DEFAULT_MAX_STRING_LENGTH } = options;
36+
const keyAttrs = Array.isArray(options) ? options : options.keyAttrs;
37+
const maxStringLength = (!Array.isArray(options) && options.maxStringLength) || DEFAULT_MAX_STRING_LENGTH;
3738

3839
while (currentElem && height++ < MAX_TRAVERSE_HEIGHT) {
3940
nextStr = _htmlElementAsString(currentElem, keyAttrs);

packages/utils/test/browser.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ describe('htmlTreeAsString', () => {
4444
</li>`;
4545
document.body.appendChild(el);
4646

47+
// Two formats for specifying keyAttrs
48+
expect(htmlTreeAsString(document.getElementById('cat-2'), ['test-id'])).toBe(
49+
'body > ul > li.li-class[title="li-title"] > img[test-id="cat-2-test-id"]',
50+
);
4751
expect(htmlTreeAsString(document.getElementById('cat-2'), { keyAttrs: ['test-id'] })).toBe(
4852
'body > ul > li.li-class[title="li-title"] > img[test-id="cat-2-test-id"]',
4953
);

0 commit comments

Comments
 (0)