|
1 | | -import { promptPluginOptions } from './prompts.js'; |
| 1 | +import { promptPluginOptions, promptPluginSelection } from './prompts.js'; |
2 | 2 | import type { PluginPromptDescriptor } from './types.js'; |
3 | 3 |
|
4 | 4 | vi.mock('@inquirer/prompts', () => ({ |
@@ -89,3 +89,137 @@ describe('promptPluginOptions', () => { |
89 | 89 | ).resolves.toStrictEqual({ formats: [] }); |
90 | 90 | }); |
91 | 91 | }); |
| 92 | + |
| 93 | +describe('promptPluginSelection', () => { |
| 94 | + const bindings = [ |
| 95 | + { |
| 96 | + slug: 'eslint', |
| 97 | + title: 'ESLint', |
| 98 | + packageName: '@code-pushup/eslint-plugin', |
| 99 | + generateConfig: () => ({ imports: [], pluginInit: '' }), |
| 100 | + }, |
| 101 | + { |
| 102 | + slug: 'coverage', |
| 103 | + title: 'Code Coverage', |
| 104 | + packageName: '@code-pushup/coverage-plugin', |
| 105 | + generateConfig: () => ({ imports: [], pluginInit: '' }), |
| 106 | + }, |
| 107 | + { |
| 108 | + slug: 'lighthouse', |
| 109 | + title: 'Lighthouse', |
| 110 | + packageName: '@code-pushup/lighthouse-plugin', |
| 111 | + generateConfig: () => ({ imports: [], pluginInit: '' }), |
| 112 | + }, |
| 113 | + ]; |
| 114 | + |
| 115 | + it('should return empty array when given no bindings', async () => { |
| 116 | + await expect(promptPluginSelection([], '/test', {})).resolves.toStrictEqual( |
| 117 | + [], |
| 118 | + ); |
| 119 | + |
| 120 | + expect(mockCheckbox).not.toHaveBeenCalled(); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('--plugins CLI arg', () => { |
| 124 | + it('should return matching bindings for valid slugs', async () => { |
| 125 | + await expect( |
| 126 | + promptPluginSelection(bindings, '/test', { |
| 127 | + plugins: ['eslint', 'lighthouse'], |
| 128 | + }), |
| 129 | + ).resolves.toStrictEqual([bindings[0], bindings[2]]); |
| 130 | + |
| 131 | + expect(mockCheckbox).not.toHaveBeenCalled(); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('--yes (non-interactive)', () => { |
| 136 | + it('should return only recommended plugins when some are recommended', async () => { |
| 137 | + const result = await promptPluginSelection( |
| 138 | + [ |
| 139 | + { ...bindings[0]!, isRecommended: () => Promise.resolve(true) }, |
| 140 | + bindings[1]!, |
| 141 | + bindings[2]!, |
| 142 | + ], |
| 143 | + '/test', |
| 144 | + { yes: true }, |
| 145 | + ); |
| 146 | + |
| 147 | + expect(result).toBeArrayOfSize(1); |
| 148 | + expect(result[0]).toHaveProperty('slug', 'eslint'); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should return no plugins when none are recommended', async () => { |
| 152 | + await expect( |
| 153 | + promptPluginSelection(bindings, '/test', { yes: true }), |
| 154 | + ).resolves.toBeArrayOfSize(0); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + describe('interactive prompt', () => { |
| 159 | + it('should pre-check recommended plugins and leave others unchecked', async () => { |
| 160 | + mockCheckbox.mockResolvedValue(['eslint']); |
| 161 | + |
| 162 | + await promptPluginSelection( |
| 163 | + [ |
| 164 | + { ...bindings[0]!, isRecommended: () => Promise.resolve(true) }, |
| 165 | + bindings[1]!, |
| 166 | + bindings[2]!, |
| 167 | + ], |
| 168 | + '/test', |
| 169 | + {}, |
| 170 | + ); |
| 171 | + |
| 172 | + expect(mockCheckbox).toHaveBeenCalledWith( |
| 173 | + expect.objectContaining({ |
| 174 | + required: true, |
| 175 | + choices: [ |
| 176 | + { name: 'ESLint', value: 'eslint', checked: true }, |
| 177 | + { name: 'Code Coverage', value: 'coverage', checked: false }, |
| 178 | + { name: 'Lighthouse', value: 'lighthouse', checked: false }, |
| 179 | + ], |
| 180 | + }), |
| 181 | + ); |
| 182 | + }); |
| 183 | + |
| 184 | + it('should not pre-check any plugins when none are recommended', async () => { |
| 185 | + mockCheckbox.mockResolvedValue(['eslint']); |
| 186 | + |
| 187 | + await promptPluginSelection(bindings, '/test', {}); |
| 188 | + |
| 189 | + expect(mockCheckbox).toHaveBeenCalledWith( |
| 190 | + expect.objectContaining({ |
| 191 | + required: true, |
| 192 | + choices: [ |
| 193 | + { name: 'ESLint', value: 'eslint', checked: false }, |
| 194 | + { name: 'Code Coverage', value: 'coverage', checked: false }, |
| 195 | + { name: 'Lighthouse', value: 'lighthouse', checked: false }, |
| 196 | + ], |
| 197 | + }), |
| 198 | + ); |
| 199 | + }); |
| 200 | + |
| 201 | + it('should return only user-selected bindings', async () => { |
| 202 | + mockCheckbox.mockResolvedValue(['coverage']); |
| 203 | + |
| 204 | + await expect( |
| 205 | + promptPluginSelection(bindings, '/test', {}), |
| 206 | + ).resolves.toStrictEqual([bindings[1]]); |
| 207 | + }); |
| 208 | + }); |
| 209 | + |
| 210 | + describe('isRecommended callback', () => { |
| 211 | + it('should receive targetDir as argument', async () => { |
| 212 | + const isRecommended = vi.fn().mockResolvedValue(false); |
| 213 | + |
| 214 | + mockCheckbox.mockResolvedValue(['eslint']); |
| 215 | + |
| 216 | + await promptPluginSelection( |
| 217 | + [{ ...bindings[0]!, isRecommended }], |
| 218 | + '/my/project', |
| 219 | + {}, |
| 220 | + ); |
| 221 | + |
| 222 | + expect(isRecommended).toHaveBeenCalledWith('/my/project'); |
| 223 | + }); |
| 224 | + }); |
| 225 | +}); |
0 commit comments