From cd9ad5c3fd829b3ea59b87d2c2c6c8e52572c07c Mon Sep 17 00:00:00 2001 From: Philip Carneiro Date: Tue, 1 Oct 2024 09:37:11 +0100 Subject: [PATCH] add tests --- test/suite/commands.test.ts | 1 - test/suite/panels.test.ts | 89 +++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/test/suite/commands.test.ts b/test/suite/commands.test.ts index 6d997fa6..46471e04 100644 --- a/test/suite/commands.test.ts +++ b/test/suite/commands.test.ts @@ -15,7 +15,6 @@ import assert from "assert"; import mock from "mock-fs"; import * as sinon from "sinon"; import * as vscode from "vscode"; -import * as fs from "fs"; import * as dataSourceCommand from "../../src/commands/dataSourceCommand"; import * as installTools from "../../src/commands/installTools"; import * as serverCommand from "../../src/commands/serverCommand"; diff --git a/test/suite/panels.test.ts b/test/suite/panels.test.ts index 7af06678..fe9d4e20 100644 --- a/test/suite/panels.test.ts +++ b/test/suite/panels.test.ts @@ -19,6 +19,7 @@ import { createDefaultDataSourceFile } from "../../src/models/dataSource"; import { DataSourcesPanel } from "../../src/panels/datasource"; import { KdbResultsViewProvider } from "../../src/services/resultsPanelProvider"; import * as utils from "../../src/utils/execution"; +import * as coreUtils from "../../src/utils/core"; import { InsightsNode, KdbNode } from "../../src/services/kdbTreeProvider"; import { TreeItemCollapsibleState } from "vscode"; import { NewConnectionPannel } from "../../src/panels/newConnection"; @@ -383,6 +384,94 @@ describe("WebPanels", () => { }); }); + describe("updateWebView", () => { + const uriTest: vscode.Uri = vscode.Uri.parse("test"); + + let resultsPanel: KdbResultsViewProvider; + let postMessageStub: sinon.SinonStub; + let kdbOutputLogStub: sinon.SinonStub; + let convertToGridStub: sinon.SinonStub; + + beforeEach(() => { + resultsPanel = new KdbResultsViewProvider(uriTest); + resultsPanel["_view"] = { + webview: { + postMessage: sinon.stub(), + }, + } as any; + postMessageStub = resultsPanel["_view"].webview + .postMessage as sinon.SinonStub; + kdbOutputLogStub = sinon.stub(coreUtils, "kdbOutputLog"); + convertToGridStub = sinon.stub(resultsPanel, "convertToGrid"); + }); + + afterEach(() => { + sinon.restore(); + }); + + it("should log an error if there is no view to update", () => { + resultsPanel["_view"] = undefined; + resultsPanel.updateWebView("test"); + sinon.assert.calledWith( + kdbOutputLogStub, + "[Results Tab] No view to update", + "ERROR", + ); + }); + + it("should handle string queryResult", () => { + const queryResult = "test string"; + resultsPanel.updateWebView(queryResult); + sinon.assert.calledWith(postMessageStub, { + command: "setResultsContent", + results: `

test string

`, + }); + }); + + it("should handle number queryResult", () => { + const queryResult = 123; + resultsPanel.updateWebView(queryResult); + sinon.assert.calledWith(postMessageStub, { + command: "setResultsContent", + results: `

123

`, + }); + }); + + it("should handle empty string queryResult", () => { + const queryResult = ""; + resultsPanel.updateWebView(queryResult); + sinon.assert.calledWith(postMessageStub, { + command: "setResultsContent", + results: "

No results to show

", + }); + }); + + it("should handle object queryResult and call convertToGrid", () => { + const queryResult = { data: "test" }; + const gridOptions = { columnDefs: [], rowData: [] }; + convertToGridStub.returns(gridOptions); + resultsPanel.updateWebView(queryResult); + sinon.assert.calledWith( + convertToGridStub, + queryResult, + resultsPanel.isInsights, + ); + sinon.assert.calledWith(postMessageStub, { + command: "setGridOptions", + gridOptions: gridOptions, + }); + }); + + it("should handle null queryResult", () => { + const queryResult = null; + resultsPanel.updateWebView(queryResult); + sinon.assert.calledWith(postMessageStub, { + command: "setResultsContent", + results: "", + }); + }); + }); + describe("_getWebviewContent", () => { const uriTest: vscode.Uri = vscode.Uri.parse("test");