|
| 1 | +jest.mock('live-plugin-manager'); |
| 2 | + |
| 3 | +import chalk from 'chalk'; |
| 4 | +import { PluginManager } from 'live-plugin-manager'; |
| 5 | + |
| 6 | +import list from './list'; |
| 7 | + |
| 8 | +describe('list', () => { |
| 9 | + beforeEach(() => { |
| 10 | + jest.spyOn(console, 'log').mockImplementation(); |
| 11 | + jest.spyOn(console, 'warn').mockImplementation(); |
| 12 | + |
| 13 | + (PluginManager as jest.Mock).mockImplementation(() => ({ |
| 14 | + install: jest.fn().mockResolvedValue(undefined), |
| 15 | + require: jest.fn().mockImplementation(() => ({ |
| 16 | + transforms: { |
| 17 | + '18.0.0': 'path/to/18.js', |
| 18 | + '19.0.0': 'path/to/19.js', |
| 19 | + }, |
| 20 | + presets: { |
| 21 | + 'sort-imports': 'path/to/sort-imports.js', |
| 22 | + }, |
| 23 | + })), |
| 24 | + })); |
| 25 | + }); |
| 26 | + afterEach(() => { |
| 27 | + jest.resetAllMocks(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should list codemods for single valid package', async () => { |
| 31 | + await list(['foobar']); |
| 32 | + |
| 33 | + const result = (console.log as jest.Mock).mock.calls.join('\n'); |
| 34 | + |
| 35 | + expect(result).toEqual(`${chalk.bold('foobar')} |
| 36 | +├─ transforms |
| 37 | +| ├─ 18.0.0 |
| 38 | +| └─ 19.0.0 |
| 39 | +└─ presets |
| 40 | + └─ sort-imports`); |
| 41 | + expect(console.warn).not.toHaveBeenCalled(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should list codemods for single valid scoped package', async () => { |
| 45 | + await list(['@foo/bar']); |
| 46 | + |
| 47 | + const result = (console.log as jest.Mock).mock.calls.join('\n'); |
| 48 | + |
| 49 | + expect(result).toEqual(`${chalk.bold('@foo/bar')} |
| 50 | +├─ transforms |
| 51 | +| ├─ 18.0.0 |
| 52 | +| └─ 19.0.0 |
| 53 | +└─ presets |
| 54 | + └─ sort-imports`); |
| 55 | + expect(console.warn).not.toHaveBeenCalled(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should list codemods for multiple packages', async () => { |
| 59 | + await list(['bar', '@foo/bar']); |
| 60 | + |
| 61 | + const result = (console.log as jest.Mock).mock.calls.join('\n'); |
| 62 | + |
| 63 | + expect(result).toEqual(`${chalk.bold('bar')} |
| 64 | +├─ transforms |
| 65 | +| ├─ 18.0.0 |
| 66 | +| └─ 19.0.0 |
| 67 | +└─ presets |
| 68 | + └─ sort-imports |
| 69 | +${chalk.bold('@foo/bar')} |
| 70 | +├─ transforms |
| 71 | +| ├─ 18.0.0 |
| 72 | +| └─ 19.0.0 |
| 73 | +└─ presets |
| 74 | + └─ sort-imports`); |
| 75 | + expect(console.warn).not.toHaveBeenCalled(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should return error message a package is not found', async () => { |
| 79 | + (PluginManager as jest.Mock).mockImplementation(() => ({ |
| 80 | + install: jest.fn().mockImplementation(() => { |
| 81 | + throw new Error('404 not found'); |
| 82 | + }), |
| 83 | + })); |
| 84 | + |
| 85 | + await list(['bar']); |
| 86 | + |
| 87 | + expect(console.warn).toHaveBeenCalledWith( |
| 88 | + chalk.red(`Unable to find codeshift package: ${chalk.bold('bar')}.`), |
| 89 | + ); |
| 90 | + expect(console.warn).toHaveBeenCalledTimes(1); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should return error message for multiple packages that are not found', async () => { |
| 94 | + (PluginManager as jest.Mock).mockImplementation(() => ({ |
| 95 | + install: jest.fn().mockImplementation(() => { |
| 96 | + throw new Error('404 not found'); |
| 97 | + }), |
| 98 | + })); |
| 99 | + |
| 100 | + await list(['bar', '@foo/bar']); |
| 101 | + |
| 102 | + expect(console.warn).toHaveBeenCalledWith( |
| 103 | + chalk.red(`Unable to find codeshift package: ${chalk.bold('bar')}.`), |
| 104 | + ); |
| 105 | + expect(console.warn).toHaveBeenCalledWith( |
| 106 | + chalk.red(`Unable to find codeshift package: ${chalk.bold('@foo/bar')}.`), |
| 107 | + ); |
| 108 | + expect(console.warn).toHaveBeenCalledTimes(2); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should continue if one or more packages are not found', async () => { |
| 112 | + (PluginManager as jest.Mock).mockImplementation(() => ({ |
| 113 | + install: jest |
| 114 | + .fn() |
| 115 | + .mockImplementationOnce(() => { |
| 116 | + throw new Error('404 not found'); |
| 117 | + }) |
| 118 | + .mockResolvedValueOnce(undefined) |
| 119 | + .mockResolvedValueOnce(undefined) |
| 120 | + .mockImplementationOnce(() => { |
| 121 | + throw new Error('404 not found'); |
| 122 | + }) |
| 123 | + .mockResolvedValueOnce(undefined) |
| 124 | + .mockResolvedValueOnce(undefined), |
| 125 | + require: jest.fn().mockImplementation(() => ({ |
| 126 | + transforms: { |
| 127 | + '18.0.0': 'path/to/18.js', |
| 128 | + '19.0.0': 'path/to/19.js', |
| 129 | + }, |
| 130 | + presets: { |
| 131 | + 'sort-imports': 'path/to/sort-imports.js', |
| 132 | + }, |
| 133 | + })), |
| 134 | + })); |
| 135 | + |
| 136 | + await list(['unknown', 'found1', 'dunno', 'found2']); |
| 137 | + |
| 138 | + const result = (console.log as jest.Mock).mock.calls.join('\n'); |
| 139 | + |
| 140 | + expect(result).toEqual(`${chalk.bold('found1')} |
| 141 | +├─ transforms |
| 142 | +| ├─ 18.0.0 |
| 143 | +| └─ 19.0.0 |
| 144 | +└─ presets |
| 145 | + └─ sort-imports |
| 146 | +${chalk.bold('found2')} |
| 147 | +├─ transforms |
| 148 | +| ├─ 18.0.0 |
| 149 | +| └─ 19.0.0 |
| 150 | +└─ presets |
| 151 | + └─ sort-imports`); |
| 152 | + |
| 153 | + expect(console.warn).toHaveBeenCalledWith( |
| 154 | + chalk.red(`Unable to find codeshift package: ${chalk.bold('unknown')}.`), |
| 155 | + ); |
| 156 | + expect(console.warn).toHaveBeenCalledWith( |
| 157 | + chalk.red(`Unable to find codeshift package: ${chalk.bold('dunno')}.`), |
| 158 | + ); |
| 159 | + expect(console.warn).toHaveBeenCalledTimes(2); |
| 160 | + }); |
| 161 | +}); |
0 commit comments