Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✅ [RUMF-815] import RRWeb integration tests #738

Merged
merged 5 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 39 additions & 22 deletions test/e2e/lib/helpers/recorder.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
NodeType,
SerializedNode,
DocumentNode,
ElementNode,
TextNode,
SerializedNodeWithId,
} from '@datadog/browser-rum-recorder/cjs/domain/rrweb-snapshot/types'
import {
Segment,
Expand Down Expand Up @@ -40,41 +40,58 @@ export function findAllIncrementalSnapshots(segment: Segment, source: Incrementa
) as IncrementalSnapshotRecord[]
}

// Retrns the textContent of a ElementNode, if any.
// Returns the textContent of a ElementNode, if any.
export function findTextContent(elem: ElementNode): string | null {
const text = elem.childNodes.find((child) => child.type === NodeType.Text) as TextNode
return text ? text.textContent : null
}

// Returns the first ElementNode with the given ID from a
// FullSnapshotRecord, if any.
export function findNodeWithId(fullSnapshot: FullSnapshotRecord, id: string): ElementNode | null {
return recFindNodeWithId(fullSnapshot.data.node as DocumentNode, id)
// Returns the first ElementNode with the given ID attribute from a FullSnapshotRecord, if any.
export function findElementWithIdAttribute(fullSnapshot: FullSnapshotRecord, id: string) {
return findElement(fullSnapshot.data.node, (node) => node.attributes.id === id)
}

function isElementNode(node: SerializedNode): node is ElementNode {
return node.type === NodeType.Element
// Returns the first ElementNode with the given tag name from a FullSnapshotRecord, if any.
export function findElementWithTagName(fullSnapshot: FullSnapshotRecord, tagName: string) {
return findElement(fullSnapshot.data.node, (node) => node.tagName === tagName)
}

function recFindNodeWithId(node: DocumentNode | ElementNode | null, id: string): ElementNode | null {
if (node === null) {
return null
}
// Returns the first TextNode with the given content from a FullSnapshotRecord, if any.
export function findTextNode(fullSnapshot: FullSnapshotRecord, textContent: string) {
return findNode(fullSnapshot.data.node, (node) => isTextNode(node) && node.textContent === textContent) as
| (TextNode & { id: number })
| null
}

if (isElementNode(node) && node.attributes.id === id) {
// Returns the first ElementNode matching the predicate
export function findElement(root: SerializedNodeWithId, predicate: (node: ElementNode) => boolean) {
return findNode(root, (node) => isElementNode(node) && predicate(node)) as (ElementNode & { id: number }) | null
}

// Returns the first SerializedNodeWithId matching the predicate
export function findNode(
node: SerializedNodeWithId,
predicate: (node: SerializedNodeWithId) => boolean
): SerializedNodeWithId | null {
if (predicate(node)) {
return node
}

for (const child of node.childNodes) {
if (!isElementNode(child)) {
continue
}

const node = recFindNodeWithId(child, id)
if (node !== null) {
return node
if ('childNodes' in node) {
for (const child of node.childNodes) {
const node = findNode(child, predicate)
if (node !== null) {
return node
}
}
}

return null
}

function isElementNode(node: SerializedNode): node is ElementNode {
return node.type === NodeType.Element
}

function isTextNode(node: SerializedNode): node is TextNode {
return node.type === NodeType.Text
}
Loading