|
1 | | -// TODO |
| 1 | +jest.mock('globby'); |
| 2 | + |
| 3 | +import globby from 'globby'; |
| 4 | +import path from 'path'; |
| 5 | +import { PluginManager } from 'live-plugin-manager'; |
| 6 | + |
| 7 | +import { fetchConfig, fetchPackage, fetchRemotePackage } from '.'; |
| 8 | + |
| 9 | +const mockBasePath = path.join(__dirname, 'path', 'to'); |
| 10 | + |
| 11 | +const mockConfig = { |
| 12 | + transforms: { |
| 13 | + '10.0.0': 'path/to/transform.js', |
| 14 | + }, |
| 15 | +}; |
| 16 | + |
| 17 | +describe('fetcher', () => { |
| 18 | + let mockMatchedPaths: string[] = []; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + mockMatchedPaths = [ |
| 22 | + path.join(mockBasePath, 'codeshift.config.js'), |
| 23 | + path.join(mockBasePath, 'src', 'codeshift.config.ts'), |
| 24 | + path.join(mockBasePath, 'codemods', 'codeshift.config.tsx'), |
| 25 | + ]; |
| 26 | + |
| 27 | + ((globby as unknown) as jest.Mock).mockImplementation(() => |
| 28 | + Promise.resolve(mockMatchedPaths), |
| 29 | + ); |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + jest.resetAllMocks(); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('fetchConfig', () => { |
| 37 | + it('fetches config with default export', async () => { |
| 38 | + jest.mock( |
| 39 | + `${__dirname}/path/to/codeshift.config.js`, |
| 40 | + () => ({ |
| 41 | + __esModule: true, |
| 42 | + default: mockConfig, |
| 43 | + }), |
| 44 | + { virtual: true }, |
| 45 | + ); |
| 46 | + |
| 47 | + const config = await fetchConfig(mockBasePath); |
| 48 | + |
| 49 | + expect(config).toEqual(mockConfig); |
| 50 | + }); |
| 51 | + |
| 52 | + it('fetches config with named export', async () => { |
| 53 | + jest.mock(`${__dirname}/path/to/codeshift.config.js`, () => mockConfig, { |
| 54 | + virtual: true, |
| 55 | + }); |
| 56 | + |
| 57 | + const config = await fetchConfig(mockBasePath); |
| 58 | + |
| 59 | + expect(config).toEqual(mockConfig); |
| 60 | + }); |
| 61 | + |
| 62 | + it('fetches first matched config when multiple are found', async () => { |
| 63 | + jest.mock( |
| 64 | + `${__dirname}/path/to/src/codeshift.config.ts`, |
| 65 | + () => ({ |
| 66 | + __esModule: true, |
| 67 | + default: mockConfig, |
| 68 | + }), |
| 69 | + { virtual: true }, |
| 70 | + ); |
| 71 | + |
| 72 | + mockMatchedPaths = [ |
| 73 | + path.join(mockBasePath, 'src', 'codeshift.config.ts'), |
| 74 | + path.join(mockBasePath, 'codemods', 'codeshift.config.tsx'), |
| 75 | + ]; |
| 76 | + |
| 77 | + const config = await fetchConfig(mockBasePath); |
| 78 | + |
| 79 | + expect(config).toEqual(mockConfig); |
| 80 | + }); |
| 81 | + |
| 82 | + it('returns undefined if no config was found', async () => { |
| 83 | + mockMatchedPaths = []; |
| 84 | + |
| 85 | + const config = await fetchConfig(mockBasePath); |
| 86 | + |
| 87 | + expect(config).toBe(undefined); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('fetchPackage', () => { |
| 92 | + it('correctly fetches package and returns a config', async () => { |
| 93 | + const mockPackageManager = { |
| 94 | + install: jest.fn(), |
| 95 | + require: jest.fn().mockReturnValue(mockConfig), |
| 96 | + }; |
| 97 | + |
| 98 | + const config = await fetchPackage( |
| 99 | + 'fake-package', |
| 100 | + (mockPackageManager as unknown) as PluginManager, |
| 101 | + ); |
| 102 | + |
| 103 | + expect(config).toEqual(mockConfig); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should throw if fetching fails', async () => { |
| 107 | + const mockPackageManager = { |
| 108 | + install: jest.fn().mockRejectedValue('Import error'), |
| 109 | + require: jest.fn().mockReturnValue(mockConfig), |
| 110 | + }; |
| 111 | + |
| 112 | + expect.assertions(1); |
| 113 | + |
| 114 | + await expect( |
| 115 | + fetchPackage( |
| 116 | + 'fake-package', |
| 117 | + (mockPackageManager as unknown) as PluginManager, |
| 118 | + ), |
| 119 | + ).rejects.toEqual('Import error'); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('fetchRemotePackage', () => { |
| 124 | + it('correctly fetches package and returns a config', async () => { |
| 125 | + const mockPackageManager = { |
| 126 | + install: jest.fn(), |
| 127 | + getInfo: jest.fn().mockReturnValue({ |
| 128 | + location: mockBasePath, |
| 129 | + }), |
| 130 | + }; |
| 131 | + |
| 132 | + const config = await fetchRemotePackage( |
| 133 | + 'fake-package', |
| 134 | + (mockPackageManager as unknown) as PluginManager, |
| 135 | + ); |
| 136 | + |
| 137 | + expect(config).toEqual(mockConfig); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should throw if fetching fails', async () => { |
| 141 | + const mockPackageManager = { |
| 142 | + install: jest.fn().mockRejectedValue('Import error'), |
| 143 | + }; |
| 144 | + |
| 145 | + expect.assertions(1); |
| 146 | + |
| 147 | + await expect( |
| 148 | + fetchRemotePackage( |
| 149 | + 'fake-package', |
| 150 | + (mockPackageManager as unknown) as PluginManager, |
| 151 | + ), |
| 152 | + ).rejects.toEqual('Import error'); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should throw if package source cannot be retrieved', async () => { |
| 156 | + const mockPackageManager = { |
| 157 | + install: jest.fn(), |
| 158 | + getInfo: () => undefined, |
| 159 | + }; |
| 160 | + |
| 161 | + expect.assertions(1); |
| 162 | + |
| 163 | + await expect( |
| 164 | + fetchRemotePackage( |
| 165 | + 'fake-package', |
| 166 | + (mockPackageManager as unknown) as PluginManager, |
| 167 | + ), |
| 168 | + ).rejects.toEqual( |
| 169 | + new Error(`Unable to locate package files for package: 'fake-package'`), |
| 170 | + ); |
| 171 | + }); |
| 172 | + }); |
| 173 | +}); |
0 commit comments