|
8 | 8 | import * as vscode from 'vscode'; |
9 | 9 | import { SelectFileName, SelectLwcComponentType } from '../../../../src/commands/util/parameterGatherers'; |
10 | 10 | import { nls } from '../../../../src/messages'; |
| 11 | +import { SalesforceProjectConfig } from '../../../../src/salesforceProject'; |
11 | 12 |
|
12 | 13 | describe('ParameterGatherers Unit Tests.', () => { |
13 | 14 | describe('SelectFileName', () => { |
@@ -94,63 +95,136 @@ describe('ParameterGatherers Unit Tests.', () => { |
94 | 95 | }); |
95 | 96 |
|
96 | 97 | describe('SelectLwcComponentType', () => { |
97 | | - let typeScriptSupportSetting = true; |
98 | | - let workspaceConfigurationSpy: jest.SpyInstance; |
99 | | - const mockConfig = { |
100 | | - get: () => typeScriptSupportSetting |
101 | | - }; |
| 98 | + let getValueSpy: jest.SpyInstance; |
| 99 | + let getConfigSpy: jest.SpyInstance; |
| 100 | + let showInfoMessageSpy: jest.SpyInstance; |
| 101 | + |
102 | 102 | beforeEach(() => { |
103 | | - typeScriptSupportSetting = true; |
104 | | - workspaceConfigurationSpy = jest.spyOn(vscode.workspace, 'getConfiguration'); |
| 103 | + getValueSpy = jest.spyOn(SalesforceProjectConfig, 'getValue'); |
| 104 | + getConfigSpy = jest.spyOn(vscode.workspace, 'getConfiguration'); |
| 105 | + showInfoMessageSpy = jest.spyOn(vscode.window, 'showInformationMessage'); |
| 106 | + }); |
| 107 | + |
| 108 | + afterEach(() => { |
| 109 | + jest.restoreAllMocks(); |
105 | 110 | }); |
106 | | - it('Should return JavaScript option when no config value', async () => { |
107 | | - typeScriptSupportSetting = false; |
108 | | - workspaceConfigurationSpy.mockReturnValue(mockConfig); |
| 111 | + |
| 112 | + it('Should return TypeScript when defaultLWCLanguage is typescript', async () => { |
| 113 | + getValueSpy.mockResolvedValue('typescript'); |
109 | 114 | const selectLwcComponentTypeInstance = new SelectLwcComponentType(); |
110 | | - const showMenuSpy = jest.spyOn(selectLwcComponentTypeInstance, 'showMenu').mockImplementation(jest.fn()); |
| 115 | + const showMenuSpy = jest.spyOn(selectLwcComponentTypeInstance, 'showMenu'); |
| 116 | + |
111 | 117 | const result = await selectLwcComponentTypeInstance.gather(); |
| 118 | + |
| 119 | + expect(result).toEqual({ |
| 120 | + type: 'CONTINUE', |
| 121 | + data: { extension: 'TypeScript' } |
| 122 | + }); |
| 123 | + expect(showMenuSpy).not.toHaveBeenCalled(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('Should return JavaScript when defaultLWCLanguage is javascript', async () => { |
| 127 | + getValueSpy.mockResolvedValue('javascript'); |
| 128 | + const selectLwcComponentTypeInstance = new SelectLwcComponentType(); |
| 129 | + const showMenuSpy = jest.spyOn(selectLwcComponentTypeInstance, 'showMenu'); |
| 130 | + |
| 131 | + const result = await selectLwcComponentTypeInstance.gather(); |
| 132 | + |
112 | 133 | expect(result).toEqual({ |
113 | 134 | type: 'CONTINUE', |
114 | 135 | data: { extension: 'JavaScript' } |
115 | 136 | }); |
116 | 137 | expect(showMenuSpy).not.toHaveBeenCalled(); |
117 | 138 | }); |
118 | 139 |
|
119 | | - it('Should return TypeScript when selected from menu', async () => { |
120 | | - workspaceConfigurationSpy.mockReturnValue(mockConfig); |
| 140 | + it('Should prompt user when defaultLWCLanguage is undefined', async () => { |
| 141 | + getValueSpy.mockResolvedValue(undefined); |
| 142 | + const mockConfig = { get: jest.fn().mockReturnValue(false) }; |
| 143 | + getConfigSpy.mockReturnValue(mockConfig); |
| 144 | + |
121 | 145 | const selectLwcComponentTypeInstance = new SelectLwcComponentType(); |
122 | 146 | const showMenuSpy = jest |
123 | 147 | .spyOn(selectLwcComponentTypeInstance, 'showMenu') |
124 | | - .mockImplementation(jest.fn().mockReturnValueOnce('TypeScript')); |
| 148 | + .mockResolvedValue('TypeScript'); |
| 149 | + |
125 | 150 | const result = await selectLwcComponentTypeInstance.gather(); |
| 151 | + |
126 | 152 | expect(result).toEqual({ |
127 | 153 | type: 'CONTINUE', |
128 | 154 | data: { extension: 'TypeScript' } |
129 | 155 | }); |
130 | 156 | expect(showMenuSpy).toHaveBeenCalled(); |
131 | 157 | }); |
132 | 158 |
|
133 | | - it('Should return JavaScript when selected from menu', async () => { |
134 | | - workspaceConfigurationSpy.mockReturnValue(mockConfig); |
| 159 | + it('Should fall back to legacy flag when defaultLWCLanguage throws error', async () => { |
| 160 | + getValueSpy.mockRejectedValue(new Error('Config not available')); |
| 161 | + const mockConfig = { get: jest.fn().mockReturnValue(true) }; |
| 162 | + getConfigSpy.mockReturnValue(mockConfig); |
| 163 | + showInfoMessageSpy.mockResolvedValue(undefined); |
| 164 | + |
| 165 | + const selectLwcComponentTypeInstance = new SelectLwcComponentType(); |
| 166 | + const showMenuSpy = jest.spyOn(selectLwcComponentTypeInstance, 'showMenu'); |
| 167 | + |
| 168 | + const result = await selectLwcComponentTypeInstance.gather(); |
| 169 | + |
| 170 | + expect(result).toEqual({ |
| 171 | + type: 'CONTINUE', |
| 172 | + data: { extension: 'TypeScript' } |
| 173 | + }); |
| 174 | + expect(showInfoMessageSpy).toHaveBeenCalled(); |
| 175 | + expect(showMenuSpy).not.toHaveBeenCalled(); |
| 176 | + }); |
| 177 | + |
| 178 | + it('Should show deprecation warning for legacy flag', async () => { |
| 179 | + getValueSpy.mockResolvedValue(undefined); |
| 180 | + const mockConfig = { get: jest.fn().mockReturnValue(true) }; |
| 181 | + getConfigSpy.mockReturnValue(mockConfig); |
| 182 | + showInfoMessageSpy.mockResolvedValue(undefined); |
| 183 | + |
| 184 | + const selectLwcComponentTypeInstance = new SelectLwcComponentType(); |
| 185 | + |
| 186 | + const result = await selectLwcComponentTypeInstance.gather(); |
| 187 | + |
| 188 | + expect(result).toEqual({ |
| 189 | + type: 'CONTINUE', |
| 190 | + data: { extension: 'TypeScript' } |
| 191 | + }); |
| 192 | + expect(showInfoMessageSpy).toHaveBeenCalledWith( |
| 193 | + expect.stringContaining('deprecated') |
| 194 | + ); |
| 195 | + }); |
| 196 | + |
| 197 | + it('Should prompt user when both configs are not set', async () => { |
| 198 | + getValueSpy.mockResolvedValue(undefined); |
| 199 | + const mockConfig = { get: jest.fn().mockReturnValue(false) }; |
| 200 | + getConfigSpy.mockReturnValue(mockConfig); |
| 201 | + |
135 | 202 | const selectLwcComponentTypeInstance = new SelectLwcComponentType(); |
136 | 203 | const showMenuSpy = jest |
137 | 204 | .spyOn(selectLwcComponentTypeInstance, 'showMenu') |
138 | | - .mockImplementation(jest.fn().mockReturnValueOnce('JavaScript')); |
| 205 | + .mockResolvedValue('JavaScript'); |
| 206 | + |
139 | 207 | const result = await selectLwcComponentTypeInstance.gather(); |
| 208 | + |
140 | 209 | expect(result).toEqual({ |
141 | 210 | type: 'CONTINUE', |
142 | 211 | data: { extension: 'JavaScript' } |
143 | 212 | }); |
144 | | - expect(showMenuSpy).toHaveBeenCalled(); |
| 213 | + expect(showMenuSpy).toHaveBeenCalledWith(['JavaScript', 'TypeScript'], 'parameter_gatherer_select_lwc_type'); |
145 | 214 | }); |
146 | 215 |
|
147 | | - it('Should cancel when selected from menu', async () => { |
148 | | - workspaceConfigurationSpy.mockReturnValue(mockConfig); |
| 216 | + it('Should return CANCEL when user cancels prompt', async () => { |
| 217 | + getValueSpy.mockResolvedValue(undefined); |
| 218 | + const mockConfig = { get: jest.fn().mockReturnValue(false) }; |
| 219 | + getConfigSpy.mockReturnValue(mockConfig); |
| 220 | + |
149 | 221 | const selectLwcComponentTypeInstance = new SelectLwcComponentType(); |
150 | 222 | const showMenuSpy = jest |
151 | 223 | .spyOn(selectLwcComponentTypeInstance, 'showMenu') |
152 | | - .mockImplementation(jest.fn().mockReturnValueOnce(null)); |
| 224 | + .mockResolvedValue(undefined); |
| 225 | + |
153 | 226 | const result = await selectLwcComponentTypeInstance.gather(); |
| 227 | + |
154 | 228 | expect(result).toEqual({ |
155 | 229 | type: 'CANCEL' |
156 | 230 | }); |
|
0 commit comments