aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/util/html.spec.ts
blob: 95af2a2d181f9f371e25097a7be785619cf872f7 (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
33
34
35
36
import * as parser from 'node-html-parser';
import { parse } from './html';

describe('util/html', () => {
  it('parses HTML', () => {
    const body = parse('<div>Hello, world!</div>');
    expect(body.childNodes).toHaveLength(1);
    const div = body.childNodes[0] as parser.HTMLElement;
    expect(div.tagName).toBe('DIV');
    expect(div.textContent).toBe('Hello, world!');
    expect(div instanceof parser.HTMLElement).toBeTrue();
  });

  it('returns empty', () => {
    const body = parse('');
    expect(body.childNodes).toHaveLength(0);
  });

  it('parses HTML: PRE block hides child nodes', () => {
    const body = parse('<div>Hello, world!</div>\n<pre><a>node A</a></pre>');
    const childNodesA = body.querySelectorAll('a');
    expect(childNodesA).toHaveLength(0);
  });

  it('parses HTML: use additional options to discover child nodes on PRE blocks', () => {
    const body = parse('<div>Hello, world!</div>\n<pre><a>node A</a></pre>', {
      blockTextElements: {},
    });
    const childNodesA = body.querySelectorAll('a');
    expect(childNodesA).toHaveLength(1);
    const div = childNodesA[0];
    expect(div.tagName).toBe('A');
    expect(div.textContent).toBe('node A');
    expect(div instanceof parser.HTMLElement).toBe(true);
  });
});