33 */
44
55import '../../utils/mock-internal-setTimeout' ;
6- import { EventType } from '@sentry-internal/rrweb' ;
6+ import { EventType , IncrementalSource , record } from '@sentry-internal/rrweb' ;
7+ import { NodeType , type serializedElementNodeWithId } from '@sentry-internal/rrweb-snapshot' ;
78import type { MockInstance } from 'vitest' ;
89import { afterEach , beforeAll , beforeEach , describe , expect , it , vi } from 'vitest' ;
10+ import { handleDom } from '../../../src/coreHandlers/handleDom' ;
911import type { ReplayOptionFrameEvent } from '../../../src/types' ;
1012import * as SentryAddEvent from '../../../src/util/addEvent' ;
11- import { createOptionsEvent , getHandleRecordingEmit } from '../../../src/util/handleRecordingEmit' ;
13+ import {
14+ createOptionsEvent ,
15+ getHandleRecordingEmit ,
16+ syncMirrorAttributesFromMutationEvent ,
17+ } from '../../../src/util/handleRecordingEmit' ;
1218import { BASE_TIMESTAMP } from '../..' ;
1319import { setupReplayContainer } from '../../utils/setupReplayContainer' ;
1420
@@ -30,6 +36,7 @@ describe('Unit | util | handleRecordingEmit', () => {
3036
3137 afterEach ( function ( ) {
3238 addEventMock . mockReset ( ) ;
39+ vi . restoreAllMocks ( ) ;
3340 } ) ;
3441
3542 it ( 'interprets first event as checkout event' , async function ( ) {
@@ -95,4 +102,106 @@ describe('Unit | util | handleRecordingEmit', () => {
95102 expect ( addEventMock ) . toHaveBeenNthCalledWith ( 3 , replay , event , true ) ;
96103 expect ( addEventMock ) . toHaveBeenLastCalledWith ( replay , { ...optionsEvent , timestamp : BASE_TIMESTAMP } , false ) ;
97104 } ) ;
105+
106+ it ( 'syncs mirror attributes from mutation events' , function ( ) {
107+ const target = document . createElement ( 'button' ) ;
108+ target . textContent = 'Save Note' ;
109+
110+ const meta = {
111+ id : 42 ,
112+ type : NodeType . Element ,
113+ tagName : 'button' ,
114+ childNodes : [ { id : 43 , type : NodeType . Text , textContent : 'Save Note' } ] ,
115+ attributes : {
116+ id : 'next-question-button' ,
117+ 'data-testid' : 'next-question-button' ,
118+ } ,
119+ } ;
120+
121+ vi . spyOn ( record . mirror , 'getNode' ) . mockReturnValue ( target ) ;
122+ vi . spyOn ( record . mirror , 'getMeta' ) . mockReturnValue ( meta as serializedElementNodeWithId ) ;
123+ vi . spyOn ( record . mirror , 'getId' ) . mockReturnValue ( 42 ) ;
124+
125+ syncMirrorAttributesFromMutationEvent ( {
126+ type : EventType . IncrementalSnapshot ,
127+ timestamp : BASE_TIMESTAMP + 10 ,
128+ data : {
129+ source : IncrementalSource . Mutation ,
130+ texts : [ ] ,
131+ attributes : [
132+ {
133+ id : 42 ,
134+ attributes : {
135+ id : 'save-note-button' ,
136+ 'data-testid' : 'save-note-button' ,
137+ } ,
138+ } ,
139+ ] ,
140+ removes : [ ] ,
141+ adds : [ ] ,
142+ } ,
143+ } ) ;
144+
145+ expect (
146+ handleDom ( {
147+ name : 'click' ,
148+ event : { target } ,
149+ } ) ,
150+ ) . toEqual ( {
151+ category : 'ui.click' ,
152+ data : {
153+ nodeId : 42 ,
154+ node : {
155+ id : 42 ,
156+ tagName : 'button' ,
157+ textContent : 'Save Note' ,
158+ attributes : {
159+ id : 'save-note-button' ,
160+ testId : 'save-note-button' ,
161+ } ,
162+ } ,
163+ } ,
164+ message : 'button' ,
165+ timestamp : expect . any ( Number ) ,
166+ type : 'default' ,
167+ } ) ;
168+ } ) ;
169+
170+ it ( 'preserves masked mutation attribute values' , function ( ) {
171+ const target = document . createElement ( 'button' ) ;
172+
173+ const meta = {
174+ id : 42 ,
175+ type : NodeType . Element ,
176+ tagName : 'button' ,
177+ childNodes : [ ] ,
178+ attributes : {
179+ 'aria-label' : 'Save Note' ,
180+ } ,
181+ } ;
182+
183+ vi . spyOn ( record . mirror , 'getNode' ) . mockReturnValue ( target ) ;
184+ vi . spyOn ( record . mirror , 'getMeta' ) . mockReturnValue ( meta as serializedElementNodeWithId ) ;
185+
186+ syncMirrorAttributesFromMutationEvent ( {
187+ type : EventType . IncrementalSnapshot ,
188+ timestamp : BASE_TIMESTAMP + 10 ,
189+ data : {
190+ source : IncrementalSource . Mutation ,
191+ texts : [ ] ,
192+ attributes : [
193+ {
194+ id : 42 ,
195+ attributes : {
196+ 'aria-label' : '*********' ,
197+ } ,
198+ } ,
199+ ] ,
200+ removes : [ ] ,
201+ adds : [ ] ,
202+ } ,
203+ } ) ;
204+
205+ expect ( meta . attributes [ 'aria-label' ] ) . toBe ( '*********' ) ;
206+ } ) ;
98207} ) ;
0 commit comments