Skip to content

Commit

Permalink
another atempt to increase test coverage
Browse files Browse the repository at this point in the history
- also included a `return` with error if  `documentSession` is not found. This should be a show stopper
- fix bug where `sesNode`  was not defined but trying to extract the nodes

Signed-off-by: Alexandru-Paul Dumitru <alexandru.dumitru@broadcom.com>
  • Loading branch information
Alexandru-Paul Dumitru committed Dec 4, 2019
1 parent c3e7227 commit 6b94c23
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
50 changes: 49 additions & 1 deletion __tests__/__unit__/extension.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ describe("Extension Unit Tests", () => {
};
})
});

Object.defineProperty(utils, "concatChildNodes", {value: concatChildNodes});
Object.defineProperty(utils, "concatUSSChildNodes", {value: concatUSSChildNodes});
Object.defineProperty(fs, "mkdirSync", {value: mkdirSync});
Expand Down Expand Up @@ -1537,6 +1536,25 @@ describe("Extension Unit Tests", () => {
validateRange: null,
validatePosition: null
};
const testDoc0: vscode.TextDocument = {
fileName: path.join(extension.DS_DIR, "HLQ.TEST.AFILE"),
uri: null,
isUntitled: null,
languageId: null,
version: null,
isDirty: null,
isClosed: null,
save: null,
eol: null,
lineCount: null,
lineAt: null,
offsetAt: null,
positionAt: null,
getText: null,
getWordRangeAtPosition: null,
validateRange: null,
validatePosition: null
};

const testResponse = {
success: true,
Expand All @@ -1545,6 +1563,36 @@ describe("Extension Unit Tests", () => {
items: []
}
};
// If session node is not defined, it should take the session from Profile
const sessionwocred = new brtimperative.Session({
user: "",
password: "",
hostname: "fake",
protocol: "https",
type: "basic",
});
// testing if no session is defined (can happen while saving from favorites)
const nodeWitoutSession = new ZoweNode("HLQ.TEST.AFILE", vscode.TreeItemCollapsibleState.None, null, null);
testTree.getChildren.mockReturnValueOnce([nodeWitoutSession]);
concatChildNodes.mockReturnValueOnce([nodeWitoutSession]);
createBasicZosmfSession.mockReturnValueOnce(sessionwocred);
await extension.saveFile(testDoc0, testTree);
expect(createBasicZosmfSession.mock.calls.length).toBe(1);
expect(createBasicZosmfSession.mock.results[0].value).toEqual(sessionwocred);

// testing if no documentSession is found (no session + no profile)
createBasicZosmfSession.mockReset();
testTree.getChildren.mockReset();
showErrorMessage.mockReset();
testTree.getChildren.mockReturnValueOnce([nodeWitoutSession]);
createBasicZosmfSession.mockReturnValueOnce(null);
await extension.saveFile(testDoc0, testTree);
expect(showErrorMessage.mock.calls.length).toBe(1);
expect(showErrorMessage.mock.calls[0][0]).toBe("Couldn't locate session when saving data set!");

testTree.getChildren.mockReset();
createBasicZosmfSession.mockReset();

testTree.getChildren.mockReturnValueOnce([new ZoweNode("node", vscode.TreeItemCollapsibleState.None, sessNode, null), sessNode]);
dataSetList.mockReset();
showErrorMessage.mockReset();
Expand Down
5 changes: 3 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,7 @@ export async function saveFile(doc: vscode.TextDocument, datasetProvider: Datase
}
if (documentSession == null) {
log.error(localize("saveFile.log.error.session", "Couldn't locate session when saving data set!"));
return vscode.window.showErrorMessage(localize("saveFile.log.error.session", "Couldn't locate session when saving data set!"));
}
// If not a member
const label = doc.fileName.substring(doc.fileName.lastIndexOf(path.sep) + 1,
Expand All @@ -1677,7 +1678,7 @@ export async function saveFile(doc: vscode.TextDocument, datasetProvider: Datase
// Get specific node based on label and parent tree (session / favorites)
let nodes: ZoweNode[];
let isFromFavorites: boolean;
if (sesNode.children.length === 0) {
if (!sesNode || sesNode.children.length === 0) {
// saving from favorites
nodes = utils.concatChildNodes(datasetProvider.mFavorites);
isFromFavorites = true;
Expand Down Expand Up @@ -1779,7 +1780,7 @@ export async function saveUSSFile(doc: vscode.TextDocument, ussFileProvider: USS
}
// Get specific node based on label and parent tree (session / favorites)
let nodes: ZoweUSSNode[];
if (sesNode.children.length === 0) {
if (!sesNode || sesNode.children.length === 0) {
// saving from favorites
nodes = utils.concatUSSChildNodes(ussFileProvider.mFavorites);
} else {
Expand Down

0 comments on commit 6b94c23

Please sign in to comment.