|
| 1 | +import BaseClass from './base-class'; |
| 2 | +import { cliux as ux } from '@contentstack/cli-utilities'; |
| 3 | + |
| 4 | +jest.mock('@contentstack/cli-utilities', () => ({ |
| 5 | + cliux: { |
| 6 | + inquire: jest.fn(), |
| 7 | + table: jest.fn(), |
| 8 | + }, |
| 9 | +})); |
| 10 | + |
| 11 | +describe('BaseClass - handleEnvImportFlow', () => { |
| 12 | + let baseClass: BaseClass; |
| 13 | + let logMock: jest.Mock; |
| 14 | + let exitMock: jest.Mock; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + logMock = jest.fn(); |
| 18 | + exitMock = jest.fn(); |
| 19 | + baseClass = new BaseClass({ |
| 20 | + log: logMock, |
| 21 | + exit: exitMock, |
| 22 | + config: { |
| 23 | + variablePreparationTypeOptions: [ |
| 24 | + 'Import variables from a stack', |
| 25 | + 'Manually add custom variables to the list', |
| 26 | + 'Import variables from the .env.local file', |
| 27 | + 'Skip adding environment variables', |
| 28 | + ], |
| 29 | + }, |
| 30 | + } as any); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(() => { |
| 34 | + jest.clearAllMocks(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should exit if no options are selected', async () => { |
| 38 | + (ux.inquire as jest.Mock).mockResolvedValueOnce([]); |
| 39 | + |
| 40 | + await baseClass.handleEnvImportFlow(); |
| 41 | + |
| 42 | + expect(logMock).toHaveBeenCalledWith( |
| 43 | + 'Please select at least one option by pressing <space>, then press <enter> to proceed.', |
| 44 | + 'error', |
| 45 | + ); |
| 46 | + expect(exitMock).toHaveBeenCalledWith(1); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should exit if "Skip adding environment variables" is selected with other options', async () => { |
| 50 | + const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce(); |
| 51 | + (ux.inquire as jest.Mock).mockResolvedValueOnce([ |
| 52 | + 'Skip adding environment variables', |
| 53 | + 'Import variables from a stack', |
| 54 | + ]); |
| 55 | + |
| 56 | + await baseClass.handleEnvImportFlow(); |
| 57 | + |
| 58 | + expect(logMock).toHaveBeenCalledWith( |
| 59 | + "The 'Skip' 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.", |
| 60 | + 'error', |
| 61 | + ); |
| 62 | + |
| 63 | + expect(exitMock).toHaveBeenCalledWith(1); |
| 64 | + expect(importEnvFromStackMock).toHaveBeenCalled(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should call importEnvFromStack if "Import variables from a stack" is selected', async () => { |
| 68 | + const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce(); |
| 69 | + |
| 70 | + (ux.inquire as jest.Mock).mockResolvedValueOnce(['Import variables from a stack']); |
| 71 | + |
| 72 | + await baseClass.handleEnvImportFlow(); |
| 73 | + |
| 74 | + expect(importEnvFromStackMock).toHaveBeenCalled(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should call promptForEnvValues if "Manually add custom variables to the list" is selected', async () => { |
| 78 | + const promptForEnvValuesMock = jest.spyOn(baseClass, 'promptForEnvValues').mockResolvedValueOnce(); |
| 79 | + |
| 80 | + (ux.inquire as jest.Mock).mockResolvedValueOnce(['Manually add custom variables to the list']); |
| 81 | + |
| 82 | + await baseClass.handleEnvImportFlow(); |
| 83 | + |
| 84 | + expect(promptForEnvValuesMock).toHaveBeenCalled(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should call importVariableFromLocalConfig if "Import variables from the .env.local file" is selected', async () => { |
| 88 | + const importVariableFromLocalConfigMock = jest |
| 89 | + .spyOn(baseClass, 'importVariableFromLocalConfig') |
| 90 | + .mockResolvedValueOnce(); |
| 91 | + |
| 92 | + (ux.inquire as jest.Mock).mockResolvedValueOnce(['Import variables from the .env.local file']); |
| 93 | + |
| 94 | + await baseClass.handleEnvImportFlow(); |
| 95 | + |
| 96 | + expect(importVariableFromLocalConfigMock).toHaveBeenCalled(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should set envVariables to an empty array if "Skip adding environment variables" is selected', async () => { |
| 100 | + (ux.inquire as jest.Mock).mockResolvedValueOnce(['Skip adding environment variables']); |
| 101 | + |
| 102 | + await baseClass.handleEnvImportFlow(); |
| 103 | + |
| 104 | + expect(baseClass.envVariables).toEqual([]); |
| 105 | + expect(logMock).toHaveBeenCalledWith('Skipped adding environment variables.', 'info'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should print all variables if envVariables are populated', async () => { |
| 109 | + const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce(); |
| 110 | + const printAllVariablesMock = jest.spyOn(baseClass, 'printAllVariables').mockImplementation(); |
| 111 | + (ux.inquire as jest.Mock).mockResolvedValueOnce(['Import variables from a stack']); |
| 112 | + |
| 113 | + baseClass.envVariables = [{ key: 'APP_ENV', value: 'test' }]; |
| 114 | + |
| 115 | + await baseClass.handleEnvImportFlow(); |
| 116 | + |
| 117 | + expect(importEnvFromStackMock).toHaveBeenCalled(); |
| 118 | + expect(printAllVariablesMock).toHaveBeenCalled(); |
| 119 | + expect(baseClass.envVariables).toEqual([{ key: 'APP_ENV', value: 'test' }]); |
| 120 | + }); |
| 121 | +}); |
0 commit comments