|
| 1 | +import BaseClass from './base-class'; |
| 2 | +import { cliux as ux } from '@contentstack/cli-utilities'; |
| 3 | +import config from '../config'; |
| 4 | + |
| 5 | +jest.mock('@contentstack/cli-utilities', () => ({ |
| 6 | + cliux: { |
| 7 | + inquire: jest.fn(), |
| 8 | + table: jest.fn(), |
| 9 | + }, |
| 10 | +})); |
| 11 | + |
| 12 | +describe('BaseClass', () => { |
| 13 | + let baseClass: BaseClass; |
| 14 | + let logMock: jest.Mock; |
| 15 | + let exitMock: jest.Mock; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + logMock = jest.fn(); |
| 19 | + exitMock = jest.fn(); |
| 20 | + }); |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + jest.clearAllMocks(); |
| 24 | + }); |
| 25 | + |
| 26 | + describe('handleEnvImportFlow', () => { |
| 27 | + beforeEach(() => { |
| 28 | + baseClass = new BaseClass({ |
| 29 | + log: logMock, |
| 30 | + exit: exitMock, |
| 31 | + config: config.variablePreparationTypeOptions, |
| 32 | + } as any); |
| 33 | + }); |
| 34 | + it('should exit if no options are selected', async () => { |
| 35 | + (ux.inquire as jest.Mock).mockResolvedValueOnce([]); |
| 36 | + |
| 37 | + await baseClass.handleEnvImportFlow(); |
| 38 | + |
| 39 | + expect(logMock).toHaveBeenCalledWith( |
| 40 | + 'Please select at least one option by pressing <space>, then press <enter> to proceed.', |
| 41 | + 'error', |
| 42 | + ); |
| 43 | + expect(exitMock).toHaveBeenCalledWith(1); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should exit if "Skip adding environment variables" is selected with other options', async () => { |
| 47 | + const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce(); |
| 48 | + (ux.inquire as jest.Mock).mockResolvedValueOnce([ |
| 49 | + 'Skip adding environment variables', |
| 50 | + 'Import variables from a stack', |
| 51 | + ]); |
| 52 | + |
| 53 | + await baseClass.handleEnvImportFlow(); |
| 54 | + |
| 55 | + expect(logMock).toHaveBeenCalledWith( |
| 56 | + "The 'Skip adding environment variables' option cannot be combined with other environment variable options. Please choose either 'Skip adding environment variables' or one or more of the other available options.", |
| 57 | + 'error', |
| 58 | + ); |
| 59 | + |
| 60 | + expect(exitMock).toHaveBeenCalledWith(1); |
| 61 | + expect(importEnvFromStackMock).toHaveBeenCalled(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should call importEnvFromStack if "Import variables from a stack" is selected', async () => { |
| 65 | + const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce(); |
| 66 | + |
| 67 | + (ux.inquire as jest.Mock).mockResolvedValueOnce(['Import variables from a stack']); |
| 68 | + |
| 69 | + await baseClass.handleEnvImportFlow(); |
| 70 | + |
| 71 | + expect(importEnvFromStackMock).toHaveBeenCalled(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should call promptForEnvValues if "Manually add custom variables to the list" is selected', async () => { |
| 75 | + const promptForEnvValuesMock = jest.spyOn(baseClass, 'promptForEnvValues').mockResolvedValueOnce(); |
| 76 | + |
| 77 | + (ux.inquire as jest.Mock).mockResolvedValueOnce(['Manually add custom variables to the list']); |
| 78 | + |
| 79 | + await baseClass.handleEnvImportFlow(); |
| 80 | + |
| 81 | + expect(promptForEnvValuesMock).toHaveBeenCalled(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should call importVariableFromLocalConfig if "Import variables from the .env.local file" is selected', async () => { |
| 85 | + const importVariableFromLocalConfigMock = jest |
| 86 | + .spyOn(baseClass, 'importVariableFromLocalConfig') |
| 87 | + .mockResolvedValueOnce(); |
| 88 | + |
| 89 | + (ux.inquire as jest.Mock).mockResolvedValueOnce(['Import variables from the .env.local file']); |
| 90 | + |
| 91 | + await baseClass.handleEnvImportFlow(); |
| 92 | + |
| 93 | + expect(importVariableFromLocalConfigMock).toHaveBeenCalled(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should set envVariables to an empty array if "Skip adding environment variables" is selected', async () => { |
| 97 | + (ux.inquire as jest.Mock).mockResolvedValueOnce(['Skip adding environment variables']); |
| 98 | + |
| 99 | + await baseClass.handleEnvImportFlow(); |
| 100 | + |
| 101 | + expect(baseClass.envVariables).toEqual([]); |
| 102 | + expect(logMock).toHaveBeenCalledWith('Skipped adding environment variables.', 'info'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should call printAllVariables if envVariables has values', async () => { |
| 106 | + const expectedEnv = [{ key: 'API_KEY', value: '12345' }]; |
| 107 | + const importVariableFromLocalConfigMock = jest |
| 108 | + .spyOn(baseClass, 'importVariableFromLocalConfig') |
| 109 | + .mockImplementationOnce(() => { |
| 110 | + baseClass.envVariables = expectedEnv; |
| 111 | + return Promise.resolve(); |
| 112 | + }); |
| 113 | + |
| 114 | + const printAllVariablesMock = jest |
| 115 | + .spyOn(baseClass, 'printAllVariables') |
| 116 | + .mockImplementation(); |
| 117 | + |
| 118 | + (ux.inquire as jest.Mock).mockResolvedValueOnce([ |
| 119 | + 'Import variables from the .env.local file', |
| 120 | + ]); |
| 121 | + |
| 122 | + await baseClass.handleEnvImportFlow(); |
| 123 | + |
| 124 | + expect(importVariableFromLocalConfigMock).toHaveBeenCalled(); |
| 125 | + expect(printAllVariablesMock).toHaveBeenCalled(); |
| 126 | + expect(baseClass.envVariables).toEqual(expectedEnv); |
| 127 | + }); |
| 128 | + |
| 129 | + }); |
| 130 | + |
| 131 | + describe('importVariableFromLocalConfig', () => { |
| 132 | + jest.mock('dotenv', () => ({ |
| 133 | + config: jest.fn().mockReturnValueOnce({ parsed: null }) |
| 134 | + })); |
| 135 | + |
| 136 | + beforeEach(() => { |
| 137 | + baseClass = new BaseClass({ |
| 138 | + config: { |
| 139 | + projectBasePath: './baseClass', |
| 140 | + }, |
| 141 | + log: logMock, |
| 142 | + exit: exitMock, |
| 143 | + } as any); |
| 144 | + }); |
| 145 | + |
| 146 | + afterEach(() => { |
| 147 | + jest.clearAllMocks(); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should log an error and exit if no .env.local file is found', async () => { |
| 151 | + jest.mock('dotenv', () => ({ |
| 152 | + config: jest.fn().mockReturnValueOnce({ parsed: {} }) |
| 153 | + })); |
| 154 | + |
| 155 | + await baseClass.importVariableFromLocalConfig(); |
| 156 | + |
| 157 | + expect(logMock).toHaveBeenCalledWith('No .env.local file found.', 'error'); |
| 158 | + expect(exitMock).toHaveBeenCalledWith(1); |
| 159 | + }); |
| 160 | + }); |
| 161 | +}); |
0 commit comments