Skip to content

Commit

Permalink
test(trees): Add test for refreshIconOnCollapse generated listeners
Browse files Browse the repository at this point in the history
Signed-off-by: Trae Yelovich <[email protected]>
  • Loading branch information
traeok committed Sep 28, 2023
1 parent d0586b7 commit a9a8407
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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);
});
});
10 changes: 1 addition & 9 deletions packages/zowe-explorer/src/dataset/DatasetTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,7 @@ export class DatasetTree extends ZoweTreeProvider implements IZoweTree<IZoweData
treeDataProvider: this,
canSelectMany: true,
});
this.treeView.onDidCollapseElement((e) => {
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));
}

/**
Expand Down
10 changes: 1 addition & 9 deletions packages/zowe-explorer/src/job/ZosJobsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,7 @@ export class ZosJobsProvider extends ZoweTreeProvider implements IZoweTree<IZowe
treeDataProvider: this,
canSelectMany: true,
});
this.treeView.onDidCollapseElement((e) => {
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 {
Expand Down
10 changes: 1 addition & 9 deletions packages/zowe-explorer/src/uss/USSTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,7 @@ export class USSTree extends ZoweTreeProvider implements IZoweTree<IZoweUSSTreeN
treeDataProvider: this,
canSelectMany: true,
});
this.treeView.onDidCollapseElement((e) => {
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));
}

/**
Expand Down
20 changes: 20 additions & 0 deletions packages/zowe-explorer/src/utils/TreeViewUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand All @@ -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<T extends IZoweTreeNode>(qualifiers: ((node: IZoweTreeNode) => boolean)[], treeProvider: ZoweTreeProvider) {
return (e: TreeViewExpansionEvent<T>): any => {
const newIcon = getIconByNode(e.element);
if (qualifiers.some((q) => q(e.element)) && newIcon) {
e.element.iconPath = newIcon;
treeProvider.mOnDidChangeTreeData.fire(e.element);
}
};
}
}

0 comments on commit a9a8407

Please sign in to comment.