|
1 | 1 | import process from 'node:process'; |
2 | | -import test from 'ava'; |
3 | | -import React, {type FC as ReactFC, type ReactNode} from 'react'; |
| 2 | +import {test, afterEach} from 'node:test'; |
| 3 | +import assert from 'node:assert/strict'; |
| 4 | +import React from 'react'; |
4 | 5 | import {Text} from 'ink'; |
5 | 6 | import {render} from 'ink-testing-library'; |
6 | 7 | import Link from './source/index.js'; |
7 | 8 |
|
8 | | -test.afterEach(() => { |
| 9 | +afterEach(() => { |
9 | 10 | delete process.env.FORCE_HYPERLINK; |
10 | 11 | }); |
11 | 12 |
|
12 | | -test('render', t => { |
| 13 | +test('render', () => { |
13 | 14 | if (process.env.CI) { |
14 | | - t.pass(); |
15 | 15 | return; |
16 | 16 | } |
17 | 17 |
|
18 | | - process.env.FORCE_HYPERLINK = 1; |
| 18 | + process.env.FORCE_HYPERLINK = '1'; |
19 | 19 |
|
20 | | - const {lastFrame} = render( |
21 | | - <Link url='https://sindresorhus.com'> |
22 | | - My{' '}<Text color='green'>Website</Text> |
23 | | - </Link>, |
24 | | - ); |
25 | | - console.log(lastFrame()); |
26 | | - t.snapshot(lastFrame()); |
| 20 | + const result = render(<Link url='https://sindresorhus.com'> |
| 21 | + My{' '}<Text color='green'>Website</Text> |
| 22 | + </Link>); |
| 23 | + console.log('render:', result.lastFrame()); |
| 24 | + // With hyperlinks enabled, should contain the hyperlink escape sequences |
| 25 | + assert.ok(result.lastFrame().includes('sindresorhus.com')); |
27 | 26 | }); |
28 | 27 |
|
29 | | -test('render fallback', t => { |
30 | | - process.env.FORCE_HYPERLINK = 0; |
| 28 | +test('render fallback', () => { |
| 29 | + process.env.FORCE_HYPERLINK = '0'; |
31 | 30 |
|
32 | | - const {lastFrame} = render( |
33 | | - <Link url='https://sindresorhus.com'> |
34 | | - My Website |
35 | | - </Link>, |
36 | | - ); |
37 | | - console.log(lastFrame()); |
38 | | - t.snapshot(lastFrame()); |
| 31 | + const result = render(<Link url='https://sindresorhus.com'> |
| 32 | + My Website |
| 33 | + </Link>); |
| 34 | + console.log('render fallback:', result.lastFrame()); |
| 35 | + // New terminal-link v5 format: "text url" (no parens) |
| 36 | + assert.strictEqual(result.lastFrame(), 'My Website https://sindresorhus.com'); |
39 | 37 | }); |
40 | 38 |
|
41 | | -test('exclude fallback if disabled', t => { |
42 | | - process.env.FORCE_HYPERLINK = 0; |
| 39 | +test('exclude fallback if disabled', () => { |
| 40 | + process.env.FORCE_HYPERLINK = '0'; |
43 | 41 |
|
44 | | - const {lastFrame} = render( |
45 | | - <Link url='https://sindresorhus.com' fallback={false}> |
46 | | - My Website |
47 | | - </Link>, |
48 | | - ); |
49 | | - console.log(lastFrame()); |
50 | | - t.snapshot(lastFrame()); |
| 42 | + const result = render(<Link url='https://sindresorhus.com' fallback={false}> |
| 43 | + My Website |
| 44 | + </Link>); |
| 45 | + console.log('exclude fallback:', result.lastFrame()); |
| 46 | + assert.strictEqual(result.lastFrame(), 'My Website'); |
51 | 47 | }); |
52 | 48 |
|
53 | | -test('include fallback if explicitly enabled', t => { |
54 | | - process.env.FORCE_HYPERLINK = 0; |
| 49 | +test('include fallback if explicitly enabled', () => { |
| 50 | + process.env.FORCE_HYPERLINK = '0'; |
55 | 51 |
|
56 | | - const {lastFrame} = render( |
57 | | - <Link fallback url='https://sindresorhus.com'> |
58 | | - My Website |
59 | | - </Link>, |
60 | | - ); |
61 | | - console.log(lastFrame()); |
62 | | - t.snapshot(lastFrame()); |
| 52 | + const result = render(<Link fallback url='https://sindresorhus.com'> |
| 53 | + My Website |
| 54 | + </Link>); |
| 55 | + console.log('include fallback:', result.lastFrame()); |
| 56 | + // New terminal-link v5 format: "text url" (no parens) |
| 57 | + assert.strictEqual(result.lastFrame(), 'My Website https://sindresorhus.com'); |
63 | 58 | }); |
64 | 59 |
|
65 | | -test('custom fallback function', t => { |
66 | | - process.env.FORCE_HYPERLINK = 0; |
| 60 | +test('custom fallback function', () => { |
| 61 | + process.env.FORCE_HYPERLINK = '0'; |
67 | 62 |
|
68 | 63 | const customFallback = (text: string, url: string) => `[${text}](${url})`; |
69 | 64 |
|
70 | | - const {lastFrame} = render( |
71 | | - <Link url='https://sindresorhus.com' fallback={customFallback}> |
72 | | - My Website |
73 | | - </Link>, |
74 | | - ); |
75 | | - console.log(lastFrame()); |
76 | | - t.is(lastFrame(), '[My Website](https://sindresorhus.com)'); |
| 65 | + const result = render(<Link url='https://sindresorhus.com' fallback={customFallback}> |
| 66 | + My Website |
| 67 | + </Link>); |
| 68 | + console.log('custom fallback:', result.lastFrame()); |
| 69 | + assert.strictEqual(result.lastFrame(), '[My Website](https://sindresorhus.com)'); |
77 | 70 | }); |
78 | 71 |
|
79 | | -test('custom fallback function with complex text', t => { |
80 | | - process.env.FORCE_HYPERLINK = 0; |
| 72 | +test('custom fallback function with complex text', () => { |
| 73 | + process.env.FORCE_HYPERLINK = '0'; |
81 | 74 |
|
82 | 75 | const customFallback = (text: string, url: string) => `${text} -> ${url}`; |
83 | 76 |
|
84 | | - const {lastFrame} = render( |
85 | | - <Link url='https://example.com/path?query=value' fallback={customFallback}> |
86 | | - Visit <Text color='cyan'>our site</Text> |
87 | | - </Link>, |
88 | | - ); |
89 | | - console.log(lastFrame()); |
| 77 | + const result = render(<Link url='https://example.com/path?query=value' fallback={customFallback}> |
| 78 | + Visit <Text color='cyan'>our site</Text> |
| 79 | + </Link>); |
| 80 | + console.log('complex fallback:', result.lastFrame()); |
90 | 81 | // The text includes ANSI color codes from the cyan Text component |
91 | | - t.regex(lastFrame(), /Visit.*our site.* -> https:\/\/example\.com\/path\?query=value/); |
| 82 | + assert.match(result.lastFrame(), /Visit.*our site.* -> https:\/\/example\.com\/path\?query=value/); |
92 | 83 | }); |
0 commit comments