feat: dedupe snapshot role and name
This commit is contained in:
@@ -278,6 +278,9 @@ type SnapshotLine = {
|
|||||||
text: string
|
text: string
|
||||||
baseLocator?: string
|
baseLocator?: string
|
||||||
hasChildren?: boolean
|
hasChildren?: boolean
|
||||||
|
role?: string
|
||||||
|
name?: string
|
||||||
|
indent?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
type SnapshotNode = {
|
type SnapshotNode = {
|
||||||
@@ -302,7 +305,7 @@ function buildSnapshotLine({ role, name, baseLocator, indent, hasChildren }: {
|
|||||||
const escapedName = name.replace(/"/g, '\\"')
|
const escapedName = name.replace(/"/g, '\\"')
|
||||||
text += ` "${escapedName}"`
|
text += ` "${escapedName}"`
|
||||||
}
|
}
|
||||||
return { text, baseLocator, hasChildren }
|
return { text, baseLocator, hasChildren, role, name, indent }
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildTextLine(text: string, indent: number): SnapshotLine {
|
function buildTextLine(text: string, indent: number): SnapshotLine {
|
||||||
@@ -319,6 +322,27 @@ function unindentLines(lines: SnapshotLine[]): SnapshotLine[] {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function buildLocatorLineText({ line, locator }: { line: SnapshotLine; locator: string }): string {
|
||||||
|
const prefix = ' '.repeat(line.indent ?? 0)
|
||||||
|
const role = line.role ?? ''
|
||||||
|
const name = line.name ?? ''
|
||||||
|
const escapedName = name.replace(/"/g, '\\"')
|
||||||
|
|
||||||
|
const hasRoleInLocator = role ? locator.includes(role) : false
|
||||||
|
const hasNameInLocator = name ? locator.includes(escapedName) : false
|
||||||
|
|
||||||
|
const parts: string[] = []
|
||||||
|
if (role && !hasRoleInLocator) {
|
||||||
|
parts.push(role)
|
||||||
|
}
|
||||||
|
if (name && !hasNameInLocator) {
|
||||||
|
parts.push(`"${escapedName}"`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const base = parts.length > 0 ? `${prefix}- ${parts.join(' ')}` : `${prefix}-`
|
||||||
|
return `${base} ${locator}`
|
||||||
|
}
|
||||||
|
|
||||||
function finalizeSnapshotOutput(lines: SnapshotLine[], nodes: SnapshotNode[]): { snapshot: string; tree: AriaSnapshotNode[] } {
|
function finalizeSnapshotOutput(lines: SnapshotLine[], nodes: SnapshotNode[]): { snapshot: string; tree: AriaSnapshotNode[] } {
|
||||||
const locatorCounts = lines.reduce<Map<string, number>>((acc, line) => {
|
const locatorCounts = lines.reduce<Map<string, number>>((acc, line) => {
|
||||||
if (!line.baseLocator) {
|
if (!line.baseLocator) {
|
||||||
@@ -347,7 +371,7 @@ function finalizeSnapshotOutput(lines: SnapshotLine[], nodes: SnapshotNode[]): {
|
|||||||
if (line.baseLocator) {
|
if (line.baseLocator) {
|
||||||
const locator = locatorSequence[lineLocatorIndex]
|
const locator = locatorSequence[lineLocatorIndex]
|
||||||
lineLocatorIndex += 1
|
lineLocatorIndex += 1
|
||||||
text = `${text} ${locator}`
|
text = buildLocatorLineText({ line, locator })
|
||||||
}
|
}
|
||||||
if (line.hasChildren) {
|
if (line.hasChildren) {
|
||||||
text += ':'
|
text += ':'
|
||||||
|
|||||||
Reference in New Issue
Block a user