2025-06-27 17:08:09 +00:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright Google LLC All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
|
* found in the LICENSE file at https://angular.dev/license
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import {sanitizeObject} from './serialization-utils';
|
|
|
|
|
|
|
|
|
|
describe('sanitizeObject', () => {
|
|
|
|
|
it('should not change valid object', () => {
|
|
|
|
|
const foo = {
|
|
|
|
|
bar: 'bar',
|
|
|
|
|
baz: 42,
|
|
|
|
|
qux: true,
|
|
|
|
|
quux: null,
|
|
|
|
|
corge: undefined,
|
|
|
|
|
grault: [1, 2, 3],
|
|
|
|
|
garply: {a: 'a', b: 'b'},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
expect(sanitizeObject(foo)).toEqual({
|
|
|
|
|
bar: 'bar',
|
|
|
|
|
baz: 42,
|
|
|
|
|
qux: true,
|
|
|
|
|
quux: null,
|
2025-10-04 06:48:04 +00:00
|
|
|
corge: '[Non-serializable data]',
|
2025-06-27 17:08:09 +00:00
|
|
|
grault: [1, 2, 3],
|
|
|
|
|
garply: {a: 'a', b: 'b'},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should remove function', () => {
|
|
|
|
|
const foo = {
|
|
|
|
|
bar: 'bar',
|
|
|
|
|
baz: () => 'baz',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
expect(sanitizeObject(foo)).toEqual({
|
|
|
|
|
bar: 'bar',
|
|
|
|
|
baz: '[Non-serializable data]',
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-04 06:48:04 +00:00
|
|
|
it('should remove nested function', () => {
|
|
|
|
|
const foo = {
|
|
|
|
|
bar: 'bar',
|
|
|
|
|
baz: {
|
|
|
|
|
qux: () => 'qux',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
expect(sanitizeObject(foo)).toEqual({
|
|
|
|
|
bar: 'bar',
|
|
|
|
|
baz: {
|
|
|
|
|
qux: '[Non-serializable data]',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2025-06-27 17:08:09 +00:00
|
|
|
it('should strip cyclic references', () => {
|
|
|
|
|
const bar: any = {foo: null};
|
|
|
|
|
const foo = {
|
|
|
|
|
bar: bar,
|
|
|
|
|
};
|
|
|
|
|
bar.foo = foo;
|
|
|
|
|
|
|
|
|
|
expect(sanitizeObject(foo)).toEqual({
|
2025-10-04 06:48:04 +00:00
|
|
|
bar: {
|
|
|
|
|
foo: '[Circular]',
|
|
|
|
|
},
|
2025-06-27 17:08:09 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|