1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
import { findChildByTagName } from '../../../content/frontend/shared/dom';
describe('frontend/shared/dom', () => {
const createElementWithChildren = children => {
const el = document.createElement('div');
children.forEach(tag => {
const child = document.createElement(tag);
el.appendChild(child);
});
return el;
};
describe('findChildByTagName', () => {
it.each`
children | tagName | expectedIndex
${['div', 'p', 'ul', 'p']} | ${'p'} | ${1}
${['div', 'ul']} | ${'p'} | ${-1}
${['li', 'li', 'li']} | ${'li'} | ${0}
${[]} | ${'li'} | ${-1}
`(
'with children $children and $tagName, returns $expectedIndex child',
({ children, tagName, expectedIndex }) => {
const el = createElementWithChildren(children);
const expectedChild = el.childNodes[expectedIndex];
expect(findChildByTagName(el, tagName)).toBe(expectedChild);
},
);
});
});
|