|
| 1 | +import { run } from 'jscodeshift/src/Runner' |
| 2 | +import prompts from 'prompts' |
| 3 | +import { upgrade } from '../upgrade' |
| 4 | + |
| 5 | +jest.mock('jscodeshift/src/Runner', () => ({ |
| 6 | + run: jest.fn(), |
| 7 | +})) |
| 8 | + |
| 9 | +describe('interactive mode', () => { |
| 10 | + beforeEach(() => { |
| 11 | + jest.clearAllMocks() |
| 12 | + }) |
| 13 | + |
| 14 | + it('runs without source params provided and select is true', async () => { |
| 15 | + const spyOnConsole = jest.spyOn(console, 'log').mockImplementation() |
| 16 | + |
| 17 | + prompts.inject([['magic-redirect', 'req-param']]) |
| 18 | + |
| 19 | + await upgrade('__testfixtures__', { select: true }) |
| 20 | + |
| 21 | + expect(spyOnConsole).toHaveBeenCalled() |
| 22 | + expect(spyOnConsole).toHaveBeenCalledTimes(3) |
| 23 | + expect(run).toHaveBeenCalledTimes(2) |
| 24 | + }) |
| 25 | + |
| 26 | + it('runs with source params provided and select is true', async () => { |
| 27 | + const spyOnConsole = jest.spyOn(console, 'log').mockImplementation() |
| 28 | + |
| 29 | + prompts.inject([['magic-redirect', 'req-param']]) |
| 30 | + |
| 31 | + await upgrade('__testfixtures__', { select: true }) |
| 32 | + |
| 33 | + expect(spyOnConsole).toHaveBeenCalled() |
| 34 | + expect(spyOnConsole).toHaveBeenCalledTimes(3) |
| 35 | + expect(run).toHaveBeenCalledTimes(2) |
| 36 | + }) |
| 37 | + |
| 38 | + it('runs without source params provided and select is undefined', async () => { |
| 39 | + const spyOnConsole = jest.spyOn(console, 'log').mockImplementation() |
| 40 | + |
| 41 | + prompts.inject(['__testfixtures__']) |
| 42 | + |
| 43 | + await upgrade(undefined) |
| 44 | + |
| 45 | + expect(spyOnConsole).toHaveBeenCalled() |
| 46 | + expect(spyOnConsole).toHaveBeenCalledTimes(5) |
| 47 | + expect(run).toHaveBeenCalledTimes(4) |
| 48 | + }) |
| 49 | +}) |
| 50 | + |
| 51 | +describe('Non-Interactive Mode', () => { |
| 52 | + beforeEach(() => { |
| 53 | + jest.clearAllMocks() |
| 54 | + }) |
| 55 | + |
| 56 | + it('Transforms code with source params provided', async () => { |
| 57 | + const spyOnConsole = jest.spyOn(console, 'log').mockImplementation() |
| 58 | + |
| 59 | + await upgrade('__testfixtures__') |
| 60 | + |
| 61 | + expect(spyOnConsole).toHaveBeenCalledTimes(5) |
| 62 | + expect(spyOnConsole).toHaveBeenCalledWith('> Applying codemod: magic-redirect') |
| 63 | + expect(spyOnConsole).toHaveBeenCalledWith('> Applying codemod: pluralized-methods') |
| 64 | + expect(spyOnConsole).toHaveBeenCalledWith('> Applying codemod: req-param') |
| 65 | + expect(spyOnConsole).toHaveBeenCalledWith('> Applying codemod: v4-deprecated-signatures') |
| 66 | + expect(spyOnConsole).toHaveBeenLastCalledWith('\n> All codemods have been applied successfully. \n') |
| 67 | + expect(run).toHaveBeenCalledTimes(4) |
| 68 | + }) |
| 69 | +}) |
0 commit comments