From a9a84074870b0c12681a58f898e2454a2ca4d064 Mon Sep 17 00:00:00 2001 From: Trae Yelovich Date: Thu, 28 Sep 2023 16:00:41 -0400 Subject: [PATCH] test(trees): Add test for refreshIconOnCollapse generated listeners Signed-off-by: Trae Yelovich --- .../__unit__/utils/TreeViewUtils.unit.test.ts | 15 ++++++++++++++ .../zowe-explorer/src/dataset/DatasetTree.ts | 10 +--------- .../zowe-explorer/src/job/ZosJobsProvider.ts | 10 +--------- packages/zowe-explorer/src/uss/USSTree.ts | 10 +--------- .../zowe-explorer/src/utils/TreeViewUtils.ts | 20 +++++++++++++++++++ 5 files changed, 38 insertions(+), 27 deletions(-) create mode 100644 packages/zowe-explorer/__tests__/__unit__/utils/TreeViewUtils.unit.test.ts diff --git a/packages/zowe-explorer/__tests__/__unit__/utils/TreeViewUtils.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/utils/TreeViewUtils.unit.test.ts new file mode 100644 index 0000000000..42bfb6c1ec --- /dev/null +++ b/packages/zowe-explorer/__tests__/__unit__/utils/TreeViewUtils.unit.test.ts @@ -0,0 +1,15 @@ +import { TreeViewUtils } from "../../../src/utils/TreeViewUtils"; +import * as globals from "../../../src/globals"; + +describe("TreeViewUtils Unit Tests", () => { + it("refreshIconOnCollapse - generated listener function works as intended", () => { + const testTreeProvider = { mOnDidChangeTreeData: { fire: jest.fn() } } as any; + const listenerFn = TreeViewUtils.refreshIconOnCollapse( + [(node): boolean => (node.contextValue as any).includes(globals.DS_PDS_CONTEXT) as boolean], + testTreeProvider + ); + const element = { label: "somenode", contextValue: globals.DS_PDS_CONTEXT } as any; + listenerFn({ element }); + expect(testTreeProvider.mOnDidChangeTreeData.fire).toHaveBeenCalledWith(element); + }); +}); diff --git a/packages/zowe-explorer/src/dataset/DatasetTree.ts b/packages/zowe-explorer/src/dataset/DatasetTree.ts index 9a18aecca6..04e71c832b 100644 --- a/packages/zowe-explorer/src/dataset/DatasetTree.ts +++ b/packages/zowe-explorer/src/dataset/DatasetTree.ts @@ -96,15 +96,7 @@ export class DatasetTree extends ZoweTreeProvider implements IZoweTree { - const newIcon = getIconByNode(e.element); - if (contextually.isPds(e.element) || contextually.isDsSession(e.element)) { - if (newIcon) { - e.element.iconPath = newIcon; - this.mOnDidChangeTreeData.fire(e.element); - } - } - }); + this.treeView.onDidCollapseElement(TreeViewUtils.refreshIconOnCollapse([contextually.isPds, contextually.isDsSession], this)); } /** diff --git a/packages/zowe-explorer/src/job/ZosJobsProvider.ts b/packages/zowe-explorer/src/job/ZosJobsProvider.ts index 06b4a65dcb..6ed92bd781 100644 --- a/packages/zowe-explorer/src/job/ZosJobsProvider.ts +++ b/packages/zowe-explorer/src/job/ZosJobsProvider.ts @@ -139,15 +139,7 @@ export class ZosJobsProvider extends ZoweTreeProvider implements IZoweTree { - const newIcon = getIconByNode(e.element); - if (contextually.isJob(e.element) || contextually.isJobsSession(e.element)) { - if (newIcon) { - e.element.iconPath = newIcon; - this.mOnDidChangeTreeData.fire(e.element); - } - } - }); + this.treeView.onDidCollapseElement(TreeViewUtils.refreshIconOnCollapse([contextually.isJob, contextually.isJobsSession], this)); } public rename(_node: IZoweJobTreeNode): void { diff --git a/packages/zowe-explorer/src/uss/USSTree.ts b/packages/zowe-explorer/src/uss/USSTree.ts index bc7ba90d16..d7bdc46ebe 100644 --- a/packages/zowe-explorer/src/uss/USSTree.ts +++ b/packages/zowe-explorer/src/uss/USSTree.ts @@ -80,15 +80,7 @@ export class USSTree extends ZoweTreeProvider implements IZoweTree { - const newIcon = getIconByNode(e.element); - if (contextually.isUssDirectory(e.element) || contextually.isUssSession(e.element)) { - if (newIcon) { - e.element.iconPath = newIcon; - this.mOnDidChangeTreeData.fire(e.element); - } - } - }); + this.treeView.onDidCollapseElement(TreeViewUtils.refreshIconOnCollapse([contextually.isUssDirectory, contextually.isUssSession], this)); } /** diff --git a/packages/zowe-explorer/src/utils/TreeViewUtils.ts b/packages/zowe-explorer/src/utils/TreeViewUtils.ts index a6d1f27df4..652e528fe5 100644 --- a/packages/zowe-explorer/src/utils/TreeViewUtils.ts +++ b/packages/zowe-explorer/src/utils/TreeViewUtils.ts @@ -11,6 +11,9 @@ import { IZoweNodeType, IZoweTree, IZoweTreeNode } from "@zowe/zowe-explorer-api"; import { ZoweLogger } from "./LoggerUtils"; +import { TreeViewExpansionEvent } from "vscode"; +import { getIconByNode } from "../generators/icons"; +import { ZoweTreeProvider } from "../abstract/ZoweTreeProvider"; export class TreeViewUtils { /** @@ -33,4 +36,21 @@ export class TreeViewUtils { ZoweLogger.trace("ZoweTreeProvider.expandNode called."); await provider.getTreeView().reveal(node, { expand: true }); } + + /** + * Builds an onDidCollapseElement event listener that will refresh node icons depending on the qualifiers given. + * If at least one node qualifier passes, it will refresh the icon for that node. + * @param qualifiers an array of boolean functions that take a tree node as a parameter + * @param treeProvider The tree provider that should update once the icons are changed + * @returns An event listener built to update the node icons based on the given qualifiers + */ + public static refreshIconOnCollapse(qualifiers: ((node: IZoweTreeNode) => boolean)[], treeProvider: ZoweTreeProvider) { + return (e: TreeViewExpansionEvent): any => { + const newIcon = getIconByNode(e.element); + if (qualifiers.some((q) => q(e.element)) && newIcon) { + e.element.iconPath = newIcon; + treeProvider.mOnDidChangeTreeData.fire(e.element); + } + }; + } }