-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.spec.ts
261 lines (224 loc) · 7.05 KB
/
index.spec.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// let mockLastSavedResult = {}
// let mockLastLoadedResult = {}
import stripAnsi from 'strip-ansi'
import { SavedEntries, SubmittedChoice } from './interfaces'
const cleanupFunctionsToCall: Array<() => void> = []
let mockLastPrinted: string
const setup = async () => {
global.console.log = jest.fn() // console.log are ignored in tests
// Keep native behaviour for other methods, use those to print out things in your own tests, not `console.log`
global.console.error = console.error
global.console.warn = console.warn
global.console.info = console.info
global.console.debug = console.debug
let mockLastSavedResult
let mockLastLoadedResult: SavedEntries
jest.mock('./save-load', () => ({
save: jest.fn((directory: string, data: Array<SubmittedChoice>) => {
// eslint-disable-next-line
mockLastSavedResult = data
}),
load: jest.fn(() => {
// faked stored data
mockLastLoadedResult = {}
return mockLastLoadedResult
})
}))
jest.mock('find-up', () => ({
sync: jest.fn(() => {
return 'mocks/rush.json'
})
}))
jest.mock('@lerna/child-process', () => ({
spawnStreaming: jest.fn(() => ({
once: () => Promise.resolve()
}))
}))
jest.mock('./choice-generation', () => ({
createChoices: jest.fn(() => ({
choices: [
{
name: 'browser-package',
category: 'libraries',
availableScripts: ['build', 'build:prod', 'test']
},
{
name: 'mfe-package-a',
category: 'micro-frontends',
availableScripts: ['build', 'build:prod']
},
{
name: 'mfe-package-b',
category: 'micro-frontends',
availableScripts: ['build', 'build:prod']
},
{
name: 'cli-package',
category: 'tooling',
availableScripts: ['build', 'build:prod']
},
{
name: 'random-package',
category: undefined,
availableScripts: ['build', 'build:prod']
}
],
allScriptNames: ['build', 'build:prod', 'test']
})),
applySelectedScriptsOnChoicesFromCache: jest.fn()
}))
let mockPromptInstance: any
let mockRunPromise: any
let mockReadyResolve: any
const promptReadyPromise = new Promise((resolve: any) => {
mockReadyResolve = resolve
})
jest.mock('./prompt', () => {
return class RushSelect extends jest.requireActual('./prompt').default {
stdout: any
constructor(options: any) {
super(options)
mockPromptInstance = this
this.stdout = {
write: jest.fn((text) => {
text = stripAnsi(text)
if (text !== '') {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
mockLastPrinted = text
}
}),
removeListener: jest.fn()
}
}
async run() {
mockRunPromise = super.run()
return mockRunPromise
// returning the answers immediately here can be helpful too.
// return []
}
async reset() {
await super.reset()
mockReadyResolve()
}
}
})
// start the prompt
require('./index')
// wait for initial render completion
await promptReadyPromise
const runAndSubmit = () => {
// perform and await the final submit step
mockPromptInstance.submit()
return mockRunPromise
}
cleanupFunctionsToCall.push(runAndSubmit)
return {
promptInstance: mockPromptInstance,
runAndSubmit
}
}
describe('index', () => {
beforeEach(async () => {
jest.resetModules()
jest.clearAllMocks()
})
afterEach(async () => {
cleanupFunctionsToCall.splice(0).forEach((f) => f())
})
test('submitting immediately should leave basically empty results', async () => {
const { runAndSubmit } = await setup()
expect(await runAndSubmit()).toEqual([
{
packageName: 'rush build',
script: 'smart',
scriptExecutable: 'rush',
scriptCommand: []
}
])
})
test('should list some packages', async () => {
const { promptInstance, runAndSubmit } = await setup()
await promptInstance.down()
await promptInstance.down()
await promptInstance.right()
await promptInstance.right()
expect(await runAndSubmit()).toEqual([
{
packageName: 'rush build',
script: 'smart',
scriptExecutable: 'rush',
scriptCommand: []
},
{
packageName: 'random-package',
script: 'build:prod',
scriptExecutable: undefined,
scriptCommand: undefined
}
])
})
test('scrolling', async () => {
const { promptInstance } = await setup()
for (let i = 0; i < 30; i++) {
await promptInstance.down()
}
expect(promptInstance.choices[promptInstance.index].name).toBe('random-package')
for (let i = 0; i < 60; i++) {
await promptInstance.up()
}
expect(promptInstance.choices[promptInstance.index].name).toBe('mfe-package-b')
})
test('navigation should be intact', async () => {
const { promptInstance } = await setup()
await promptInstance.down()
await promptInstance.down()
await promptInstance.down()
expect(promptInstance.choices[promptInstance.index].name).toBe('browser-package')
await promptInstance.down()
await promptInstance.down()
expect(promptInstance.choices[promptInstance.index].name).toBe('mfe-package-b')
await promptInstance.up()
expect(promptInstance.choices[promptInstance.index].name).toBe('mfe-package-a')
await promptInstance.up()
expect(promptInstance.choices[promptInstance.index].name).toBe('browser-package')
await promptInstance.down()
await promptInstance.down()
await promptInstance.up()
await promptInstance.up()
expect(promptInstance.choices[promptInstance.index].name).toBe('browser-package')
})
test('filtering', async () => {
const { promptInstance, runAndSubmit } = await setup()
promptInstance.onKeyPress('r', {})
promptInstance.onKeyPress('a', {})
promptInstance.onKeyPress('n', {})
promptInstance.onKeyPress('d', {})
await promptInstance.right()
expect(promptInstance.visible).toHaveLength(1)
expect(promptInstance.visible[0].name).toBe('random-package')
expect(promptInstance.visible[0].scaleIndex).toBe(1)
promptInstance.onKeyPress('', { action: 'delete' })
expect(promptInstance.visible).toHaveLength(1)
expect(promptInstance.visible[0].name).toBe('random-package')
expect(promptInstance.visible[0].scaleIndex).toBe(1)
promptInstance.onKeyPress('', { action: 'delete' })
promptInstance.onKeyPress('', { action: 'delete' })
promptInstance.onKeyPress('', { action: 'delete' })
promptInstance.onKeyPress('', { action: 'delete' })
const result = await runAndSubmit()
expect(result).toEqual([
{
packageName: 'rush build',
script: 'smart',
scriptExecutable: 'rush',
scriptCommand: []
},
{
packageName: 'random-package',
script: 'build',
scriptExecutable: undefined,
scriptCommand: undefined
}
])
})
})