From 7a1da8debb9d2bd370e543642351f447338121cf Mon Sep 17 00:00:00 2001 From: Philip Carneiro Date: Tue, 4 Mar 2025 10:25:56 +0000 Subject: [PATCH 1/5] add optional params --- src/webview/components/kdbDataSourceView.ts | 259 +++++++++++++++----- src/webview/components/styles.ts | 101 ++++++++ src/webview/main.ts | 4 + 3 files changed, 304 insertions(+), 60 deletions(-) diff --git a/src/webview/components/kdbDataSourceView.ts b/src/webview/components/kdbDataSourceView.ts index 68c7a579..ea32b398 100644 --- a/src/webview/components/kdbDataSourceView.ts +++ b/src/webview/components/kdbDataSourceView.ts @@ -203,6 +203,30 @@ export class KdbDataSourceView extends LitElement { `; } + renderTrashIcon() { + return html` + + + + + + + + + + + `; + } + /* istanbul ignore next */ postMessage(msg: Partial) { this.vscode.postMessage(msg); @@ -908,11 +932,11 @@ export class KdbDataSourceView extends LitElement { renderUDA() { return html` -
+
0) { - UDAParams.push(UDAReqParams); + if (UDAVisibleParams.length > 0) { + UDAParams.push(UDAVisibleParams); } else { UDAParams.push(UDANoParams); } @@ -1006,6 +1031,86 @@ export class KdbDataSourceView extends LitElement { return UDAParams; } + renderUDAAddParamButton() { + return html` + + + + Add Parameter + ${this.renderUDAAddParamBtnOptions()} + + `; + } + + renderUDAAddParamBtnOptions() { + return html` + + ${this.renderUDAOptionalParamsOpts()} + + `; + } + + renderUDAOptionalParamsOpts() { + const UDAParamsList: any[] = []; + if (this.userSelectedUDA) { + const UDAOptionalParams = this.userSelectedUDA.params.filter( + (param) => param.isReq === false, + ); + if (UDAOptionalParams.length > 0) { + for (const param of UDAOptionalParams) { + UDAParamsList.push( + html`${param.name}
${param.description}
`, + ); + } + } + } + + if (UDAParamsList.length === 0) { + UDAParamsList.push( + html`No optional parameters available`, + ); + } + return UDAParamsList; + } + + renderDeleteParamButton(param: UDAParam) { + if (!param.isReq) { + return html` + ${this.renderTrashIcon()} + `; + } + } + retrieveUDAParamInputType(type: string | undefined) { switch (type) { case "number": @@ -1024,11 +1129,11 @@ export class KdbDataSourceView extends LitElement { } } - renderReqUDAParams() { + renderVisibleUDAParams() { const UDAParamsList = []; if (this.userSelectedUDA) { const UDAReqParams = this.userSelectedUDA.params.filter( - (param) => param.isReq === true, + (param) => param.isVisible === true, ); if (UDAReqParams.length > 0) { for (const param of UDAReqParams) { @@ -1060,15 +1165,18 @@ export class KdbDataSourceView extends LitElement { ? param.default : false; return html` - ${param.name} +
+ ${param.name} + ${this.renderDeleteParamButton(param)} +
`; } @@ -1079,14 +1187,20 @@ export class KdbDataSourceView extends LitElement { ? param.default : ""; return html` - +
+ + ${this.renderDeleteParamButton(param)} +
`; } @@ -1104,24 +1218,35 @@ export class KdbDataSourceView extends LitElement { ? param.typeStrings[0] : ""; + const renderDeleteParam = this.renderDeleteParamButton(param); + return html` -
- - ${param.typeStrings?.map( - (option) => - html`${option}`, - )} - - ${this.renderMultiTypeInput(param)} +
+
+ + ${param.typeStrings?.map( + (option) => + html`${option}`, + )} + + ${this.renderMultiTypeInput(param)} +
+
+ ${this.renderDeleteParamButton(param)} +
`; } @@ -1134,27 +1259,39 @@ export class KdbDataSourceView extends LitElement { : param.default ? param.default : ""; + const renderDeleteParam = this.renderDeleteParamButton(param); return html` - +
+
+ +
+
+ ${this.renderDeleteParamButton(param)} +
+
`; } @@ -1184,6 +1321,7 @@ export class KdbDataSourceView extends LitElement { } else if (inputType === "textarea") { return html` Date: Tue, 4 Mar 2025 10:54:07 +0000 Subject: [PATCH 2/5] improve code quality --- src/webview/components/kdbDataSourceView.ts | 469 ++++++++++---------- 1 file changed, 224 insertions(+), 245 deletions(-) diff --git a/src/webview/components/kdbDataSourceView.ts b/src/webview/components/kdbDataSourceView.ts index ea32b398..15187226 100644 --- a/src/webview/components/kdbDataSourceView.ts +++ b/src/webview/components/kdbDataSourceView.ts @@ -924,8 +924,7 @@ export class KdbDataSourceView extends LitElement { } getSavedUDAStatus(): string { - const udaExists = this.UDAs.some((uda) => uda.name === this.selectedUDA); - return udaExists + return this.UDAs.some((uda) => uda.name === this.selectedUDA) ? "Available UDAs" : "Pre-selected UDA is not available for this connection."; } @@ -938,15 +937,7 @@ export class KdbDataSourceView extends LitElement { .value="${live(encodeURIComponent(this.selectedUDA))}" class="reset-widths-limit width-97-pct" search - @sl-change="${(event: Event) => { - this.selectedUDA = decodeURIComponent( - (event.target as HTMLSelectElement).value, - ); - this.userSelectedUDA = this.UDAs.find( - (uda) => uda.name === this.selectedUDA, - ); - this.requestChange(); - }}"> + @sl-change="${this.handleUDAChange}"> ${this.userSelectedUDA.description}` : ""} - ${this.isMetaLoaded + + ${this.isMetaLoaded ? this.getSavedUDAStatus() - : "Connect to a server environment to access the available UDAs."} + : "Connect to a server environment to access the available UDAs."} + ${this.renderUDAOptions()} ${this.renderUDADetails()} ${this.renderUDAParams()} @@ -968,91 +959,96 @@ export class KdbDataSourceView extends LitElement { `; } + handleUDAChange(event: Event) { + this.selectedUDA = decodeURIComponent( + (event.target as HTMLSelectElement).value, + ); + this.userSelectedUDA = this.UDAs.find( + (uda) => uda.name === this.selectedUDA, + ); + this.requestChange(); + } + renderUDAOptions() { - if (this.isInsights && this.isMetaLoaded) { - const udaOptions = this.UDAs.map((uda) => { - return html` - ${uda.name} - ${uda.description} - `; - }); - if (udaOptions.length === 0) { - udaOptions.push( - html`No deployed UDAs available`, - ); - } - return udaOptions; + if (!this.isInsights || !this.isMetaLoaded) return []; + + const udaOptions = this.UDAs.map( + (uda) => html` + ${uda.name} + ${uda.description} + `, + ); + + if (udaOptions.length === 0) { + udaOptions.push(html` + No deployed UDAs available + `); } - return []; + + return udaOptions; } renderUDADetails() { - const UDADetails = []; - if (this.userSelectedUDA) { - if (this.userSelectedUDA.description) { - UDADetails.push(html`${this.userSelectedUDA.description}
`); - } - if (this.userSelectedUDA.return) { - const returnType = Array.isArray(this.userSelectedUDA.return.type) - ? this.userSelectedUDA.return.type.join(", ") - : this.userSelectedUDA.return.type; - UDADetails.push( - html`Return Description: ${this.userSelectedUDA.return.description}
Return - Type: ${returnType}
`, - ); - } + if (!this.userSelectedUDA) return []; + + const details = []; + if (this.userSelectedUDA.description) { + details.push(html`${this.userSelectedUDA.description}
`); } - if (UDADetails.length !== 0) { - return html`${UDADetails}`; + if (this.userSelectedUDA.return) { + const returnType = Array.isArray(this.userSelectedUDA.return.type) + ? this.userSelectedUDA.return.type.join(", ") + : this.userSelectedUDA.return.type; + details.push(html` + Return Description: ${this.userSelectedUDA.return.description}
+ Return Type: ${returnType}
+ `); } - return UDADetails; + + return details.length ? html`${details}` : details; } renderUDAParams() { - const UDAParams = []; - if (this.userSelectedUDA) { - UDAParams.push(html`PARAMETERS:`); - const UDAVisibleParams = this.renderVisibleUDAParams(); - const UDANoParams = this.renderUDANoParams(); - const UDAInvalidParams = this.renderUDAInvalidParams(); - if (UDAInvalidParams !== "") { - UDAParams.push(UDAInvalidParams); - } else { - if (UDAVisibleParams.length > 0) { - UDAParams.push(UDAVisibleParams); - } else { - UDAParams.push(UDANoParams); - } - } + if (!this.userSelectedUDA) return []; + + const params = [html`PARAMETERS:`]; + const visibleParams = this.renderVisibleUDAParams(); + const noParams = this.renderUDANoParams(); + const invalidParams = this.renderUDAInvalidParams(); + + if (invalidParams) { + params.push(invalidParams); + } else { + params.push(...(visibleParams.length ? visibleParams : [noParams])); } - return UDAParams; + return params; } renderUDAAddParamButton() { return html` + @sl-select="${this.handleAddParamSelect}"> - + Add Parameter + + Add Parameter + ${this.renderUDAAddParamBtnOptions()} `; } + handleAddParamSelect(event: any) { + const paramSelected = event.detail.item.value; + const param = this.userSelectedUDA?.params.find( + (param) => param.name === paramSelected, + ); + if (param) { + param.isVisible = true; + } + this.requestChange(); + } + renderUDAAddParamBtnOptions() { return html` @@ -1062,108 +1058,94 @@ export class KdbDataSourceView extends LitElement { } renderUDAOptionalParamsOpts() { - const UDAParamsList: any[] = []; - if (this.userSelectedUDA) { - const UDAOptionalParams = this.userSelectedUDA.params.filter( - (param) => param.isReq === false, - ); - if (UDAOptionalParams.length > 0) { - for (const param of UDAOptionalParams) { - UDAParamsList.push( - html`${param.name}
${param.description}
`, - ); - } - } + if (!this.userSelectedUDA) { + return html` + No optional parameters available + `; } - if (UDAParamsList.length === 0) { - UDAParamsList.push( - html`No optional parameters available`, - ); - } - return UDAParamsList; - } + const optionalParams = this.userSelectedUDA.params.filter( + (param) => !param.isReq, + ); - renderDeleteParamButton(param: UDAParam) { - if (!param.isReq) { + if (optionalParams.length === 0) { return html` - ${this.renderTrashIcon()} + No optional parameters available `; } + + return optionalParams.map( + (param) => html` + + ${param.name}
${param.description} +
+ `, + ); + } + + renderDeleteUDAParamButton(param: UDAParam) { + if (param.isReq) return; + + return html` + + ${this.renderTrashIcon()} + + `; + } + + handleDeleteParam(param: UDAParam) { + param.isVisible = false; + param.value = undefined; + param.selectedMultiTypeString = undefined; + this.requestChange(); } retrieveUDAParamInputType(type: string | undefined) { - switch (type) { - case "number": - return "number"; - case "boolean": - return "checkbox"; - case "timestamp": - return "datetime-local"; - case "json": - return "textarea"; - case "multitype": - return "multitype"; - case "text": - default: - return "text"; - } + const inputTypes: { [key: string]: string } = { + number: "number", + boolean: "checkbox", + timestamp: "datetime-local", + json: "textarea", + multitype: "multitype", + text: "text", + }; + return inputTypes[type || "text"] || "text"; } renderVisibleUDAParams() { - const UDAParamsList = []; - if (this.userSelectedUDA) { - const UDAReqParams = this.userSelectedUDA.params.filter( - (param) => param.isVisible === true, + if (!this.userSelectedUDA) return []; + + return this.userSelectedUDA.params + .filter((param) => param.isVisible) + .map((param) => + this.renderUDAParam( + param, + this.retrieveUDAParamInputType(param.fieldType), + ), ); - if (UDAReqParams.length > 0) { - for (const param of UDAReqParams) { - const inputType = this.retrieveUDAParamInputType(param.fieldType); - UDAParamsList.push(this.renderUDAParam(param, inputType)); - } - } - } - return UDAParamsList; } renderUDAParam(param: UDAParam, inputType: string) { switch (inputType) { case "checkbox": - return this.renderCheckbox(param); + return this.renderUDACheckbox(param); case "textarea": - return this.renderTextarea(param); + return this.renderUDATextarea(param); case "multitype": - return this.renderMultitype(param); + return this.renderUDAMultitype(param); default: - return this.renderInput(param, inputType); + return this.renderUDAInput(param, inputType); } } - renderCheckbox(param: UDAParam) { - const isChecked = param.value - ? param.value - : param.default - ? param.default - : false; + renderUDACheckbox(param: UDAParam) { + const isChecked = param.value || param.default || false; return html`
${param.name} - ${this.renderDeleteParamButton(param)} + .checked="${live(isChecked)}"> + ${param.name} + + ${this.renderDeleteUDAParamButton(param)}
`; } - renderTextarea(param: UDAParam) { - const value = param.value - ? param.value - : param.default - ? param.default - : ""; + renderUDATextarea(param: UDAParam) { + const value = param.value || param.default || ""; return html`
- ${this.renderDeleteParamButton(param)} + }}"> + + ${this.renderDeleteUDAParamButton(param)}
`; } - renderMultitype(param: UDAParam) { - const selectedMultiTypeString = param.selectedMultiTypeString - ? param.selectedMultiTypeString - : param.typeStrings - ? param.typeStrings[0] - : ""; + renderUDAMultitype(param: UDAParam) { + const selectedMultiTypeString = + param.selectedMultiTypeString || param.typeStrings?.[0] || ""; param.selectedMultiTypeString = selectedMultiTypeString; - const value = param.selectedMultiTypeString - ? param.selectedMultiTypeString - : param.typeStrings - ? param.typeStrings[0] - : ""; - - const renderDeleteParam = this.renderDeleteParamButton(param); + const value = param.selectedMultiTypeString || param.typeStrings?.[0] || ""; + const renderDeleteParam = this.renderDeleteUDAParamButton(param); return html`
+ : "width-97-pct"} row align-top"> ${option}`, )} - ${this.renderMultiTypeInput(param)} + ${this.renderUDAMultiTypeInput(param)}
-
- ${this.renderDeleteParamButton(param)} +
+ ${this.renderDeleteUDAParamButton(param)}
`; } - renderInput(param: UDAParam, inputType: string) { + renderUDAInput(param: UDAParam, inputType: string) { const validInputTypes = ["text", "number", "datetime-local"]; const type = validInputTypes.includes(inputType) ? inputType : "text"; - const value = param.value - ? param.value - : param.default - ? param.default - : ""; - const renderDeleteParam = this.renderDeleteParamButton(param); + const value = param.value || param.default || ""; + const renderDeleteParam = this.renderDeleteUDAParamButton(param); return html`
+ : "width-97-pct"} row align-top"> { param.value = (event.target as HTMLInputElement).value; this.requestChange(); - }}"> + }}"> +
-
- ${this.renderDeleteParamButton(param)} +
+ ${this.renderDeleteUDAParamButton(param)}
`; } - renderMultiTypeInput(param: UDAParam) { + renderUDAMultiTypeInput(param: UDAParam) { const selectedType = param.selectedMultiTypeString; const multiFieldType = param.multiFieldTypes?.find( (type) => Object.keys(type)[0] === selectedType, @@ -1305,59 +1273,70 @@ export class KdbDataSourceView extends LitElement { : "text"; const inputType = this.retrieveUDAParamInputType(fieldType); - if (inputType === "checkbox") { - return html` - Selected type: ${selectedType} - `; - } else if (inputType === "textarea") { - return html` - - `; - } else { - return html` - - `; + switch (inputType) { + case "checkbox": + return html` + + Selected type: ${selectedType} + + `; + case "textarea": + return html` + + + `; + default: + return html` + + + `; } } renderUDAInvalidParams() { - if (this.userSelectedUDA) { - if (this.userSelectedUDA.incompatibleError !== undefined) { - return html` - - ${this.renderExclamationTriangleIcon()} - Invalid Parameters
- The UDA you have selected cannot be queried because it has required - fields with types that are not supported. -
- `; - } + if (this.userSelectedUDA?.incompatibleError !== undefined) { + return html` + + ${this.renderExclamationTriangleIcon()} + Invalid Parameters
+ The UDA you have selected cannot be queried because it has required + fields with types that are not supported. +
+ `; } return ""; } From 9e1cb3d908bc6a6a943998dddf43b5eb0e0ad8db Mon Sep 17 00:00:00 2001 From: Philip Carneiro Date: Tue, 4 Mar 2025 10:59:41 +0000 Subject: [PATCH 3/5] fix tests --- test/suite/webview.test.ts | 50 +++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/test/suite/webview.test.ts b/test/suite/webview.test.ts index cf041c8c..d0956c35 100644 --- a/test/suite/webview.test.ts +++ b/test/suite/webview.test.ts @@ -243,31 +243,31 @@ describe("KdbDataSourceView", () => { describe("renderUDAParam", () => { it("should render UDA checkbox field", () => { - sinon.stub(view, "renderCheckbox").returns(html`checkbox`); + sinon.stub(view, "renderUDACheckbox").returns(html`checkbox`); const result = view.renderUDAParam(dummyUDAs[0].params[0], "checkbox"); assert.deepStrictEqual(result, html`checkbox`); }); it("should render UDA textarea field", () => { - sinon.stub(view, "renderTextarea").returns(html`textarea`); + sinon.stub(view, "renderUDATextarea").returns(html`textarea`); const result = view.renderUDAParam(dummyUDAs[0].params[0], "textarea"); assert.deepStrictEqual(result, html`textarea`); }); it("should render UDA multitype field", () => { - sinon.stub(view, "renderMultitype").returns(html`multitype`); + sinon.stub(view, "renderUDAMultitype").returns(html`multitype`); const result = view.renderUDAParam(dummyUDAs[0].params[0], "multitype"); assert.deepStrictEqual(result, html`multitype`); }); it("should render UDA input field", () => { - sinon.stub(view, "renderInput").returns(html`input`); + sinon.stub(view, "renderUDAInput").returns(html`input`); const result = view.renderUDAParam(dummyUDAs[0].params[0], "input"); assert.deepStrictEqual(result, html`input`); }); }); - describe("renderCheckbox", () => { + describe("renderUDACheckbox", () => { it("should render checkbox with value true", () => { const param: UDAParam = { name: "testParam", @@ -277,7 +277,7 @@ describe("KdbDataSourceView", () => { isReq: false, type: 0, }; - const result = view.renderCheckbox(param); + const result = view.renderUDACheckbox(param); assert.ok(result); }); @@ -290,7 +290,7 @@ describe("KdbDataSourceView", () => { isReq: false, type: 0, }; - const result = view.renderCheckbox(param); + const result = view.renderUDACheckbox(param); assert.ok(result); }); @@ -303,7 +303,7 @@ describe("KdbDataSourceView", () => { isReq: false, type: 0, }; - const result = view.renderCheckbox(param); + const result = view.renderUDACheckbox(param); assert.ok(result); }); @@ -316,7 +316,7 @@ describe("KdbDataSourceView", () => { isReq: false, type: 0, }; - const result = view.renderCheckbox(param); + const result = view.renderUDACheckbox(param); assert.ok(result); }); @@ -329,12 +329,12 @@ describe("KdbDataSourceView", () => { isReq: false, type: 0, }; - const result = view.renderCheckbox(param); + const result = view.renderUDACheckbox(param); assert.ok(result); }); }); - describe("renderTextarea", () => { + describe("renderUDATextarea", () => { it("should render textarea with value", () => { const param: UDAParam = { name: "testParam", @@ -344,7 +344,7 @@ describe("KdbDataSourceView", () => { isReq: false, type: 0, }; - const result = view.renderTextarea(param); + const result = view.renderUDATextarea(param); assert.ok(result); }); @@ -357,7 +357,7 @@ describe("KdbDataSourceView", () => { isReq: false, type: 0, }; - const result = view.renderTextarea(param); + const result = view.renderUDATextarea(param); assert.ok(result); }); @@ -370,12 +370,12 @@ describe("KdbDataSourceView", () => { isReq: false, type: 0, }; - const result = view.renderTextarea(param); + const result = view.renderUDATextarea(param); assert.ok(result); }); }); - describe("renderInput", () => { + describe("renderUDAInput", () => { it("should render input with valid input type", () => { const param: UDAParam = { name: "testParam", @@ -385,7 +385,7 @@ describe("KdbDataSourceView", () => { isReq: true, type: 1, }; - const result = view.renderInput(param, "number"); + const result = view.renderUDAInput(param, "number"); assert.ok(result); }); @@ -398,7 +398,7 @@ describe("KdbDataSourceView", () => { isReq: true, type: 1, }; - const result = view.renderInput(param, "invalid-type"); + const result = view.renderUDAInput(param, "invalid-type"); assert.ok(result); }); @@ -411,7 +411,7 @@ describe("KdbDataSourceView", () => { isReq: true, type: 1, }; - const result = view.renderInput(param, "text"); + const result = view.renderUDAInput(param, "text"); assert.ok(result); }); @@ -424,7 +424,7 @@ describe("KdbDataSourceView", () => { isReq: true, type: 1, }; - const result = view.renderInput(param, "text"); + const result = view.renderUDAInput(param, "text"); assert.ok(result); }); @@ -437,7 +437,7 @@ describe("KdbDataSourceView", () => { isReq: true, type: 1, }; - const result = view.renderInput(param, "text"); + const result = view.renderUDAInput(param, "text"); assert.ok(result); }); }); @@ -455,7 +455,7 @@ describe("KdbDataSourceView", () => { typeStrings: ["type1", "type2"], multiFieldTypes: [{ type1: ParamFieldType.Text }], }; - const result = view.renderMultitype(param); + const result = view.renderUDAMultitype(param); assert.ok(result); }); @@ -471,7 +471,7 @@ describe("KdbDataSourceView", () => { typeStrings: ["type1", "type2"], multiFieldTypes: [{ type1: ParamFieldType.Text }], }; - const result = view.renderMultitype(param); + const result = view.renderUDAMultitype(param); assert.ok(result); assert.strictEqual(param.selectedMultiTypeString, "type1"); }); @@ -488,7 +488,7 @@ describe("KdbDataSourceView", () => { typeStrings: undefined, multiFieldTypes: undefined, }; - const result = view.renderMultitype(param); + const result = view.renderUDAMultitype(param); assert.ok(result); assert.strictEqual(param.selectedMultiTypeString, ""); }); @@ -505,7 +505,7 @@ describe("KdbDataSourceView", () => { typeStrings: ["type1", "type2"], multiFieldTypes: [{ type1: ParamFieldType.Boolean }], }; - const result = view.renderMultitype(param); + const result = view.renderUDAMultitype(param); assert.ok(result); }); @@ -521,7 +521,7 @@ describe("KdbDataSourceView", () => { typeStrings: ["type1", "type2"], multiFieldTypes: [{ type1: ParamFieldType.JSON }], }; - const result = view.renderMultitype(param); + const result = view.renderUDAMultitype(param); assert.ok(result); }); }); From 58da9f196a28b89fde9e4a9f30b453ad8f073112 Mon Sep 17 00:00:00 2001 From: Philip Carneiro Date: Tue, 4 Mar 2025 11:24:26 +0000 Subject: [PATCH 4/5] improve the code and tests --- src/webview/components/kdbDataSourceView.ts | 8 +- test/suite/webview.test.ts | 145 +++++++++++++++++++- 2 files changed, 148 insertions(+), 5 deletions(-) diff --git a/src/webview/components/kdbDataSourceView.ts b/src/webview/components/kdbDataSourceView.ts index 15187226..126c0d69 100644 --- a/src/webview/components/kdbDataSourceView.ts +++ b/src/webview/components/kdbDataSourceView.ts @@ -1029,7 +1029,7 @@ export class KdbDataSourceView extends LitElement { return html` + @sl-select="${this.handleUDAAddParamSelect}"> + Add Parameter @@ -1038,7 +1038,7 @@ export class KdbDataSourceView extends LitElement { `; } - handleAddParamSelect(event: any) { + handleUDAAddParamSelect(event: any) { const paramSelected = event.detail.item.value; const param = this.userSelectedUDA?.params.find( (param) => param.name === paramSelected, @@ -1093,13 +1093,13 @@ export class KdbDataSourceView extends LitElement { + @click="${() => this.handleUDADeleteParam(param)}"> ${this.renderTrashIcon()} `; } - handleDeleteParam(param: UDAParam) { + handleUDADeleteParam(param: UDAParam) { param.isVisible = false; param.value = undefined; param.selectedMultiTypeString = undefined; diff --git a/test/suite/webview.test.ts b/test/suite/webview.test.ts index d0956c35..c465211c 100644 --- a/test/suite/webview.test.ts +++ b/test/suite/webview.test.ts @@ -442,7 +442,7 @@ describe("KdbDataSourceView", () => { }); }); - describe("renderMultitype", () => { + describe("renderUDAMultitype", () => { it("should render multitype with selectedMultiTypeString", () => { const param: UDAParam = { name: "testParam", @@ -550,6 +550,149 @@ describe("KdbDataSourceView", () => { assert.strictEqual(result, ""); }); }); + + describe("handleUDADeleteParam", () => { + it("should set param.isVisible to false", () => { + const param: UDAParam = { + name: "param", + type: 10, + description: "param description", + isReq: false, + isVisible: true, + }; + view.handleUDADeleteParam(param); + assert.strictEqual(param.isVisible, false); + }); + + it("should set param.value to undefined", () => { + const param: UDAParam = { + name: "param", + type: 10, + description: "param description", + isReq: false, + value: "some value", + }; + view.handleUDADeleteParam(param); + assert.strictEqual(param.value, undefined); + }); + + it("should set param.selectedMultiTypeString to undefined", () => { + const param: UDAParam = { + name: "param", + type: 10, + description: "param description", + isReq: false, + selectedMultiTypeString: "some string", + }; + view.handleUDADeleteParam(param); + assert.strictEqual(param.selectedMultiTypeString, undefined); + }); + + it("should call requestChange", () => { + const param: UDAParam = { + name: "param", + type: 10, + description: "param description", + isReq: false, + }; + const requestChangeSpy = sinon.spy(view, "requestChange"); + view.handleUDADeleteParam(param); + assert.strictEqual(requestChangeSpy.calledOnce, true); + }); + }); + + describe("handleAddParamSelect", () => { + it("should set param.isVisible to true if param is found", () => { + const param: UDAParam = { + name: "param", + type: 10, + description: "param description", + isReq: false, + isVisible: false, + }; + view.userSelectedUDA = { + name: "test", + description: "test description", + params: [param], + return: { + type: ["99"], + description: "test return description", + }, + }; + + const event = { + detail: { + item: { + value: "param", + }, + }, + }; + + view.handleUDAAddParamSelect(event); + assert.strictEqual(param.isVisible, true); + }); + + it("should not change param.isVisible if param is not found", () => { + const param: UDAParam = { + name: "param", + type: 10, + description: "param description", + isReq: false, + isVisible: false, + }; + view.userSelectedUDA = { + name: "test", + description: "test description", + params: [param], + return: { + type: ["99"], + description: "test return description", + }, + }; + + const event = { + detail: { + item: { + value: "nonexistent_param", + }, + }, + }; + + view.handleUDAAddParamSelect(event); + assert.strictEqual(param.isVisible, false); + }); + + it("should call requestChange", () => { + const param: UDAParam = { + name: "param", + type: 10, + description: "param description", + isReq: false, + isVisible: false, + }; + view.userSelectedUDA = { + name: "test", + description: "test description", + params: [param], + return: { + type: ["99"], + description: "test return description", + }, + }; + + const event = { + detail: { + item: { + value: "param", + }, + }, + }; + + const requestChangeSpy = sinon.spy(view, "requestChange"); + view.handleUDAAddParamSelect(event); + assert.strictEqual(requestChangeSpy.calledOnce, true); + }); + }); }); describe("save", () => { From ee656cb523c400a0b2c60cecf7409b3b0dff3713 Mon Sep 17 00:00:00 2001 From: Philip Carneiro Date: Tue, 4 Mar 2025 11:29:37 +0000 Subject: [PATCH 5/5] increase code coverage --- test/suite/webview.test.ts | 60 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/test/suite/webview.test.ts b/test/suite/webview.test.ts index c465211c..9c899902 100644 --- a/test/suite/webview.test.ts +++ b/test/suite/webview.test.ts @@ -693,6 +693,66 @@ describe("KdbDataSourceView", () => { assert.strictEqual(requestChangeSpy.calledOnce, true); }); }); + + describe("handleUDAChange", () => { + it("should update selectedUDA with the decoded value from the event", () => { + const event = { + target: { + value: encodeURIComponent("testUDA"), + }, + } as unknown as Event; + + view.handleUDAChange(event); + assert.strictEqual(view.selectedUDA, "testUDA"); + }); + + it("should update userSelectedUDA with the corresponding UDA from the list", () => { + const dummyUDA = { + name: "testUDA", + description: "test description", + params: [], + return: { + type: ["99"], + description: "test return description", + }, + }; + view.UDAs = [dummyUDA]; + + const event = { + target: { + value: encodeURIComponent("testUDA"), + }, + } as unknown as Event; + + view.handleUDAChange(event); + assert.deepStrictEqual(view.userSelectedUDA, dummyUDA); + }); + + it("should set userSelectedUDA to undefined if no matching UDA is found", () => { + view.UDAs = []; + + const event = { + target: { + value: encodeURIComponent("nonexistentUDA"), + }, + } as unknown as Event; + + view.handleUDAChange(event); + assert.strictEqual(view.userSelectedUDA, undefined); + }); + + it("should call requestChange", () => { + const event = { + target: { + value: encodeURIComponent("testUDA"), + }, + } as unknown as Event; + + const requestChangeSpy = sinon.spy(view, "requestChange"); + view.handleUDAChange(event); + assert.strictEqual(requestChangeSpy.calledOnce, true); + }); + }); }); describe("save", () => {