|
| 1 | +import { Toolkit } from '@aws-cdk/toolkit-lib'; |
| 2 | +import { asIoHelper } from '../../lib/api-private'; |
| 3 | +import { displayFlagsMessage } from '../../lib/cli/cdk-toolkit'; |
| 4 | +import { TestIoHost } from '../_helpers/io-host'; |
| 5 | + |
| 6 | +describe('displayFlagsMessage', () => { |
| 7 | + let ioHost: TestIoHost; |
| 8 | + let ioHelper: any; |
| 9 | + let mockToolkit: jest.Mocked<Toolkit>; |
| 10 | + let mockCloudExecutable: any; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + ioHost = new TestIoHost(); |
| 14 | + ioHelper = asIoHelper(ioHost, 'synth'); |
| 15 | + mockCloudExecutable = {}; |
| 16 | + |
| 17 | + mockToolkit = { |
| 18 | + flags: jest.fn(), |
| 19 | + } as any; |
| 20 | + |
| 21 | + jest.spyOn(Toolkit.prototype, 'flags').mockImplementation(mockToolkit.flags); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(() => { |
| 25 | + jest.restoreAllMocks(); |
| 26 | + }); |
| 27 | + |
| 28 | + test('displays message with correct count of unconfigured flags, filtering out obsolete flags', async () => { |
| 29 | + const mockFlagsData = [ |
| 30 | + { |
| 31 | + name: '@aws-cdk/core:testFlag', |
| 32 | + userValue: undefined, |
| 33 | + recommendedValue: 'true', |
| 34 | + explanation: 'Test flag', |
| 35 | + module: 'aws-cdk-lib', |
| 36 | + }, |
| 37 | + { |
| 38 | + name: '@aws-cdk/s3:anotherFlag', |
| 39 | + userValue: 'false', |
| 40 | + recommendedValue: 'false', |
| 41 | + explanation: 'Another test flag', |
| 42 | + module: 'aws-cdk-lib', |
| 43 | + }, |
| 44 | + { |
| 45 | + name: '@aws-cdk/core:enableStackNameDuplicates', |
| 46 | + userValue: undefined, |
| 47 | + recommendedValue: 'true', |
| 48 | + explanation: 'Obsolete flag', |
| 49 | + module: 'aws-cdk-lib', |
| 50 | + }, |
| 51 | + ]; |
| 52 | + |
| 53 | + mockToolkit.flags.mockResolvedValue(mockFlagsData); |
| 54 | + |
| 55 | + await displayFlagsMessage(mockToolkit as any, mockCloudExecutable, ioHelper); |
| 56 | + |
| 57 | + expect(mockToolkit.flags).toHaveBeenCalledWith(mockCloudExecutable); |
| 58 | + expect(ioHost.notifySpy).toHaveBeenCalledWith( |
| 59 | + expect.objectContaining({ |
| 60 | + message: 'You currently have 1 unconfigured feature flags that may require attention to keep your application up-to-date. Run \'cdk flags\' to learn more.', |
| 61 | + level: 'info', |
| 62 | + }), |
| 63 | + ); |
| 64 | + }); |
| 65 | + test('does not display a message when user has no unconfigured flags', async () => { |
| 66 | + const mockFlagsData = [ |
| 67 | + { |
| 68 | + name: '@aws-cdk/s3:anotherFlag', |
| 69 | + userValue: 'false', |
| 70 | + recommendedValue: 'false', |
| 71 | + explanation: 'Another test flag', |
| 72 | + module: 'aws-cdk-lib', |
| 73 | + }, |
| 74 | + ]; |
| 75 | + mockToolkit.flags.mockResolvedValue(mockFlagsData); |
| 76 | + |
| 77 | + await displayFlagsMessage(mockToolkit as any, mockCloudExecutable, ioHelper); |
| 78 | + |
| 79 | + expect(mockToolkit.flags).toHaveBeenCalledWith(mockCloudExecutable); |
| 80 | + expect(ioHost.notifySpy).not.toHaveBeenCalled(); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
0 commit comments