xref: /aosp_15_r20/external/perfetto/ui/src/base/dom_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 {
16  elementIsEditable,
17  findRef,
18  isOrContains,
19  toHTMLElement,
20} from './dom_utils';
21
22describe('isOrContains', () => {
23  const parent = document.createElement('div');
24  const child = document.createElement('div');
25  parent.appendChild(child);
26
27  it('finds child in parent', () => {
28    expect(isOrContains(parent, child)).toBeTruthy();
29  });
30
31  it('finds child in child', () => {
32    expect(isOrContains(child, child)).toBeTruthy();
33  });
34
35  it('does not find parent in child', () => {
36    expect(isOrContains(child, parent)).toBeFalsy();
37  });
38});
39
40describe('findRef', () => {
41  const parent = document.createElement('div');
42  const fooChild = document.createElement('div');
43  fooChild.setAttribute('ref', 'foo');
44  parent.appendChild(fooChild);
45  const barChild = document.createElement('div');
46  barChild.setAttribute('ref', 'bar');
47  parent.appendChild(barChild);
48
49  it('should find refs in parent divs', () => {
50    expect(findRef(parent, 'foo')).toEqual(fooChild);
51    expect(findRef(parent, 'bar')).toEqual(barChild);
52  });
53
54  it('should find refs in self divs', () => {
55    expect(findRef(fooChild, 'foo')).toEqual(fooChild);
56    expect(findRef(barChild, 'bar')).toEqual(barChild);
57  });
58
59  it('should fail to find ref in unrelated divs', () => {
60    const unrelated = document.createElement('div');
61    expect(findRef(unrelated, 'foo')).toBeNull();
62    expect(findRef(fooChild, 'bar')).toBeNull();
63    expect(findRef(barChild, 'foo')).toBeNull();
64  });
65});
66
67describe('toHTMLElement', () => {
68  it('should convert a div to an HTMLElement', () => {
69    const divElement: Element = document.createElement('div');
70    expect(toHTMLElement(divElement)).toEqual(divElement);
71  });
72
73  it('should fail to convert an svg element to an HTMLElement', () => {
74    const svgElement = document.createElementNS(
75      'http://www.w3.org/2000/svg',
76      'svg',
77    );
78    expect(() => toHTMLElement(svgElement)).toThrow(Error);
79  });
80});
81
82describe('elementIsEditable', () => {
83  test('text input', () => {
84    const el = document.createElement('input');
85    el.setAttribute('type', 'text');
86    expect(elementIsEditable(el)).toBeTruthy();
87  });
88
89  test('radio input', () => {
90    const el = document.createElement('input');
91    el.setAttribute('type', 'radio');
92    expect(elementIsEditable(el)).toBeFalsy();
93  });
94
95  test('checkbox input', () => {
96    const el = document.createElement('input');
97    el.setAttribute('type', 'checkbox');
98    expect(elementIsEditable(el)).toBeFalsy();
99  });
100
101  test('button input', () => {
102    const el = document.createElement('input');
103    el.setAttribute('type', 'button');
104    expect(elementIsEditable(el)).toBeFalsy();
105  });
106
107  test('div', () => {
108    const el = document.createElement('div');
109    expect(elementIsEditable(el)).toBeFalsy();
110  });
111
112  test('textarea', () => {
113    const el = document.createElement('textarea');
114    expect(elementIsEditable(el)).toBeTruthy();
115  });
116
117  test('nested', () => {
118    const el = document.createElement('textarea');
119    const nested = document.createElement('div');
120    el.appendChild(nested);
121    expect(elementIsEditable(nested)).toBeTruthy();
122  });
123});
124