|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import * as vscode from 'vscode'; |
| 3 | +import { designerVersionSetting, defaultDesignerVersion } from '../../../../../constants'; |
| 4 | +import { ext } from '../../../../../extensionVariables'; |
| 5 | + |
| 6 | +// Mock dependencies before importing the class |
| 7 | +vi.mock('../../../../../localize', () => ({ |
| 8 | + localize: (_key: string, defaultMsg: string) => defaultMsg, |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock('../../../../utils/codeless/common', () => ({ |
| 12 | + tryGetWebviewPanel: vi.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock('../../../../utils/codeless/getWebViewHTML', () => ({ |
| 16 | + getWebViewHTML: vi.fn().mockResolvedValue('<html></html>'), |
| 17 | +})); |
| 18 | + |
| 19 | +vi.mock('@microsoft/logic-apps-shared', () => ({ |
| 20 | + getRecordEntry: vi.fn((obj: any, key: string) => obj?.[key]), |
| 21 | + isEmptyString: vi.fn((s: any) => !s || (typeof s === 'string' && s.trim().length === 0)), |
| 22 | + resolveConnectionsReferences: vi.fn(() => ({})), |
| 23 | +})); |
| 24 | + |
| 25 | +// Import the actual class after mocks |
| 26 | +import { OpenDesignerBase } from '../openDesignerBase'; |
| 27 | + |
| 28 | +// Concrete subclass to test the abstract class |
| 29 | +class TestDesigner extends OpenDesignerBase { |
| 30 | + constructor(context?: any) { |
| 31 | + super( |
| 32 | + context ?? { telemetry: { properties: {}, measurements: {} } }, |
| 33 | + 'test-workflow', |
| 34 | + 'test-panel', |
| 35 | + '2018-11-01', |
| 36 | + 'test-key', |
| 37 | + false, |
| 38 | + true, |
| 39 | + false, |
| 40 | + '' |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + async createPanel(): Promise<void> {} |
| 45 | + |
| 46 | + // Expose protected methods for testing |
| 47 | + public testGetDesignerVersion() { |
| 48 | + return this.getDesignerVersion(); |
| 49 | + } |
| 50 | + public async testShowDesignerVersionNotification() { |
| 51 | + return this.showDesignerVersionNotification(); |
| 52 | + } |
| 53 | + public testNormalizeLocation(location: string) { |
| 54 | + return this.normalizeLocation(location); |
| 55 | + } |
| 56 | + public testGetPanelOptions() { |
| 57 | + return this.getPanelOptions(); |
| 58 | + } |
| 59 | + public testGetApiHubServiceDetails(azureDetails: any, localSettings: any) { |
| 60 | + return this.getApiHubServiceDetails(azureDetails, localSettings); |
| 61 | + } |
| 62 | + public testGetInterpolateConnectionData(data: string) { |
| 63 | + return this.getInterpolateConnectionData(data); |
| 64 | + } |
| 65 | + public setTestPanel(panel: any) { |
| 66 | + this.panel = panel; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +describe('OpenDesignerBase', () => { |
| 71 | + const mockGetConfiguration = vi.mocked(vscode.workspace.getConfiguration); |
| 72 | + const mockShowInformationMessage = vi.mocked(vscode.window.showInformationMessage); |
| 73 | + const mockConfig = { |
| 74 | + get: vi.fn(), |
| 75 | + update: vi.fn().mockResolvedValue(undefined), |
| 76 | + }; |
| 77 | + let designer: TestDesigner; |
| 78 | + |
| 79 | + beforeEach(() => { |
| 80 | + vi.clearAllMocks(); |
| 81 | + mockGetConfiguration.mockReturnValue(mockConfig as any); |
| 82 | + designer = new TestDesigner(); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('constructor', () => { |
| 86 | + it('should initialize with correct properties', () => { |
| 87 | + expect(designer).toBeDefined(); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('getDesignerVersion', () => { |
| 92 | + it('should return version from config when set to 2', () => { |
| 93 | + mockConfig.get.mockReturnValue(2); |
| 94 | + expect(designer.testGetDesignerVersion()).toBe(2); |
| 95 | + expect(mockGetConfiguration).toHaveBeenCalledWith(ext.prefix); |
| 96 | + expect(mockConfig.get).toHaveBeenCalledWith(designerVersionSetting); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should return version from config when set to 1', () => { |
| 100 | + mockConfig.get.mockReturnValue(1); |
| 101 | + expect(designer.testGetDesignerVersion()).toBe(1); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should return default version when config is undefined', () => { |
| 105 | + mockConfig.get.mockReturnValue(undefined); |
| 106 | + expect(designer.testGetDesignerVersion()).toBe(defaultDesignerVersion); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should return default version when config is null', () => { |
| 110 | + mockConfig.get.mockReturnValue(null); |
| 111 | + expect(designer.testGetDesignerVersion()).toBe(defaultDesignerVersion); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + describe('showDesignerVersionNotification', () => { |
| 116 | + it('should show preview available message when version is 1', async () => { |
| 117 | + mockConfig.get.mockReturnValue(1); |
| 118 | + mockShowInformationMessage.mockResolvedValue(undefined); |
| 119 | + designer.setTestPanel({ dispose: vi.fn() }); |
| 120 | + |
| 121 | + await designer.testShowDesignerVersionNotification(); |
| 122 | + |
| 123 | + expect(mockShowInformationMessage).toHaveBeenCalledWith('A new Logic Apps experience is available for preview!', 'Enable preview'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should show previewing message when version is 2', async () => { |
| 127 | + mockConfig.get.mockReturnValue(2); |
| 128 | + mockShowInformationMessage.mockResolvedValue(undefined); |
| 129 | + designer.setTestPanel({ dispose: vi.fn() }); |
| 130 | + |
| 131 | + await designer.testShowDesignerVersionNotification(); |
| 132 | + |
| 133 | + expect(mockShowInformationMessage).toHaveBeenCalledWith( |
| 134 | + 'You are previewing the new Logic Apps experience.', |
| 135 | + 'Go back to previous version' |
| 136 | + ); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should update setting to version 2 when Enable preview is clicked', async () => { |
| 140 | + mockConfig.get.mockReturnValue(1); |
| 141 | + mockShowInformationMessage.mockResolvedValueOnce('Enable preview' as any).mockResolvedValueOnce(undefined); |
| 142 | + designer.setTestPanel({ dispose: vi.fn() }); |
| 143 | + |
| 144 | + await designer.testShowDesignerVersionNotification(); |
| 145 | + |
| 146 | + expect(mockConfig.update).toHaveBeenCalledWith(designerVersionSetting, 2, expect.anything()); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should update setting to version 1 when Go back is clicked', async () => { |
| 150 | + mockConfig.get.mockReturnValue(2); |
| 151 | + mockShowInformationMessage.mockResolvedValueOnce('Go back to previous version' as any).mockResolvedValueOnce(undefined); |
| 152 | + designer.setTestPanel({ dispose: vi.fn() }); |
| 153 | + |
| 154 | + await designer.testShowDesignerVersionNotification(); |
| 155 | + |
| 156 | + expect(mockConfig.update).toHaveBeenCalledWith(designerVersionSetting, 1, expect.anything()); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should not update setting when notification is dismissed', async () => { |
| 160 | + mockConfig.get.mockReturnValue(1); |
| 161 | + mockShowInformationMessage.mockResolvedValue(undefined); |
| 162 | + designer.setTestPanel({ dispose: vi.fn() }); |
| 163 | + |
| 164 | + await designer.testShowDesignerVersionNotification(); |
| 165 | + |
| 166 | + expect(mockConfig.update).not.toHaveBeenCalled(); |
| 167 | + }); |
| 168 | + |
| 169 | + it('should dispose panel when Close is clicked after enabling preview', async () => { |
| 170 | + mockConfig.get.mockReturnValue(1); |
| 171 | + const mockDispose = vi.fn(); |
| 172 | + mockShowInformationMessage.mockResolvedValueOnce('Enable preview' as any).mockResolvedValueOnce('Close' as any); |
| 173 | + designer.setTestPanel({ dispose: mockDispose }); |
| 174 | + |
| 175 | + await designer.testShowDesignerVersionNotification(); |
| 176 | + |
| 177 | + expect(mockDispose).toHaveBeenCalled(); |
| 178 | + }); |
| 179 | + }); |
| 180 | + |
| 181 | + describe('normalizeLocation', () => { |
| 182 | + it('should lowercase and remove spaces', () => { |
| 183 | + expect(designer.testNormalizeLocation('West US')).toBe('westus'); |
| 184 | + }); |
| 185 | + |
| 186 | + it('should handle already normalized location', () => { |
| 187 | + expect(designer.testNormalizeLocation('westus')).toBe('westus'); |
| 188 | + }); |
| 189 | + |
| 190 | + it('should return empty string for empty input', () => { |
| 191 | + expect(designer.testNormalizeLocation('')).toBe(''); |
| 192 | + }); |
| 193 | + }); |
| 194 | + |
| 195 | + describe('getPanelOptions', () => { |
| 196 | + it('should return options with scripts enabled and context retained', () => { |
| 197 | + const options = designer.testGetPanelOptions(); |
| 198 | + expect(options.enableScripts).toBe(true); |
| 199 | + expect(options.retainContextWhenHidden).toBe(true); |
| 200 | + }); |
| 201 | + }); |
| 202 | + |
| 203 | + describe('getApiHubServiceDetails', () => { |
| 204 | + it('should return service details when API hub is enabled', () => { |
| 205 | + const azureDetails = { |
| 206 | + enabled: true, |
| 207 | + subscriptionId: 'sub-123', |
| 208 | + location: 'westus', |
| 209 | + resourceGroupName: 'rg-test', |
| 210 | + tenantId: 'tenant-123', |
| 211 | + accessToken: 'token-123', |
| 212 | + }; |
| 213 | + const result = designer.testGetApiHubServiceDetails(azureDetails, {}); |
| 214 | + |
| 215 | + expect(result).toBeDefined(); |
| 216 | + expect(result.subscriptionId).toBe('sub-123'); |
| 217 | + expect(result.apiVersion).toBe('2018-07-01-preview'); |
| 218 | + }); |
| 219 | + |
| 220 | + it('should return undefined when API hub is disabled', () => { |
| 221 | + const azureDetails = { enabled: false }; |
| 222 | + const result = designer.testGetApiHubServiceDetails(azureDetails, {}); |
| 223 | + expect(result).toBeUndefined(); |
| 224 | + }); |
| 225 | + }); |
| 226 | + |
| 227 | + describe('getInterpolateConnectionData', () => { |
| 228 | + it('should return falsy data as-is', () => { |
| 229 | + expect(designer.testGetInterpolateConnectionData('')).toBe(''); |
| 230 | + }); |
| 231 | + |
| 232 | + it('should handle connections data with no managed API connections', () => { |
| 233 | + const data = JSON.stringify({ serviceProviderConnections: {} }); |
| 234 | + const result = designer.testGetInterpolateConnectionData(data); |
| 235 | + expect(JSON.parse(result)).toEqual({ serviceProviderConnections: {} }); |
| 236 | + }); |
| 237 | + }); |
| 238 | +}); |
0 commit comments