|
| 1 | +import { stubInterface } from 'ts-sinon'; |
1 | 2 | import { describe, it, expect, beforeEach, vi } from 'vitest'; |
2 | 3 | import { CancellationToken, RequestHandler } from 'vscode-languageserver-protocol'; |
3 | | -import { TextDocument } from 'vscode-languageserver-textdocument'; |
4 | 4 | import { getEntityMap } from '../../../src/context/SectionContextBuilder'; |
5 | | -import { Document } from '../../../src/document/Document'; |
| 5 | +import { CloudFormationFileType, Document } from '../../../src/document/Document'; |
6 | 6 | import { |
7 | 7 | getManagedResourceStackTemplateHandler, |
8 | 8 | importResourceStateHandler, |
@@ -168,33 +168,39 @@ describe('ResourceHandler - removeResourceTypeHandler', () => { |
168 | 168 | describe('ResourceHandler - importResourceStateHandler', () => { |
169 | 169 | let mockComponents: ReturnType<typeof createMockComponents>; |
170 | 170 | let handler: RequestHandler<ResourceStateParams, ResourceStateResult, void>; |
| 171 | + const params = { |
| 172 | + textDocument: { uri: 'docUri' }, |
| 173 | + resourceSelections: [ |
| 174 | + { |
| 175 | + resourceType: 'AWS::S3::Bucket', |
| 176 | + resourceIdentifiers: ['bucket1234'], |
| 177 | + }, |
| 178 | + ], |
| 179 | + purpose: ResourceStatePurpose.IMPORT, |
| 180 | + }; |
171 | 181 |
|
172 | 182 | beforeEach(() => { |
173 | 183 | vi.clearAllMocks(); |
174 | 184 | mockComponents = createMockComponents(); |
175 | 185 | handler = importResourceStateHandler(mockComponents); |
176 | 186 | }); |
177 | 187 |
|
178 | | - it('should throw error if template file is not open', async () => { |
179 | | - mockComponents.documentManager.get.returns( |
180 | | - new Document(TextDocument.create('sample.yaml', 'yaml', 1, 'Hello:')), |
181 | | - ); |
182 | | - mockComponents.documentManager.isTemplate.resolves(false); |
| 188 | + it('should throw error if document not found', async () => { |
| 189 | + mockComponents.documentManager.get.returns(undefined); |
183 | 190 |
|
184 | 191 | await expect(async () => { |
185 | | - await handler( |
186 | | - { |
187 | | - textDocument: { uri: 'docUri' }, |
188 | | - resourceSelections: [ |
189 | | - { |
190 | | - resourceType: 'AWS::S3::Bucket', |
191 | | - resourceIdentifiers: ['bucket1234'], |
192 | | - }, |
193 | | - ], |
194 | | - purpose: ResourceStatePurpose.IMPORT, |
195 | | - }, |
196 | | - CancellationToken.None, |
197 | | - ); |
198 | | - }).rejects.toThrow('Must open CloudFormation template to import or clone resource state'); |
| 192 | + await handler(params, CancellationToken.None); |
| 193 | + }).rejects.toThrow('Import failed: docUri not found'); |
| 194 | + }); |
| 195 | + |
| 196 | + it('should throw error if document is not a valid CloudFormation template', async () => { |
| 197 | + const mockDoc = stubInterface<Document>(); |
| 198 | + mockDoc.isTemplate.returns(false); |
| 199 | + Object.defineProperty(mockDoc, 'cfnFileType', { value: CloudFormationFileType.Other }); |
| 200 | + mockComponents.documentManager.get.returns(mockDoc); |
| 201 | + |
| 202 | + await expect(async () => { |
| 203 | + await handler(params, CancellationToken.None); |
| 204 | + }).rejects.toThrow('Import failed: docUri is not a valid CloudFormation template'); |
199 | 205 | }); |
200 | 206 | }); |
0 commit comments