|
| 1 | +import { getAnchor, getAnchorText, serializeNodeToInlineText } from './toc'; |
| 2 | + |
| 3 | +const plainHeading = { |
| 4 | + type: 'h2', |
| 5 | + children: [{ text: 'Subtitle 1: Everything is okay' }], |
| 6 | +}; |
| 7 | + |
| 8 | +const italicHeading = { |
| 9 | + type: 'h2', |
| 10 | + children: [ |
| 11 | + { text: 'Subtitle 2: Everything is ' }, |
| 12 | + { text: 'not', italic: true }, |
| 13 | + { text: ' okay' }, |
| 14 | + ], |
| 15 | +}; |
| 16 | + |
| 17 | +const partiallyItalicWordHeading = { |
| 18 | + type: 'h2', |
| 19 | + children: [ |
| 20 | + { text: 'Subtitle 3: n' }, |
| 21 | + { text: 'o', italic: true }, |
| 22 | + { text: 't okay' }, |
| 23 | + ], |
| 24 | +}; |
| 25 | + |
| 26 | +const linkHeading = { |
| 27 | + type: 'h2', |
| 28 | + children: [ |
| 29 | + { text: 'Subtitle 4: Everything is ' }, |
| 30 | + { |
| 31 | + type: 'link', |
| 32 | + data: { url: '/some-page' }, |
| 33 | + children: [{ text: 'okay' }], |
| 34 | + }, |
| 35 | + { text: '' }, |
| 36 | + ], |
| 37 | +}; |
| 38 | + |
| 39 | +describe('serializeNodeToInlineText', () => { |
| 40 | + it('returns the text of a node without a type', () => { |
| 41 | + expect(serializeNodeToInlineText({ text: 'Hello' })).toBe('Hello'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('does not add whitespace between the leafs of a node', () => { |
| 45 | + expect(serializeNodeToInlineText(partiallyItalicWordHeading)).toBe( |
| 46 | + 'Subtitle 3: not okay', |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + it('includes the text of inline elements', () => { |
| 51 | + expect(serializeNodeToInlineText(linkHeading)).toBe( |
| 52 | + 'Subtitle 4: Everything is okay', |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + it('returns an empty string for an empty node', () => { |
| 57 | + expect(serializeNodeToInlineText(undefined)).toBe(''); |
| 58 | + expect(serializeNodeToInlineText({ type: 'h2' })).toBe(''); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe('getAnchorText', () => { |
| 63 | + it('collapses the inner whitespace and trims the outer one', () => { |
| 64 | + expect( |
| 65 | + getAnchorText({ |
| 66 | + type: 'h2', |
| 67 | + children: [{ text: ' Some ' }, { text: '' }, { text: 'heading ' }], |
| 68 | + }), |
| 69 | + ).toBe('Some heading'); |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +describe('getAnchor', () => { |
| 74 | + it('slugs a heading', () => { |
| 75 | + expect(getAnchor(plainHeading)).toBe('subtitle-1-everything-is-okay'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('is not affected by inline formatting', () => { |
| 79 | + expect(getAnchor(italicHeading)).toBe('subtitle-2-everything-is-not-okay'); |
| 80 | + expect(getAnchor(partiallyItalicWordHeading)).toBe('subtitle-3-not-okay'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('is not affected by inline links', () => { |
| 84 | + expect(getAnchor(linkHeading)).toBe('subtitle-4-everything-is-okay'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('returns an empty string for an empty heading', () => { |
| 88 | + expect(getAnchor({ type: 'h2', children: [{ text: '' }] })).toBe(''); |
| 89 | + }); |
| 90 | +}); |
0 commit comments