|
| 1 | +import {Answers, PromptModule, QuestionCollection} from 'inquirer'; |
| 2 | +import input from './input'; |
| 3 | +import InputCustomPrompt from './inquirer/InputCustomPrompt'; |
| 4 | +import chalk from 'chalk'; |
| 5 | + |
| 6 | +jest.mock( |
| 7 | + '@commitlint/load', |
| 8 | + () => { |
| 9 | + return () => require('@commitlint/config-angular'); |
| 10 | + }, |
| 11 | + { |
| 12 | + virtual: true, |
| 13 | + } |
| 14 | +); |
| 15 | + |
| 16 | +test('should work with all fields filled', async () => { |
| 17 | + const prompt = stub({ |
| 18 | + 'input-custom': { |
| 19 | + type: 'fix', |
| 20 | + scope: 'test', |
| 21 | + subject: 'subject', |
| 22 | + body: 'body', |
| 23 | + footer: 'footer', |
| 24 | + }, |
| 25 | + }); |
| 26 | + const message = await input(prompt); |
| 27 | + expect(message).toEqual('fix(test): subject\n' + 'body\n' + 'footer'); |
| 28 | +}); |
| 29 | + |
| 30 | +test('should work without scope', async () => { |
| 31 | + const prompt = stub({ |
| 32 | + 'input-custom': { |
| 33 | + type: 'fix', |
| 34 | + scope: '', |
| 35 | + subject: 'subject', |
| 36 | + body: 'body', |
| 37 | + footer: 'footer', |
| 38 | + }, |
| 39 | + }); |
| 40 | + const message = await input(prompt); |
| 41 | + expect(message).toEqual('fix: subject\n' + 'body\n' + 'footer'); |
| 42 | +}); |
| 43 | + |
| 44 | +test('should fail without type', async () => { |
| 45 | + const spy = jest.spyOn(console, 'error').mockImplementation(); |
| 46 | + const prompt = stub({ |
| 47 | + 'input-custom': { |
| 48 | + type: '', |
| 49 | + scope: '', |
| 50 | + subject: '', |
| 51 | + body: '', |
| 52 | + footer: '', |
| 53 | + }, |
| 54 | + }); |
| 55 | + const message = await input(prompt); |
| 56 | + expect(message).toEqual(''); |
| 57 | + expect(console.error).toHaveBeenCalledTimes(1); |
| 58 | + expect(console.error).toHaveBeenLastCalledWith( |
| 59 | + new Error(`⚠ ${chalk.bold('type')} may not be empty.`) |
| 60 | + ); |
| 61 | + spy.mockRestore(); |
| 62 | +}); |
| 63 | + |
| 64 | +function stub(config: Record<string, Record<string, unknown>>): PromptModule { |
| 65 | + const prompt = async (questions: QuestionCollection): Promise<any> => { |
| 66 | + const result: Answers = {}; |
| 67 | + const resolvedConfig = Array.isArray(questions) ? questions : [questions]; |
| 68 | + for (const promptConfig of resolvedConfig) { |
| 69 | + const configType = promptConfig.type || 'input'; |
| 70 | + const questions = config[configType]; |
| 71 | + if (!questions) { |
| 72 | + throw new Error(`Unexpected config type: ${configType}`); |
| 73 | + } |
| 74 | + const answer = questions[promptConfig.name!]; |
| 75 | + if (answer == null) { |
| 76 | + throw new Error(`Unexpected config name: ${promptConfig.name}`); |
| 77 | + } |
| 78 | + let validate = promptConfig.validate; |
| 79 | + if (promptConfig.type === 'input-custom') { |
| 80 | + const customInput = new InputCustomPrompt( |
| 81 | + promptConfig, |
| 82 | + {write: () => true} as any, |
| 83 | + result |
| 84 | + ) as any; |
| 85 | + validate = customInput.opt.validate.bind(customInput); |
| 86 | + } |
| 87 | + if (validate) { |
| 88 | + const validationResult = validate(answer, result); |
| 89 | + if (validationResult !== true) { |
| 90 | + throw new Error(validationResult || undefined); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + result[promptConfig.name!] = answer; |
| 95 | + } |
| 96 | + return result; |
| 97 | + }; |
| 98 | + prompt.registerPrompt = () => { |
| 99 | + return prompt; |
| 100 | + }; |
| 101 | + prompt.restoreDefaultPrompts = () => true; |
| 102 | + prompt.prompts = {}; |
| 103 | + return (prompt as any) as PromptModule; |
| 104 | +} |
0 commit comments