|
| 1 | +const path = require('path'); |
| 2 | +const { createCommandInterface, parseOutput } = require('cli-testing-tool'); |
| 3 | +const cleanUp = require('./utils/cleanup'); |
| 4 | +const { STRING_ESC } = require('cli-testing-tool/lib/cli-ansi-parser'); |
| 5 | + |
| 6 | +const NO_PROJECT_FOUND_ERROR = |
| 7 | + '[BOLD_START][RED_START]>>>[COLOR_END][BOLD_END] No projects to open :('; |
| 8 | +const CD_TO_ADD_PROJECT_MSG = |
| 9 | + // eslint-disable-next-line max-len |
| 10 | + '[BOLD_START][BLUE_START]>>>[COLOR_END][BOLD_END] cd /till/project/directory/ and run [YELLOW_START]pm add[COLOR_END] to add projects and get started'; |
| 11 | + |
| 12 | +async function addProject(dir) { |
| 13 | + const addCommandInterface = createCommandInterface( |
| 14 | + `node ${__dirname}/../bin/index.js add`, |
| 15 | + { |
| 16 | + cwd: dir |
| 17 | + } |
| 18 | + ); |
| 19 | + await addCommandInterface.keys.enter(); // selects default name |
| 20 | + return addCommandInterface.getOutput(); |
| 21 | +} |
| 22 | + |
| 23 | +describe('projectman open', () => { |
| 24 | + afterEach(() => { |
| 25 | + cleanUp(); |
| 26 | + }); |
| 27 | + |
| 28 | + test('should log no error found and pm add instructions', async () => { |
| 29 | + const commandInterface = createCommandInterface( |
| 30 | + 'node ../bin/index.js open', |
| 31 | + { |
| 32 | + cwd: __dirname |
| 33 | + } |
| 34 | + ); |
| 35 | + |
| 36 | + const terminal = await commandInterface.getOutput(); |
| 37 | + expect(terminal.tokenizedOutput).toBe( |
| 38 | + NO_PROJECT_FOUND_ERROR + '\n' + CD_TO_ADD_PROJECT_MSG |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + // eslint-disable-next-line max-len |
| 43 | + test('should show dropdown with tests and utils and with arrow on utils', async () => { |
| 44 | + // add tests and utils |
| 45 | + await addProject(__dirname); |
| 46 | + await addProject(path.join(__dirname, 'utils')); |
| 47 | + |
| 48 | + const openCommandInterface = createCommandInterface( |
| 49 | + 'node ../bin/index.js open', |
| 50 | + { |
| 51 | + cwd: __dirname |
| 52 | + } |
| 53 | + ); |
| 54 | + await openCommandInterface.keys.arrowDown(); // move down to select utils |
| 55 | + const { rawOutput } = await openCommandInterface.getOutput(); |
| 56 | + /** |
| 57 | + * Why slice from last clear line? |
| 58 | + * |
| 59 | + * Even though we see one output in terminal, sometimes libraries can create multiple outputs |
| 60 | + * slicing from the last clear line just makes sure we only get final output. |
| 61 | + */ |
| 62 | + const outputAfterLastLineClear = rawOutput.slice( |
| 63 | + rawOutput.lastIndexOf(`${STRING_ESC}2K`) |
| 64 | + ); |
| 65 | + const parsedOutputAfterLastLineClear = parseOutput( |
| 66 | + outputAfterLastLineClear |
| 67 | + ); |
| 68 | + |
| 69 | + expect(parsedOutputAfterLastLineClear.stringOutput).toBe( |
| 70 | + `? Select project to open:› \ntests \nutils` |
| 71 | + ); |
| 72 | + }); |
| 73 | +}); |
0 commit comments