10
10
11
11
'use strict' ;
12
12
13
- import type { Config } from 'types/Config ' ;
13
+ import type { TestResult } from 'types/TestResult ' ;
14
14
15
15
const ansiEscapes = require ( 'ansi-escapes' ) ;
16
16
const chalk = require ( 'chalk' ) ;
@@ -30,108 +30,106 @@ const usage = () =>
30
30
31
31
const usageRows = usage ( ) . split ( '\n' ) . length ;
32
32
33
- module . exports = (
34
- config : Config ,
35
- pipe : stream$Writable | tty$WriteStream ,
36
- prompt : Prompt ,
37
- ) => {
38
- class TestNamePatternPrompt {
39
- // $FlowFixMe
40
- _cachedTestResults ;
41
-
42
- constructor ( ) {
43
- ( this : any ) . onChange = this . onChange . bind ( this ) ;
44
- }
33
+ module . exports = class TestNamePatternPrompt {
34
+ _cachedTestResults : Array < TestResult > ;
35
+ _pipe: stream$Writable | tty$WriteStream;
36
+ _prompt: Prompt;
45
37
46
- run ( onSuccess : Function , onCancel : Function ) {
47
- pipe . write ( ansiEscapes . cursorHide ) ;
48
- pipe . write ( ansiEscapes . clearScreen ) ;
49
- pipe . write ( usage ( ) ) ;
50
- pipe . write ( ansiEscapes . cursorShow ) ;
38
+ constructor(pipe: stream$Writable | tty$WriteStream, prompt: Prompt ) {
39
+ this . _pipe = pipe ;
40
+ this . _prompt = prompt ;
41
+ ( this : any ) . onChange = this . onChange . bind ( this ) ;
42
+ }
51
43
52
- prompt . enter ( this . onChange , onSuccess , onCancel ) ;
53
- }
44
+ run(onSuccess: Function, onCancel: Function) {
45
+ this . _pipe . write ( ansiEscapes . cursorHide ) ;
46
+ this . _pipe . write ( ansiEscapes . clearScreen ) ;
47
+ this . _pipe . write ( usage ( ) ) ;
48
+ this . _pipe . write ( ansiEscapes . cursorShow ) ;
54
49
55
- onChange ( pattern : string ) {
56
- pipe . write ( ansiEscapes . eraseLine ) ;
57
- pipe . write ( ansiEscapes . cursorLeft ) ;
58
- this . printTypeahead ( pattern , 10 ) ;
59
- }
50
+ this . _prompt . enter ( this . onChange , onSuccess , onCancel ) ;
51
+ }
60
52
61
- printTypeahead ( pattern : string , max : number ) {
62
- const matchedTests = this . getMatchedTests ( pattern ) ;
53
+ onChange(pattern: string) {
54
+ this . _pipe . write ( ansiEscapes . eraseLine ) ;
55
+ this . _pipe . write ( ansiEscapes . cursorLeft ) ;
56
+ this . printTypeahead ( pattern , 10 ) ;
57
+ }
63
58
64
- const total = matchedTests . length ;
65
- const results = matchedTests . slice ( 0 , max ) ;
66
- const inputText = `${ chalk . dim ( ' pattern \u203A' ) } ${ pattern } ` ;
59
+ printTypeahead(pattern: string, max: number) {
60
+ const matchedTests = this . getMatchedTests ( pattern ) ;
67
61
68
- pipe . write ( ansiEscapes . eraseDown ) ;
69
- pipe . write ( inputText ) ;
70
- pipe . write ( ansiEscapes . cursorSavePosition ) ;
62
+ const total = matchedTests . length ;
63
+ const results = matchedTests . slice ( 0 , max ) ;
64
+ const inputText = ` ${ chalk . dim ( ' pattern \u203A' ) } ${ pattern } ` ;
71
65
72
- if ( pattern ) {
73
- if ( total ) {
74
- pipe . write ( `\n\n Pattern matches ${ total } ${ pluralizeTest ( total ) } ` ) ;
75
- } else {
76
- pipe . write ( `\n\n Pattern matches no tests` ) ;
77
- }
66
+ this . _pipe . write ( ansiEscapes . eraseDown ) ;
67
+ this . _pipe . write ( inputText ) ;
68
+ this . _pipe . write ( ansiEscapes . cursorSavePosition ) ;
78
69
79
- pipe . write ( ' from cached test suites.' ) ;
70
+ if ( pattern ) {
71
+ if ( total ) {
72
+ this . _pipe . write (
73
+ `\n\n Pattern matches ${ total } ${ pluralizeTest ( total ) } ` ,
74
+ ) ;
75
+ } else {
76
+ this . _pipe . write ( `\n\n Pattern matches no tests` ) ;
77
+ }
80
78
81
- const width = getTerminalWidth ( ) ;
79
+ this . _pipe . write ( ' from cached test suites.' ) ;
82
80
83
- results . forEach ( name => {
84
- const testName = formatTestNameByPattern ( name , pattern , width - 4 ) ;
81
+ const width = getTerminalWidth ( ) ;
85
82
86
- pipe . write ( `\n ${ chalk . dim ( '\u203A' ) } ${ testName } ` ) ;
87
- } ) ;
83
+ results . forEach ( name => {
84
+ const testName = formatTestNameByPattern ( name , pattern , width - 4 ) ;
88
85
89
- if ( total > max ) {
90
- const more = total - max ;
91
- pipe . write (
92
- // eslint-disable-next-line max-len
93
- `\n ${ chalk . dim ( `\u203A and ${ more } more ${ pluralizeTest ( more ) } ` ) } ` ,
94
- ) ;
95
- }
96
- } else {
97
- pipe . write (
86
+ this . _pipe . write ( `\n ${ chalk . dim ( '\u203A' ) } ${ testName } ` ) ;
87
+ } ) ;
88
+
89
+ if ( total > max ) {
90
+ const more = total - max ;
91
+ this . _pipe . write (
98
92
// eslint-disable-next-line max-len
99
- `\n\n ${ chalk . italic . yellow ( 'Start typing to filter by a test name regex pattern.' ) } ` ,
93
+ `\n ${ chalk . dim ( `\u203A and ${ more } more ${ pluralizeTest ( more ) } ` ) } ` ,
100
94
) ;
101
95
}
102
-
103
- pipe . write ( ansiEscapes . cursorTo ( stringLength ( inputText ) , usageRows - 1 ) ) ;
104
- pipe . write ( ansiEscapes . cursorRestorePosition ) ;
96
+ } else {
97
+ this . _pipe . write (
98
+ // eslint-disable-next-line max-len
99
+ `\n\n ${ chalk . italic . yellow ( 'Start typing to filter by a test name regex pattern.' ) } ` ,
100
+ ) ;
105
101
}
106
102
107
- getMatchedTests ( pattern : string ) {
108
- let regex ;
103
+ this._pipe.write(
104
+ ansiEscapes.cursorTo(stringLength(inputText), usageRows - 1),
105
+ );
106
+ this._pipe.write(ansiEscapes.cursorRestorePosition);
107
+ }
109
108
110
- try {
111
- regex = new RegExp ( pattern , 'i' ) ;
112
- } catch ( e ) {
113
- return [ ] ;
114
- }
109
+ getMatchedTests ( pattern : string ) {
110
+ let regex ;
115
111
116
- const matchedTests = [ ] ;
112
+ try {
113
+ regex = new RegExp ( pattern , 'i' ) ;
114
+ } catch (e) {
115
+ return [ ] ;
116
+ }
117
117
118
- this . _cachedTestResults . forEach ( ( { testResults} ) =>
119
- testResults . forEach ( ( {
120
- title,
121
- } ) => {
122
- if ( regex . test ( title ) ) {
123
- matchedTests . push ( title ) ;
124
- }
125
- } ) ) ;
118
+ const matchedTests = [];
126
119
127
- return matchedTests ;
128
- }
120
+ this._cachedTestResults.forEach(({ testResults } ) =>
121
+ testResults . forEach ( ( {
122
+ title ,
123
+ } ) => {
124
+ if ( regex . test ( title ) ) {
125
+ matchedTests . push ( title ) ;
126
+ }
127
+ } ) ) ;
129
128
130
- // $FlowFixMe
131
- updateCachedTestResults ( testResults ) {
132
- this . _cachedTestResults = testResults || [ ] ;
133
- }
129
+ return matchedTests ;
134
130
}
135
131
136
- return new TestNamePatternPrompt ( ) ;
132
+ updateCachedTestResults ( testResults : Array < TestResult > ) {
133
+ this . _cachedTestResults = testResults || [ ] ;
134
+ }
137
135
} ;
0 commit comments