-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add happy path test for minimal arg language service
This adds some tests to: - Test against the minimal arguments needed for the getLanguageService() api - Test the happy path of the hover functionality to ensure hover works without any of the optional arguments. Signed-off-by: Nikolas Komonen <nkomonen@amazon.com>
- Loading branch information
1 parent
a5c0ec0
commit f9ca463
Showing
1 changed file
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Red Hat, Inc. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
import { assert } from 'chai'; | ||
import { Position, TextDocument } from 'vscode-languageserver-textdocument'; | ||
import { getLanguageService, LanguageService, SchemaRequestService, WorkspaceContextService } from '../src'; | ||
import { workspaceContext } from '../src/languageservice/services/schemaRequestHandler'; | ||
import { caretPosition, setupSchemaIDTextDocument } from './utils/testHelper'; | ||
|
||
/** | ||
* Builds a simple schema request service | ||
* @param contentMap Mapping of a schema uri to the schema content | ||
*/ | ||
function schemaRequestServiceBuilder(contentMap: { [uri: string]: string }): SchemaRequestService { | ||
return async (uri: string) => { | ||
return contentMap[uri]; | ||
}; | ||
} | ||
|
||
describe('getLanguageService()', () => { | ||
it('successfully creates an instance without optional arguments', () => { | ||
getLanguageService({ | ||
schemaRequestService: {} as SchemaRequestService, | ||
workspaceContext: {} as WorkspaceContextService, | ||
}); | ||
}); | ||
|
||
describe('minimal language service hover happy path', () => { | ||
const schemaUri = 'my.schema.uri'; | ||
const schemaContentMap: { [uri: string]: string } = {}; | ||
|
||
let schemaRequestService: SchemaRequestService; | ||
let textDocument: TextDocument; | ||
let hoverPosition: Position; // Position the 'mouse' is hovering on the content | ||
let minimalYamlService: LanguageService; | ||
|
||
before(async () => { | ||
// Setup object that resolves schema content | ||
schemaContentMap[schemaUri] = ` | ||
{ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"type": "object", | ||
"properties": { | ||
"firstName": { | ||
"type": "string", | ||
"description": "The person's first name." | ||
} | ||
} | ||
} | ||
`; | ||
schemaRequestService = schemaRequestServiceBuilder(schemaContentMap); | ||
|
||
// Setup the document and where the hover is on it | ||
const contentWithHoverPosition = 'fi|r|stName: "Nikolas"'; | ||
const { content, position: offset } = caretPosition(contentWithHoverPosition); | ||
textDocument = setupSchemaIDTextDocument(content); | ||
hoverPosition = textDocument.positionAt(offset); | ||
|
||
// Setup minimal language service + indicate to provide hover functionality | ||
minimalYamlService = getLanguageService({ | ||
schemaRequestService: schemaRequestService, | ||
workspaceContext: workspaceContext, | ||
}); | ||
minimalYamlService.configure({ | ||
hover: true, | ||
schemas: [ | ||
{ | ||
fileMatch: [textDocument.uri], | ||
uri: schemaUri, | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
it('successfully creates an instance without optional arguments', async () => { | ||
const result = await minimalYamlService.doHover(textDocument, hoverPosition); | ||
|
||
assert.deepEqual(result, { | ||
contents: { | ||
kind: 'markdown', | ||
value: "The person's first name\\.\n\nSource: [my.schema.uri](my.schema.uri)", | ||
}, | ||
range: { | ||
start: { | ||
line: 0, | ||
character: 0, | ||
}, | ||
end: { | ||
line: 0, | ||
character: 9, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |