import { DOMParser } from 'xmldom'; import xpath from 'xpath'; import { listItemToLink } from '../convert/navdoc'; import Epub from '../Epub'; const tocNav = ` `; const select = xpath.useNamespaces({ epub: 'http://www.idpf.org/2007/ops', xhtml: 'http://www.w3.org/1999/xhtml', }); function strToDoc(str: string): Document { return new DOMParser().parseFromString(str, 'utf-8'); } function strToLi(str: string): Element { const doc = strToDoc(str); return select('/xhtml:html/xhtml:li', doc, true) as Element; } describe('listItemToLink', () => { const listItemWithoutSpan = strToLi(`
  • Parent
    1. Child 1
    2. Child 2
  • `); const listItemWithSpan = strToLi(`
  • Parent Heading
    1. Child 1
    2. Child 2
  • `); const basicListItem = strToLi(`
  • Basic Title
  • `); const epub = { contentPath: 'OPS/', fetcher: { resolveRelativePath: (from: string, to: string) => { return `OPS/${to}`; }, }, } as Epub; it('extracts link from basic list item', () => { expect(listItemToLink(epub)(basicListItem)).toEqual({ title: 'Basic Title', href: 'OPS/basic-href', }); }); it('extracts link from nested list item', () => { expect(listItemToLink(epub)(listItemWithoutSpan)).toEqual({ title: 'Parent', href: 'OPS/parent-href', children: [ { title: 'Child 1', href: 'OPS/child-1-href' }, { title: 'Child 2', href: 'OPS/child-2-href' }, ], }); }); it('extracts link from list item with instead of ', () => { expect(listItemToLink(epub)(listItemWithSpan)).toEqual({ title: 'Parent Heading', href: 'OPS/child-1-href', children: [ { title: 'Child 1', href: 'OPS/child-1-href' }, { title: 'Child 2', href: 'OPS/child-2-href' }, ], }); }); });