blob: 0097d9d244e6b763779f5e09777278daaa87aab3 [file] [log] [blame]
// Copyright (C) 2023 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {findRef, isOrContains, toHTMLElement} from './utils';
describe('isOrContains', () => {
const parent = document.createElement('div');
const child = document.createElement('div');
parent.appendChild(child);
it('finds child in parent', () => {
expect(isOrContains(parent, child)).toBeTruthy();
});
it('finds child in child', () => {
expect(isOrContains(child, child)).toBeTruthy();
});
it('does not find parent in child', () => {
expect(isOrContains(child, parent)).toBeFalsy();
});
});
describe('findRef', () => {
const parent = document.createElement('div');
const fooChild = document.createElement('div');
fooChild.setAttribute('ref', 'foo');
parent.appendChild(fooChild);
const barChild = document.createElement('div');
barChild.setAttribute('ref', 'bar');
parent.appendChild(barChild);
it('should find refs in parent divs', () => {
expect(findRef(parent, 'foo')).toEqual(fooChild);
expect(findRef(parent, 'bar')).toEqual(barChild);
});
it('should find refs in self divs', () => {
expect(findRef(fooChild, 'foo')).toEqual(fooChild);
expect(findRef(barChild, 'bar')).toEqual(barChild);
});
it('should fail to find ref in unrelated divs', () => {
const unrelated = document.createElement('div');
expect(findRef(unrelated, 'foo')).toBeNull();
expect(findRef(fooChild, 'bar')).toBeNull();
expect(findRef(barChild, 'foo')).toBeNull();
});
});
describe('toHTMLElement', () => {
it('should convert a div to an HTMLElement', () => {
const divElement: Element = document.createElement('div');
expect(toHTMLElement(divElement)).toEqual(divElement);
});
it('should fail to convert an svg element to an HTMLElement', () => {
const svgElement =
document.createElementNS('http://www.w3.org/2000/svg', 'svg');
expect(() => toHTMLElement(svgElement)).toThrow(Error);
});
});