Skip to content

Commit 29c5e14

Browse files
committed
test: add unit tests for links module
Covers link() and links() behavior: text preservation, fallback URL, theme name acceptance for all builtin themes, and empty-text edge case.
1 parent 08488e1 commit 29c5e14

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

test/unit/links.test.mts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* @fileoverview Unit tests for themed link utilities.
3+
*/
4+
5+
import { describe, expect, it } from 'vitest'
6+
7+
import { link, links } from '@socketsecurity/lib/links'
8+
9+
describe('links', () => {
10+
describe('link()', () => {
11+
it('returns the link text (colored or not)', () => {
12+
const output = link('Docs', 'https://socket.dev')
13+
// Output should always include the text, optionally with ANSI codes.
14+
expect(output).toContain('Docs')
15+
})
16+
17+
it('appends url in parentheses when fallback is true', () => {
18+
const output = link('Docs', 'https://socket.dev', { fallback: true })
19+
expect(output).toContain('Docs')
20+
expect(output).toContain('(https://socket.dev)')
21+
})
22+
23+
it('omits url when fallback is false (default)', () => {
24+
const output = link('Docs', 'https://socket.dev')
25+
expect(output).not.toContain('https://socket.dev')
26+
})
27+
28+
it('accepts a theme name string', () => {
29+
const output = link('Docs', 'https://socket.dev', { theme: 'socket' })
30+
expect(output).toContain('Docs')
31+
})
32+
33+
it('accepts each builtin theme name without throwing', () => {
34+
for (const name of [
35+
'socket',
36+
'sunset',
37+
'terracotta',
38+
'lush',
39+
'ultra',
40+
] as const) {
41+
const output = link('Docs', 'https://socket.dev', { theme: name })
42+
expect(output).toContain('Docs')
43+
}
44+
})
45+
46+
it('works with empty text (may include ANSI reset sequences)', () => {
47+
const output = link('', 'https://socket.dev')
48+
// Empty text with no fallback produces only ANSI color codes around
49+
// the (empty) text. Verify the url is not embedded in output.
50+
expect(output).not.toContain('https://socket.dev')
51+
})
52+
})
53+
54+
describe('links()', () => {
55+
it('returns one formatted output per input pair', () => {
56+
const output = links([
57+
['Docs', 'https://socket.dev'],
58+
['API', 'https://api.socket.dev'],
59+
])
60+
expect(output).toHaveLength(2)
61+
expect(output[0]).toContain('Docs')
62+
expect(output[1]).toContain('API')
63+
})
64+
65+
it('respects the fallback option across all links', () => {
66+
const output = links(
67+
[
68+
['Docs', 'https://socket.dev'],
69+
['API', 'https://api.socket.dev'],
70+
],
71+
{ fallback: true },
72+
)
73+
expect(output[0]).toContain('(https://socket.dev)')
74+
expect(output[1]).toContain('(https://api.socket.dev)')
75+
})
76+
77+
it('returns an empty array for empty input', () => {
78+
expect(links([])).toEqual([])
79+
})
80+
})
81+
})

0 commit comments

Comments
 (0)