Files
vercel/packages/cli/test/unit/util/output/create-output.test.ts
Nathan Rajlich 21f25f5eb0 [cli] Add Output#link() function to format terminal hyperlinks (#8370)
This adds a `link()` helper function to the `Output` class that is inspired by the `terminal-link` npm package.

The main difference with this version is that it's more tightly integrated with the `Output` class for the purposes of being able to toggle hyperlinks support on/off for [unit tests](4a54b19f46/packages/cli/test/unit/util/output/create-output.test.ts) by setting the `output.supportsHyperlink` boolean.

> **Note:** Since hyperlinks are still a relatively new feature, and users might not yet understand how to interact with them, we should only use this function for progressive enhancement scenarios at this time, and _not_ as part of a critical UX.
2022-12-20 02:33:59 +00:00

38 lines
1.4 KiB
TypeScript

import stripAnsi from 'strip-ansi';
import { client } from '../../../mocks/client';
describe('Output', () => {
describe('link()', () => {
it('should return hyperlink ANSI codes when `supportsHyperlink=true`', () => {
client.output.supportsHyperlink = true;
const val = client.output.link('Click Here', 'https://example.com');
expect(val).toEqual(
'\x1B]8;;https://example.com\x07Click Here\x1B]8;;\x07'
);
expect(stripAnsi(val)).toEqual('Click Here');
});
it('should return default fallback when `supportsHyperlink=false`', () => {
client.output.supportsHyperlink = false;
const val = client.output.link('Click Here', 'https://example.com');
expect(val).toEqual('Click Here (https://example.com)');
});
it('should return text fallback when `supportsHyperlink=false` with `fallback: false`', () => {
client.output.supportsHyperlink = false;
const val = client.output.link('Click Here', 'https://example.com', {
fallback: false,
});
expect(val).toEqual('Click Here');
});
it('should return fallback when `supportsHyperlink=false` with `fallback` function', () => {
client.output.supportsHyperlink = false;
const val = client.output.link('Click Here', 'https://example.com', {
fallback: () => 'other',
});
expect(val).toEqual('other');
});
});
});