/
utils.test.ts
157 lines (141 loc) · 4.32 KB
/
utils.test.ts
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* 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 * as path from 'path';
import chalk = require('chalk');
import stripAnsi = require('strip-ansi');
import {makeProjectConfig} from '@jest/test-utils';
import printDisplayName from '../printDisplayName';
import trimAndFormatPath from '../trimAndFormatPath';
import wrapAnsiString from '../wrapAnsiString';
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,
makeProjectConfig({cwd: '', 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,
makeProjectConfig({cwd: '', 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,
makeProjectConfig({cwd: '', 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,
makeProjectConfig({cwd: '', 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,
makeProjectConfig({cwd: '', rootDir: ''}),
path.join(dirname, basename),
columns,
);
expect(result).toMatchSnapshot();
expect(stripAnsi(result).length).toBe(columns - pad);
});
});
describe('printDisplayName', () => {
it('should default displayName color to white when displayName is a string', () => {
expect(
printDisplayName(
makeProjectConfig({
displayName: {
color: 'white',
name: 'hello',
},
}),
),
).toMatchSnapshot();
});
it('should default displayName color to white when color is not a valid value', () => {
expect(
printDisplayName(
makeProjectConfig({
displayName: {
color: 'rubbish' as any,
name: 'hello',
},
}),
),
).toMatchSnapshot();
});
it('should correctly print the displayName when color and name are valid values', () => {
expect(
printDisplayName(
makeProjectConfig({
displayName: {
color: 'green',
name: 'hello',
},
}),
),
).toMatchSnapshot();
});
});