xref: /aosp_15_r20/external/perfetto/ui/src/base/object_utils_unittest.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1// Copyright (C) 2023 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import {getPath, setPath, shallowEquals} from './object_utils';
16
17describe('getPath', () => {
18  const nested = {quux: 'qux'};
19  const value = {
20    foo: {
21      bar: [1, 2, 3],
22    },
23    baz: nested,
24  };
25
26  test('happy path', () => {
27    expect(getPath(value, ['foo', 'bar', 1])).toBe(2);
28    expect(getPath(value, ['foo', 'bar', 2])).toBe(3);
29    expect(getPath(value, ['foo', 'bar'])).toEqual([1, 2, 3]);
30    expect(getPath(value, ['foo'])).toEqual({bar: [1, 2, 3]});
31    expect(getPath(value, [])).toBe(value);
32    expect(getPath(value, ['baz'])).toBe(nested);
33  });
34
35  it('returns undefined when path is incomplete', () => {
36    // value.quux does not exist so we expect undefined to be returned
37    expect(getPath(value, ['quux'])).toBe(undefined);
38    expect(getPath(value, ['quux', 'corge'])).toBe(undefined);
39  });
40});
41
42describe('setPath', () => {
43  const nested = {quux: 'qux'};
44  const value = {
45    foo: {
46      bar: [1, 2, 3],
47    },
48    baz: nested,
49  };
50
51  test('happy path', () => {
52    setPath(value, ['foo', 'bar', 1], 10);
53    expect(value).toEqual({
54      foo: {
55        bar: [1, 10, 3],
56      },
57      baz: nested,
58    });
59  });
60
61  it('appends node when target node is missing', () => {
62    setPath(value, ['quux'], 'abc');
63    expect((value as unknown as {quux: string})['quux']).toBe('abc');
64  });
65
66  it('throws when path node(s) are missing', () => {
67    // value.quux does not exist, so setting value.quux.corge is an error.
68    expect(() => {
69      setPath(value, ['quux', 'corge'], 'abc');
70    }).toThrowError(TypeError);
71  });
72
73  it('throws when path is empty', () => {
74    expect(() => {
75      setPath(value, [], 'abc');
76    }).toThrowError(TypeError);
77  });
78});
79
80test('shallowEquals', () => {
81  const one = 1;
82  const foo = 'Foo!';
83  const nestedFoo = {
84    foo,
85  };
86  const nestedFooDupe = {
87    foo,
88  };
89
90  expect(shallowEquals({}, {})).toBe(true);
91  expect(shallowEquals({one}, {})).toBe(false);
92  expect(shallowEquals({}, {one})).toBe(false);
93  expect(shallowEquals({one}, {one})).toBe(true);
94  expect(shallowEquals({nestedFoo}, {nestedFoo})).toBe(true);
95  expect(shallowEquals({nestedFoo}, {nestedFooDupe})).toBe(false);
96});
97