xref: /aosp_15_r20/external/perfetto/ui/src/base/registry_unittest.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1// Copyright (C) 2018 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 {Registry} from './registry';
16
17interface Registrant {
18  kind: string;
19  n: number;
20}
21
22test('registry returns correct registrant', () => {
23  const registry = Registry.kindRegistry<Registrant>();
24
25  const a: Registrant = {kind: 'a', n: 1};
26  const b: Registrant = {kind: 'b', n: 2};
27  registry.register(a);
28  registry.register(b);
29
30  expect(registry.get('a')).toBe(a);
31  expect(registry.get('b')).toBe(b);
32});
33
34test('registry throws error on kind collision', () => {
35  const registry = Registry.kindRegistry<Registrant>();
36
37  const a1: Registrant = {kind: 'a', n: 1};
38  const a2: Registrant = {kind: 'a', n: 2};
39
40  registry.register(a1);
41  expect(() => registry.register(a2)).toThrow();
42});
43
44test('registry throws error on non-existent track', () => {
45  const registry = Registry.kindRegistry<Registrant>();
46  expect(() => registry.get('foo')).toThrow();
47});
48
49test('registry allows iteration', () => {
50  const registry = Registry.kindRegistry<Registrant>();
51  const a: Registrant = {kind: 'a', n: 1};
52  const b: Registrant = {kind: 'b', n: 2};
53  registry.register(a);
54  registry.register(b);
55
56  const values = [...registry.values()];
57  expect(values.length).toBe(2);
58  expect(values.includes(a)).toBe(true);
59  expect(values.includes(b)).toBe(true);
60});
61