/
utils.test.js
114 lines (101 loc) · 3.1 KB
/
utils.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import path from 'path';
import chalk from 'chalk';
import stripAnsi from 'strip-ansi';
import {trimAndFormatPath, wrapAnsiString} from '../utils';
describe('wrapAnsiString()', () => {
it('wraps a long string containing ansi chars', () => {
const string =
`abcde ${chalk.red.bold('red-bold')} 1234456` +
`${chalk.dim('bcd')} ` +
`123ttttttththththththththththththththththththththth` +
`tetetetetettetetetetetetetete${chalk.underline.bold('stnhsnthsnth')}` +
`ssot`;
expect(wrapAnsiString(string, 10)).toMatchSnapshot();
expect(stripAnsi(wrapAnsiString(string, 10))).toMatchSnapshot();
});
it('returns the string unaltered if given a terminal width of zero', () => {
const string = `This string shouldn't cause you any trouble`;
expect(wrapAnsiString(string, 0)).toMatchSnapshot();
expect(stripAnsi(wrapAnsiString(string, 0))).toMatchSnapshot();
});
});
describe('trimAndFormatPath()', () => {
it('trims dirname', () => {
const pad = 5;
const basename = '1234.js';
const dirname = '1234567890/1234567890';
const columns = 25;
const result = trimAndFormatPath(
pad,
{rootDir: ''},
path.join(dirname, basename),
columns,
);
expect(result).toMatchSnapshot();
expect(stripAnsi(result).length).toBe(20);
});
it('trims dirname (longer line width)', () => {
const pad = 5;
const basename = '1234.js';
const dirname = '1234567890/1234567890';
const columns = 30;
const result = trimAndFormatPath(
pad,
{rootDir: ''},
path.join(dirname, basename),
columns,
);
expect(result).toMatchSnapshot();
expect(stripAnsi(result).length).toBe(25);
});
it('trims dirname and basename', () => {
const pad = 5;
const basename = '1234.js';
const dirname = '1234567890/1234567890';
const columns = 15;
const result = trimAndFormatPath(
pad,
{rootDir: ''},
path.join(dirname, basename),
columns,
);
expect(result).toMatchSnapshot();
expect(stripAnsi(result).length).toBe(10);
});
it('does not trim anything', () => {
const pad = 5;
const basename = '1234.js';
const dirname = '1234567890/1234567890';
const columns = 50;
const totalLength = basename.length + path.sep.length + dirname.length;
const result = trimAndFormatPath(
pad,
{rootDir: ''},
path.join(dirname, basename),
columns,
);
expect(result).toMatchSnapshot();
expect(stripAnsi(result).length).toBe(totalLength);
});
test('split at the path.sep index', () => {
const pad = 5;
const basename = '1234.js';
const dirname = '1234567890';
const columns = 16;
const result = trimAndFormatPath(
pad,
{rootDir: ''},
path.join(dirname, basename),
columns,
);
expect(result).toMatchSnapshot();
expect(stripAnsi(result).length).toBe(columns - pad);
});
});