-
Notifications
You must be signed in to change notification settings - Fork 226
Add tests for Model Details View #2735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cb2802e
Add mode details panel test
norascheuch 6e4e89a
Move dataProvider creation to beforeEach
norascheuch 392e76d
Add getChildren tests
norascheuch 45f7d22
Add reveal test
norascheuch c56936e
create and use factories
norascheuch 0daa06b
rename test and remove boolean type
norascheuch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
extensions/ql-vscode/test/factories/data-extension/external-api-factories.ts
This file contains hidden or 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,57 @@ | ||
| import { | ||
| Usage, | ||
| ExternalApiUsage, | ||
| CallClassification, | ||
| } from "../../../src/data-extensions-editor/external-api-usage"; | ||
| import { ModeledMethodType } from "../../../src/data-extensions-editor/modeled-method"; | ||
| import { ResolvableLocationValue } from "../../../src/common/bqrs-cli-types"; | ||
|
|
||
| export function createExternalApiUsage({ | ||
| library = "test", | ||
| supported = true, | ||
| supportedType = "none" as ModeledMethodType, | ||
| usages = [], | ||
| signature = "test", | ||
| packageName = "test", | ||
| typeName = "test", | ||
| methodName = "test", | ||
| methodParameters = "test", | ||
| }: { | ||
| library?: string; | ||
| supported?: boolean; | ||
| supportedType?: ModeledMethodType; | ||
| usages?: Usage[]; | ||
| signature?: string; | ||
| packageName?: string; | ||
| typeName?: string; | ||
| methodName?: string; | ||
| methodParameters?: string; | ||
| } = {}): ExternalApiUsage { | ||
| return { | ||
| library, | ||
| supported, | ||
| supportedType, | ||
| usages, | ||
| signature, | ||
| packageName, | ||
| typeName, | ||
| methodName, | ||
| methodParameters, | ||
| }; | ||
| } | ||
|
|
||
| export function createUsage({ | ||
| classification = CallClassification.Unknown, | ||
| label = "test", | ||
| url = {} as ResolvableLocationValue, | ||
| }: { | ||
| classification?: CallClassification; | ||
| label?: string; | ||
| url?: ResolvableLocationValue; | ||
| } = {}): Usage { | ||
| return { | ||
| classification, | ||
| label, | ||
| url, | ||
| }; | ||
| } |
This file contains hidden or 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
73 changes: 73 additions & 0 deletions
73
...scode-tests/no-workspace/data-extensions-editor/model-details/model-details-panel.test.ts
This file contains hidden or 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,73 @@ | ||
| import { window, TreeView } from "vscode"; | ||
| import { CodeQLCliServer } from "../../../../../src/codeql-cli/cli"; | ||
| import { ExternalApiUsage } from "../../../../../src/data-extensions-editor/external-api-usage"; | ||
| import { ModelDetailsPanel } from "../../../../../src/data-extensions-editor/model-details/model-details-panel"; | ||
| import { DatabaseItem } from "../../../../../src/databases/local-databases"; | ||
| import { mockedObject } from "../../../utils/mocking.helpers"; | ||
| import { | ||
| createExternalApiUsage, | ||
| createUsage, | ||
| } from "../../../../factories/data-extension/external-api-factories"; | ||
|
|
||
| describe("ModelDetailsPanel", () => { | ||
| const mockCliServer = mockedObject<CodeQLCliServer>({}); | ||
| const dbItem = mockedObject<DatabaseItem>({ | ||
| getSourceLocationPrefix: () => "test", | ||
| }); | ||
|
|
||
| describe("setState", () => { | ||
| const hideModeledApis = false; | ||
| const externalApiUsages: ExternalApiUsage[] = [createExternalApiUsage()]; | ||
|
|
||
| it("should update the tree view with the correct batch number", async () => { | ||
| const mockTreeView = { | ||
| badge: undefined, | ||
| } as TreeView<unknown>; | ||
| jest.spyOn(window, "createTreeView").mockReturnValue(mockTreeView); | ||
|
|
||
| const panel = new ModelDetailsPanel(mockCliServer); | ||
| await panel.setState(externalApiUsages, dbItem, hideModeledApis); | ||
|
|
||
| expect(mockTreeView.badge?.value).toBe(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe("revealItem", () => { | ||
| let mockTreeView: TreeView<unknown>; | ||
|
|
||
| const hideModeledApis: boolean = false; | ||
| const usage = createUsage(); | ||
|
|
||
| beforeEach(() => { | ||
| mockTreeView = { | ||
| reveal: jest.fn(), | ||
| } as unknown as TreeView<unknown>; | ||
| jest.spyOn(window, "createTreeView").mockReturnValue(mockTreeView); | ||
| }); | ||
|
|
||
| it("should reveal the correct item in the tree view", async () => { | ||
| const externalApiUsages = [ | ||
| createExternalApiUsage({ | ||
| usages: [usage], | ||
| }), | ||
| ]; | ||
|
|
||
| const panel = new ModelDetailsPanel(mockCliServer); | ||
| await panel.setState(externalApiUsages, dbItem, hideModeledApis); | ||
|
|
||
| await panel.revealItem(usage); | ||
|
|
||
| expect(mockTreeView.reveal).toHaveBeenCalledWith(usage); | ||
| }); | ||
|
|
||
| it("should do nothing if usage cannot be found", async () => { | ||
| const externalApiUsages = [createExternalApiUsage({})]; | ||
| const panel = new ModelDetailsPanel(mockCliServer); | ||
| await panel.setState(externalApiUsages, dbItem, hideModeledApis); | ||
|
|
||
| await panel.revealItem(usage); | ||
|
|
||
| expect(mockTreeView.reveal).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is similar to
as any as Xso we should ideally avoid it. Is there a way to get around it easily, or is creating mockTreeView<>T>items too painful? I'm happy to take it on as tech debt too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem lies in the
reveal: jest.fn(). Without it we could remove theas unknown. With it we would have to mock the vs code reveal method better. I'm not sure if that is easily possible? We only check ifrevealwas called, not what it does. Therefore I leaned towards leaving it as is. However, can we introduce other errors by usingas unknownhere apart from the reveal method?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets leave it as is. I imagine creating the mock properly isn't trivial.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for talking this through. I think
mockedObjectcould be a good solution for this case. I've opened #2768 for this where I've tried to explain my reasoning, so let me know what you think.