Skip to content

Commit

Permalink
Add test for circularReferenceReplacer
Browse files Browse the repository at this point in the history
  • Loading branch information
robbie-c committed Aug 13, 2024
1 parent 6325059 commit 0b88932
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/__tests__/extensions/replay/sessionrecording-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
splitBuffer,
SEVEN_MEGABYTES,
estimateSize,
circularReferenceReplacer,
} from '../../../extensions/replay/sessionrecording-utils'
import { largeString, threeMBAudioURI, threeMBImageURI } from '../test_data/sessionrecording-utils-test-data'
import { eventWithTime } from '@rrweb/types'
Expand Down Expand Up @@ -333,4 +334,22 @@ describe(`SessionRecording utility functions`, () => {
expect(result).toEqual([buffer])
})
})

describe('circularReferenceReplacer', () => {
it('should handle circular references', () => {
const obj: any = {}
obj.obj = obj
const result = JSON.stringify(obj, circularReferenceReplacer())
expect(result).toEqual('{"obj":"[Circular]"}')
})

it('should handle nested circular references', () => {
const a: any = {}
const b: any = {}
a.b = b
b.a = a
const result = JSON.stringify(a, circularReferenceReplacer())
expect(result).toEqual('{"b":{"a":"[Circular]"}}')
})
})
})
2 changes: 1 addition & 1 deletion src/extensions/replay/sessionrecording-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { isObject } from '../../utils/type-utils'
import { SnapshotBuffer } from './sessionrecording'

// taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#circular_references
function circularReferenceReplacer() {
export function circularReferenceReplacer() {
const ancestors: any[] = []
return function (this: any, _key: string, value: any) {
if (isObject(value)) {
Expand Down

0 comments on commit 0b88932

Please sign in to comment.