|
| 1 | +import u from 'unist-builder'; |
| 2 | + |
| 3 | +import exportHeadings from './remark-export-headings'; |
| 4 | + |
| 5 | +/** |
| 6 | + * See all MDAST types here https://github.com/syntax-tree/mdast#root. |
| 7 | + * All nodes are based on the Universal Syntax Tree (unist) system. |
| 8 | + * |
| 9 | + * @typedef {import('unist').Node} Node |
| 10 | + * @typedef {import('unist').Parent} Parent |
| 11 | + */ |
| 12 | + |
| 13 | +describe('exports constant', () => { |
| 14 | + it('when no headers are found', () => { |
| 15 | + const { data } = transform(u('root', [u('text', 'lorem ipsum')])); |
| 16 | + expect(data).not.toBeNull(); |
| 17 | + expect(data).toHaveLength(0); |
| 18 | + }); |
| 19 | + |
| 20 | + it('when single header is found', () => { |
| 21 | + const { data } = transform(u('root', [u('heading', [u('text', 'lorem ipsum')])])); |
| 22 | + expect(data).toHaveLength(1); |
| 23 | + }); |
| 24 | + |
| 25 | + it('when multiple headers are found', () => { |
| 26 | + const { data } = transform( |
| 27 | + u('root', [u('heading', [u('text', 'lorem ipsum')]), u('heading', [u('text', 'sit amet')])]) |
| 28 | + ); |
| 29 | + expect(data).toHaveLength(2); |
| 30 | + }); |
| 31 | +}); |
| 32 | + |
| 33 | +describe('header object', () => { |
| 34 | + it('has title from text child', () => { |
| 35 | + const { data } = transform(u('root', [u('heading', [u('text', 'header title')])])); |
| 36 | + expect(data[0]).toHaveProperty('title', 'header title'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('has title from multiple text children', () => { |
| 40 | + const { data } = transform( |
| 41 | + u('root', [u('heading', [u('text', 'header'), u('text', 'title')])]) |
| 42 | + ); |
| 43 | + expect(data[0]).toHaveProperty('title', 'header title'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('has depth from heading', () => { |
| 47 | + const { data } = transform(u('root', [u('heading', { depth: 3 }, [u('text', 'title')])])); |
| 48 | + expect(data[0]).toHaveProperty('depth', 3); |
| 49 | + }); |
| 50 | + |
| 51 | + it('has id when defined as data', () => { |
| 52 | + const { data } = transform( |
| 53 | + u('root', [u('heading', { data: { id: 'title' } }, [u('text', 'title')])]) |
| 54 | + ); |
| 55 | + expect(data[0]).toHaveProperty('id', 'title'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('has text type from text children', () => { |
| 59 | + const { data } = transform( |
| 60 | + u('root', [u('heading', [u('text', 'hello there'), u('text', 'general kenobi')])]) |
| 61 | + ); |
| 62 | + expect(data[0]).toHaveProperty('type', 'text'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('has inlineCode type from mixed children', () => { |
| 66 | + const { data } = transform( |
| 67 | + u('root', [u('heading', [u('text', 'hello there'), u('inlineCode', 'general kenobi')])]) |
| 68 | + ); |
| 69 | + expect(data[0]).toHaveProperty('type', 'inlineCode'); |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +/** |
| 74 | + * Helper function to run the MDAST transform, and find the added node. |
| 75 | + * |
| 76 | + * @param {Parent} tree |
| 77 | + * @param {object} [options] |
| 78 | + * @param {string} [options.exportName] |
| 79 | + */ |
| 80 | +function transform(tree, options = {}) { |
| 81 | + exportHeadings(options)(tree); |
| 82 | + |
| 83 | + const value = `export const ${options.exportName || 'headings'} = `; |
| 84 | + const node = tree.children |
| 85 | + .reverse() |
| 86 | + .find(node => node.type === 'export' && node.value.startsWith(value)); |
| 87 | + |
| 88 | + const json = node ? node.value.replace(value, '').replace(/;$/, '') : null; |
| 89 | + const data = json ? JSON.parse(json) : null; |
| 90 | + |
| 91 | + return { node, json, data }; |
| 92 | +} |
0 commit comments