Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dom_spec.js « shared « frontend « spec - gitlab.com/gitlab-org/gitlab-docs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ceb097080429eccb5b65d8fa60a0603e6761ad3d (plain)
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);
      },
    );
  });
});