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 {findRef, isOrContains, toHTMLElement} from './utils'; 16 17describe('isOrContains', () => { 18 const parent = document.createElement('div'); 19 const child = document.createElement('div'); 20 parent.appendChild(child); 21 22 it('finds child in parent', () => { 23 expect(isOrContains(parent, child)).toBeTruthy(); 24 }); 25 26 it('finds child in child', () => { 27 expect(isOrContains(child, child)).toBeTruthy(); 28 }); 29 30 it('does not find parent in child', () => { 31 expect(isOrContains(child, parent)).toBeFalsy(); 32 }); 33}); 34 35describe('findRef', () => { 36 const parent = document.createElement('div'); 37 const fooChild = document.createElement('div'); 38 fooChild.setAttribute('ref', 'foo'); 39 parent.appendChild(fooChild); 40 const barChild = document.createElement('div'); 41 barChild.setAttribute('ref', 'bar'); 42 parent.appendChild(barChild); 43 44 it('should find refs in parent divs', () => { 45 expect(findRef(parent, 'foo')).toEqual(fooChild); 46 expect(findRef(parent, 'bar')).toEqual(barChild); 47 }); 48 49 it('should find refs in self divs', () => { 50 expect(findRef(fooChild, 'foo')).toEqual(fooChild); 51 expect(findRef(barChild, 'bar')).toEqual(barChild); 52 }); 53 54 it('should fail to find ref in unrelated divs', () => { 55 const unrelated = document.createElement('div'); 56 expect(findRef(unrelated, 'foo')).toBeNull(); 57 expect(findRef(fooChild, 'bar')).toBeNull(); 58 expect(findRef(barChild, 'foo')).toBeNull(); 59 }); 60}); 61 62describe('toHTMLElement', () => { 63 it('should convert a div to an HTMLElement', () => { 64 const divElement: Element = document.createElement('div'); 65 expect(toHTMLElement(divElement)).toEqual(divElement); 66 }); 67 68 it('should fail to convert an svg element to an HTMLElement', () => { 69 const svgElement = 70 document.createElementNS('http://www.w3.org/2000/svg', 'svg'); 71 expect(() => toHTMLElement(svgElement)).toThrow(Error); 72 }); 73}); 74