Skip to content

Commit 0734696

Browse files
authored
Get better truncation for testname typeahead by truncating the start and not the end (#28)
* Add pretterrc * Add some tests * Better truncation * Simplify the implementation * Better implementatin * Remove prettier rules config from eslintrc * Update changelog
1 parent 8766546 commit 0734696

File tree

6 files changed

+90
-25
lines changed

6 files changed

+90
-25
lines changed

.eslintrc.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,7 @@
1212
"strict": 0,
1313
"no-underscore-dangle": 0,
1414
"arrow-body-style": 0,
15-
"prettier/prettier": [
16-
"error",
17-
{
18-
"singleQuote": true,
19-
"trailingComma": "all"
20-
}
21-
]
15+
"prettier/prettier": 2
2216
},
2317
"env": {
2418
"jest/globals": true

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all"
4+
}

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Chore & Maintenance
44

55
- Bump dated dependencies ([#25](https://github.com/jest-community/jest-watch-typeahead/pull/25))
6+
- Get better truncation for testname typeahead by truncating the start and not the end ([#28](https://github.com/jest-community/jest-watch-typeahead/pull/28))
67

78
### Fixes
89

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`formatTestNameByPattern formats when testname="the test name", pattern="name", and width="5" 1`] = `"</>...me</>"`;
4+
5+
exports[`formatTestNameByPattern formats when testname="the test name", pattern="name", and width="10" 1`] = `"<dim>...st </></>name</>"`;
6+
7+
exports[`formatTestNameByPattern formats when testname="the test name", pattern="name", and width="15" 1`] = `"<dim>the test </></>name</>"`;
8+
9+
exports[`formatTestNameByPattern formats when testname="the test name", pattern="name", and width="20" 1`] = `"<dim>the test </></>name</>"`;
10+
11+
exports[`formatTestNameByPattern formats when testname="the test name", pattern="name", and width="25" 1`] = `"<dim>the test </></>name</>"`;
12+
13+
exports[`formatTestNameByPattern formats when testname="the test name", pattern="name", and width="30" 1`] = `"<dim>the test </></>name</>"`;
14+
15+
exports[`formatTestNameByPattern formats when testname="the test name", pattern="test", and width="5" 1`] = `"</>...</><dim>me</>"`;
16+
17+
exports[`formatTestNameByPattern formats when testname="the test name", pattern="test", and width="10" 1`] = `"</>...st</><dim> name</>"`;
18+
19+
exports[`formatTestNameByPattern formats when testname="the test name", pattern="test", and width="15" 1`] = `"<dim>the </></>test</><dim> name</>"`;
20+
21+
exports[`formatTestNameByPattern formats when testname="the test name", pattern="test", and width="20" 1`] = `"<dim>the </></>test</><dim> name</>"`;
22+
23+
exports[`formatTestNameByPattern formats when testname="the test name", pattern="test", and width="25" 1`] = `"<dim>the </></>test</><dim> name</>"`;
24+
25+
exports[`formatTestNameByPattern formats when testname="the test name", pattern="test", and width="30" 1`] = `"<dim>the </></>test</><dim> name</>"`;
26+
27+
exports[`formatTestNameByPattern formats when testname="the test name", pattern="the", and width="5" 1`] = `"</>...</><dim>me</>"`;
28+
29+
exports[`formatTestNameByPattern formats when testname="the test name", pattern="the", and width="10" 1`] = `"</>...</><dim>st name</>"`;
30+
31+
exports[`formatTestNameByPattern formats when testname="the test name", pattern="the", and width="15" 1`] = `"</>the</><dim> test name</>"`;
32+
33+
exports[`formatTestNameByPattern formats when testname="the test name", pattern="the", and width="20" 1`] = `"</>the</><dim> test name</>"`;
34+
35+
exports[`formatTestNameByPattern formats when testname="the test name", pattern="the", and width="25" 1`] = `"</>the</><dim> test name</>"`;
36+
37+
exports[`formatTestNameByPattern formats when testname="the test name", pattern="the", and width="30" 1`] = `"</>the</><dim> test name</>"`;

src/lib/__tests__/utils.test.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import stripAnsi from 'strip-ansi';
2-
import { trimAndFormatPath } from '../utils';
2+
import { trimAndFormatPath, formatTestNameByPattern } from '../utils';
33

44
test('trimAndFormatPath', () => {
55
expect(
@@ -8,3 +8,34 @@ test('trimAndFormatPath', () => {
88
),
99
).toEqual('to/you');
1010
});
11+
12+
describe('formatTestNameByPattern', () => {
13+
test.each`
14+
testName | pattern | width
15+
${'the test name'} | ${'the'} | ${30}
16+
${'the test name'} | ${'the'} | ${25}
17+
${'the test name'} | ${'the'} | ${20}
18+
${'the test name'} | ${'the'} | ${15}
19+
${'the test name'} | ${'the'} | ${10}
20+
${'the test name'} | ${'the'} | ${5}
21+
${'the test name'} | ${'test'} | ${30}
22+
${'the test name'} | ${'test'} | ${25}
23+
${'the test name'} | ${'test'} | ${20}
24+
${'the test name'} | ${'test'} | ${15}
25+
${'the test name'} | ${'test'} | ${10}
26+
${'the test name'} | ${'test'} | ${5}
27+
${'the test name'} | ${'name'} | ${30}
28+
${'the test name'} | ${'name'} | ${25}
29+
${'the test name'} | ${'name'} | ${20}
30+
${'the test name'} | ${'name'} | ${15}
31+
${'the test name'} | ${'name'} | ${10}
32+
${'the test name'} | ${'name'} | ${5}
33+
`(
34+
'formats when testname="$testName", pattern="$pattern", and width="$width"',
35+
({ testName, pattern, width }) => {
36+
expect(
37+
formatTestNameByPattern(testName, pattern, width),
38+
).toMatchSnapshot();
39+
},
40+
);
41+
});

src/lib/utils.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -134,26 +134,24 @@ export const formatTestNameByPattern = (
134134
const startPatternIndex = Math.max(match.index, 0);
135135
const endPatternIndex = startPatternIndex + match[0].length;
136136

137-
if (inlineTestName.length <= width) {
137+
const testNameFitsInTerminal = inlineTestName.length <= width;
138+
if (testNameFitsInTerminal) {
138139
return colorize(inlineTestName, startPatternIndex, endPatternIndex);
139140
}
140141

141-
const slicedTestName = inlineTestName.slice(0, width - DOTS.length);
142-
143-
if (startPatternIndex < slicedTestName.length) {
144-
if (endPatternIndex > slicedTestName.length) {
145-
return colorize(
146-
slicedTestName + DOTS,
147-
startPatternIndex,
148-
slicedTestName.length + DOTS.length,
149-
);
150-
}
151-
return colorize(
152-
slicedTestName + DOTS,
153-
Math.min(startPatternIndex, slicedTestName.length),
154-
endPatternIndex,
155-
);
142+
const numberOfTruncatedChars = DOTS.length + inlineTestName.length - width;
143+
const end = Math.max(endPatternIndex - numberOfTruncatedChars, 0);
144+
const truncatedTestName = inlineTestName.slice(numberOfTruncatedChars);
145+
146+
const shouldHighlightDots = startPatternIndex <= numberOfTruncatedChars;
147+
if (shouldHighlightDots) {
148+
return colorize(DOTS + truncatedTestName, 0, end + DOTS.length);
156149
}
157150

158-
return `${chalk.dim(slicedTestName)}${chalk.reset(DOTS)}`;
151+
const start = startPatternIndex - numberOfTruncatedChars;
152+
return colorize(
153+
DOTS + truncatedTestName,
154+
start + DOTS.length,
155+
end + DOTS.length,
156+
);
159157
};

0 commit comments

Comments
 (0)