|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { render } from '@testing-library/react'; |
| 3 | +import parseTextWithLinks from './parseTextWithLinks'; |
| 4 | + |
| 5 | +describe('parseTextWithLinks', () => { |
| 6 | + describe('Basic functionality', () => { |
| 7 | + it('parses text with a single markdown link', () => { |
| 8 | + const text = 'Click [here](https://example.com) to continue'; |
| 9 | + const result = render(parseTextWithLinks(text)); |
| 10 | + |
| 11 | + const link = result.container.querySelector('a'); |
| 12 | + expect(link).not.toBeNull(); |
| 13 | + expect(link?.href).toBe('https://example.com/'); |
| 14 | + expect(link?.textContent).toBe('here'); |
| 15 | + expect(link?.target).toBe('_blank'); |
| 16 | + expect(link?.rel).toBe('noreferrer'); |
| 17 | + expect(link?.getAttribute('aria-label')).toBe( |
| 18 | + 'here - this link will open in a new tab', |
| 19 | + ); |
| 20 | + }); |
| 21 | + |
| 22 | + it('parses text with multiple markdown links', () => { |
| 23 | + const text = 'Visit [Home](https://home.com) or [NHS](https://nhs.uk) for info'; |
| 24 | + const result = render(parseTextWithLinks(text)); |
| 25 | + |
| 26 | + const links = result.container.querySelectorAll('a'); |
| 27 | + expect(links).toHaveLength(2); |
| 28 | + |
| 29 | + expect(links[0]?.href).toBe('https://home.com/'); |
| 30 | + expect(links[0]?.textContent).toBe('Home'); |
| 31 | + |
| 32 | + expect(links[1]?.href).toBe('https://nhs.uk/'); |
| 33 | + expect(links[1]?.textContent).toBe('NHS'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('preserves text before, between, and after links', () => { |
| 37 | + const text = 'Start [link1](url1) middle [link2](url2) end'; |
| 38 | + const result = render(parseTextWithLinks(text)); |
| 39 | + |
| 40 | + expect(result.container.textContent).toBe('Start link1 middle link2 end'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('returns plain text when no markdown links are present', () => { |
| 44 | + const text = 'This is plain text without any links'; |
| 45 | + const result = render(parseTextWithLinks(text)); |
| 46 | + |
| 47 | + expect(result.container.querySelector('a')).toBeNull(); |
| 48 | + expect(result.container.textContent).toBe(text); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('Edge cases', () => { |
| 53 | + it('handles empty string', () => { |
| 54 | + const text = ''; |
| 55 | + const result = render(parseTextWithLinks(text)); |
| 56 | + |
| 57 | + expect(result.container.textContent).toBe(''); |
| 58 | + expect(result.container.querySelector('a')).toBeNull(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('handles text with only a markdown link', () => { |
| 62 | + const text = '[Click here](https://example.com)'; |
| 63 | + const result = render(parseTextWithLinks(text)); |
| 64 | + |
| 65 | + const link = result.container.querySelector('a'); |
| 66 | + expect(link).not.toBeNull(); |
| 67 | + expect(link?.textContent).toBe('Click here'); |
| 68 | + expect(result.container.textContent).toBe('Click here'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('handles consecutive markdown links', () => { |
| 72 | + const text = '[Link1](url1)[Link2](url2)'; |
| 73 | + const result = render(parseTextWithLinks(text)); |
| 74 | + |
| 75 | + const links = result.container.querySelectorAll('a'); |
| 76 | + expect(links).toHaveLength(2); |
| 77 | + expect(result.container.textContent).toBe('Link1Link2'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('handles markdown link at the start', () => { |
| 81 | + const text = '[Start link](url) followed by text'; |
| 82 | + const result = render(parseTextWithLinks(text)); |
| 83 | + |
| 84 | + expect(result.container.textContent).toBe('Start link followed by text'); |
| 85 | + expect(result.container.querySelector('a')?.textContent).toBe('Start link'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('handles markdown link at the end', () => { |
| 89 | + const text = 'Text followed by [end link](url)'; |
| 90 | + const result = render(parseTextWithLinks(text)); |
| 91 | + |
| 92 | + expect(result.container.textContent).toBe('Text followed by end link'); |
| 93 | + expect(result.container.querySelector('a')?.textContent).toBe('end link'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('handles special characters in URL', () => { |
| 97 | + const text = 'Visit [NHS](https://nhs.uk/conditions?query=test&page=1)'; |
| 98 | + const result = render(parseTextWithLinks(text)); |
| 99 | + |
| 100 | + const link = result.container.querySelector('a'); |
| 101 | + expect(link?.href).toBe('https://nhs.uk/conditions?query=test&page=1'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('handles URLs with fragments', () => { |
| 105 | + const text = 'Go to [section](https://example.com#section-1)'; |
| 106 | + const result = render(parseTextWithLinks(text)); |
| 107 | + |
| 108 | + const link = result.container.querySelector('a'); |
| 109 | + expect(link?.href).toBe('https://example.com/#section-1'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('handles relative URLs', () => { |
| 113 | + const text = 'See [documentation](./docs/readme.md)'; |
| 114 | + const result = render(parseTextWithLinks(text)); |
| 115 | + |
| 116 | + const link = result.container.querySelector('a'); |
| 117 | + expect(link).not.toBeNull(); |
| 118 | + expect(link?.textContent).toBe('documentation'); |
| 119 | + }); |
| 120 | + |
| 121 | + it('does not parse incomplete markdown links - missing closing bracket', () => { |
| 122 | + const text = 'This is [incomplete link(url) text'; |
| 123 | + const result = render(parseTextWithLinks(text)); |
| 124 | + |
| 125 | + expect(result.container.querySelector('a')).toBeNull(); |
| 126 | + expect(result.container.textContent).toBe(text); |
| 127 | + }); |
| 128 | + |
| 129 | + it('does not parse incomplete markdown links - missing closing parenthesis', () => { |
| 130 | + const text = 'This is [text](incomplete url text'; |
| 131 | + const result = render(parseTextWithLinks(text)); |
| 132 | + |
| 133 | + expect(result.container.querySelector('a')).toBeNull(); |
| 134 | + expect(result.container.textContent).toBe(text); |
| 135 | + }); |
| 136 | + |
| 137 | + it('handles text inbetween text and link', () => { |
| 138 | + const text = 'Check this link: [example] text (https://example.com)'; |
| 139 | + const result = render(parseTextWithLinks(text)); |
| 140 | + |
| 141 | + const link = result.container.querySelector('a'); |
| 142 | + expect(link).toBeNull(); |
| 143 | + }); |
| 144 | + }); |
| 145 | +}); |
0 commit comments