liubinfighter_csv-lite/test/url-utils.test.ts
JayBridge e7d2fe56ca feat: enhance clickable URLs feature to support Markdown links
- Updated the clickable URLs functionality to automatically detect and render both plain-text URLs and Markdown-style links in CSV cells.
- Enhanced documentation to reflect the new capabilities and provide examples for users.
- Added tests to ensure proper detection and parsing of Markdown links alongside plain URLs.
2026-02-01 14:01:08 +08:00

151 lines
5.3 KiB
TypeScript

import { containsUrl, parseTextWithUrls, createUrlDisplay } from '../src/utils/url-utils';
describe('URL Utils', () => {
describe('containsUrl', () => {
it('should detect HTTP URLs', () => {
expect(containsUrl('http://example.com')).toBe(true);
expect(containsUrl('Visit http://example.com for more')).toBe(true);
});
it('should detect HTTPS URLs', () => {
expect(containsUrl('https://example.com')).toBe(true);
expect(containsUrl('Check out https://github.com')).toBe(true);
});
it('should detect URLs with paths', () => {
expect(containsUrl('https://example.com/path/to/page')).toBe(true);
expect(containsUrl('http://site.com/docs?q=test')).toBe(true);
});
it('should detect URLs with query parameters', () => {
expect(containsUrl('https://example.com?foo=bar&baz=qux')).toBe(true);
});
it('should detect URLs with fragments', () => {
expect(containsUrl('https://example.com#section')).toBe(true);
expect(containsUrl('https://example.com/page#top')).toBe(true);
});
it('should return false for non-URLs', () => {
expect(containsUrl('Just plain text')).toBe(false);
expect(containsUrl('example.com')).toBe(false); // No protocol
expect(containsUrl('www.example.com')).toBe(false); // No protocol
expect(containsUrl('')).toBe(false);
});
it('should handle multiple URLs in text', () => {
expect(containsUrl('Visit https://site1.com and https://site2.com')).toBe(true);
});
it('should detect Markdown-style links', () => {
expect(containsUrl('[GitHub](https://github.com)')).toBe(true);
expect(containsUrl('Check [this link](https://example.com) out')).toBe(true);
});
it('should detect mixed URLs and Markdown links', () => {
expect(containsUrl('[GitHub](https://github.com) and https://example.com')).toBe(true);
});
});
describe('parseTextWithUrls', () => {
it('should parse text with no URLs', () => {
const result = parseTextWithUrls('Just plain text');
expect(result).toHaveLength(1);
expect(result[0]).toEqual({
text: 'Just plain text',
isUrl: false
});
});
it('should parse text with single URL', () => {
const result = parseTextWithUrls('Visit https://example.com today');
expect(result).toHaveLength(3);
expect(result[0]).toEqual({
text: 'Visit ',
isUrl: false
});
expect(result[1]).toEqual({
text: 'https://example.com',
isUrl: true,
url: 'https://example.com',
displayText: 'https://example.com'
});
expect(result[2]).toEqual({
text: ' today',
isUrl: false
});
});
it('should parse text with multiple URLs', () => {
const result = parseTextWithUrls('Check https://site1.com and https://site2.com out');
expect(result).toHaveLength(5);
expect(result[1].isUrl).toBe(true);
expect(result[3].isUrl).toBe(true);
});
it('should parse URL at start of text', () => {
const result = parseTextWithUrls('https://example.com is great');
expect(result[0]).toEqual({
text: 'https://example.com',
isUrl: true,
url: 'https://example.com',
displayText: 'https://example.com'
});
});
it('should parse URL at end of text', () => {
const result = parseTextWithUrls('Visit https://example.com');
expect(result[result.length - 1]).toEqual({
text: 'https://example.com',
isUrl: true,
url: 'https://example.com',
displayText: 'https://example.com'
});
});
it('should parse URL with complex path', () => {
const url = 'https://example.com/path/to/page?param=value&other=123#section';
const result = parseTextWithUrls(url);
expect(result).toHaveLength(1);
expect(result[0].isUrl).toBe(true);
expect(result[0].url).toBe(url);
});
it('should parse Markdown-style link', () => {
const result = parseTextWithUrls('[GitHub](https://github.com)');
expect(result).toHaveLength(1);
expect(result[0].isUrl).toBe(true);
expect(result[0].url).toBe('https://github.com');
expect(result[0].displayText).toBe('GitHub');
});
it('should parse text with Markdown link and plain text', () => {
const result = parseTextWithUrls('Visit [GitHub](https://github.com) now');
expect(result).toHaveLength(3);
expect(result[0]).toEqual({
text: 'Visit ',
isUrl: false
});
expect(result[1].isUrl).toBe(true);
expect(result[1].url).toBe('https://github.com');
expect(result[1].displayText).toBe('GitHub');
expect(result[2]).toEqual({
text: ' now',
isUrl: false
});
});
it('should parse mixed Markdown links and plain URLs', () => {
const result = parseTextWithUrls('[GitHub](https://github.com) and https://example.com');
expect(result).toHaveLength(3);
expect(result[0].isUrl).toBe(true);
expect(result[0].displayText).toBe('GitHub');
expect(result[1].text).toBe(' and ');
expect(result[2].isUrl).toBe(true);
expect(result[2].url).toBe('https://example.com');
});
});
// Note: createUrlDisplay tests are skipped as they require DOM environment
// The function is tested through integration tests in the actual plugin
});