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

✨ [RUM-3684] Capture scroll record on shadow dom elements #2708

Merged
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
17 changes: 12 additions & 5 deletions packages/rum/src/domain/record/record.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import type { RumConfiguration, ViewCreatedEvent } from '@datadog/browser-rum-co
import { LifeCycle, LifeCycleEventType } from '@datadog/browser-rum-core'
import type { Clock } from '@datadog/browser-core/test'
import { createNewEvent, collectAsyncCalls } from '@datadog/browser-core/test'
import { findFullSnapshot, findNode, recordsPerFullSnapshot } from '../../../test'
import { findElement, findFullSnapshot, findNode, recordsPerFullSnapshot } from '../../../test'
import type {
BrowserIncrementalSnapshotRecord,
BrowserMutationData,
BrowserRecord,
DocumentFragmentNode,
ElementNode,
FocusRecord,
ScrollData,
} from '../../types'
import { NodeType, RecordType, IncrementalSource } from '../../types'
import { appendElement } from '../../../../rum-core/test'
Expand Down Expand Up @@ -334,20 +335,26 @@ describe('record', () => {
expect(inputRecords.length).toBe(1)
})

it('should record the scroll event inside a shadow root', () => {
const div = appendElement('<div></div>', createShadow()) as HTMLDivElement
fit('should record the scroll event inside a shadow root', () => {
N-Boutaib marked this conversation as resolved.
Show resolved Hide resolved
const div = appendElement('<div unique-selector="enabled"></div>', createShadow()) as HTMLDivElement
startRecording()
expect(getEmittedRecords().length).toBe(recordsPerFullSnapshot())

div.dispatchEvent(createNewEvent('scroll', { target: div, composed: false }))

recordApi.flushMutations()
const innerMutationData = getLastIncrementalSnapshotData<BrowserMutationData>(
const innerMutationData = getLastIncrementalSnapshotData<ScrollData>(
getEmittedRecords(),
IncrementalSource.Scroll
)

expect(innerMutationData).toBeDefined()
const fs = findFullSnapshot({ records: getEmittedRecords() })!
const scrollableNode = findElement(
fs.data.node,
(node) => { return node.attributes['unique-selector'] === "enabled" }
)!
N-Boutaib marked this conversation as resolved.
Show resolved Hide resolved

expect(innerMutationData.id).toBe(scrollableNode.id)
})

it('should clean the state once the shadow dom is removed to avoid memory leak', () => {
Expand Down
18 changes: 16 additions & 2 deletions test/e2e/scenario/recorder/shadowDom.scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ const divShadowDom = `<script>
</script>
`

/** Will generate the following HTML
* ```html
* <my-div id="titi">
* #shadow-root
* <div scrollable-div style="height:100px; overflow: scroll;">
* <div style="height:500px;"></div>
* </div>
* <button>scroll to 250</button>
*</my-div>
*```
when called like `<my-div />`
*/
const scrollableDivShadowDom = `<script>
class CustomScrollableDiv extends HTMLElement {
constructor() {
Expand Down Expand Up @@ -290,9 +302,11 @@ describe('recorder with shadow DOM', () => {
<my-scrollable-div id="host" />
`)
.run(async ({ intakeRegistry }) => {
const div = await getNodeInsideShadowDom('my-scrollable-div', 'button')
const button = await getNodeInsideShadowDom('my-scrollable-div', 'button')

await div.click()
// Triggering scrollTo from the test itself is not allowed
// Thus, a callback to scroll the div was added to the button 'click' event
await button.click()

await flushEvents()
expect(intakeRegistry.replaySegments.length).toBe(1)
Expand Down