Skip to content

Commit

Permalink
Add coverage for additional branch of code
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew W. Harn <[email protected]>
  • Loading branch information
awharn committed Nov 14, 2024
1 parent 5455420 commit a41eb4b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3121,7 +3121,7 @@ describe("Dataset Actions Unit Tests - function search", () => {
jest.restoreAllMocks();
});

it("should show a message if cancellation was requested", async () => {
it("should show a message if cancellation was requested 1", async () => {
const tokenCancellation: vscode.CancellationToken = {
isCancellationRequested: true,
onCancellationRequested: jest.fn(),
Expand All @@ -3142,6 +3142,39 @@ describe("Dataset Actions Unit Tests - function search", () => {
expect(loggerErrorSpy).not.toHaveBeenCalled();
});

it("should show a message if cancellation was requested 2", async () => {
const tokenCancellation: vscode.CancellationToken = {
isCancellationRequested: false,
onCancellationRequested: jest.fn(),
};
const myProgress = { test: "test" };
const profile = createIProfile();
const node = createDatasetSessionNode(createISession(), profile);
const taskExpected = { percentComplete: 0, stageName: 0, statusMessage: "" };

searchDataSetsMock.mockImplementation((object) => {
object.progressTask.percentComplete = 0;
object.continueSearch([]);
return Promise.resolve({ success: false, commandResponse: "The search was cancelled." });
});

await (DatasetActions as any).performSearch(myProgress, tokenCancellation, { node, pattern: "TEST.*", searchString: "test" });

expect(getMvsApiSpy).toHaveBeenCalledTimes(1);
expect(showMessageSpy).not.toHaveBeenCalled();
expect(reportProgressSpy).toHaveBeenCalledWith(myProgress, 100, -1, "Percent Complete");
expect(searchDataSetsMock).toHaveBeenCalledWith({
pattern: "TEST.*",
searchString: "test",
progressTask: taskExpected,
mainframeSearch: false,
continueSearch: continueSearchPromptSpy,
});
expect(continueSearchPromptSpy).toHaveBeenCalledTimes(1);
expect(authErrorHandlingSpy).not.toHaveBeenCalled();
expect(loggerErrorSpy).not.toHaveBeenCalled();
});

it("should perform the search and succeed", async () => {
const myProgress = { test: "test" };
const taskExpected = { percentComplete: 51, stageName: 0, statusMessage: "" };
Expand Down Expand Up @@ -3769,6 +3802,42 @@ describe("Dataset Actions Unit Tests - function search", () => {
expect(tableBuilderTitleSpy).not.toHaveBeenCalled();
});

it("should fail to perform a search if the user responds no to the prompt", async () => {
const profile = createIProfile();
const node = createDatasetSessionNode(createISession(), profile);
node.pattern = "FAKE.*.DS";
const context = { context: "fake" } as any;
const searchString = "test";

const tokenCancellation: vscode.CancellationToken = {
isCancellationRequested: false,
onCancellationRequested: jest.fn(),
};
const myProgress = { test: "test" };

showInputBoxSpy.mockResolvedValue(searchString);
withProgressSpy.mockImplementation((opts: any, fn: any) => {
return fn(myProgress, tokenCancellation);
});
performSearchSpy.mockResolvedValue(undefined);

await DatasetActions.search(context, node);

expect(errorMessageSpy).not.toHaveBeenCalled();
expect(showInputBoxSpy).toHaveBeenCalledWith({ prompt: "Enter the text to search for." });
expect(showMessageSpy).not.toHaveBeenCalled();
expect(performSearchSpy).toHaveBeenCalledTimes(1);
expect(getSearchMatchesSpy).not.toHaveBeenCalled();
expect(tableViewProviderSpy).not.toHaveBeenCalled();
expect(openSearchAtLocationSpy).not.toHaveBeenCalled();
expect(withProgressSpy.mock.calls[0][0]).toEqual({
location: vscode.ProgressLocation.Notification,
title: 'Searching for "test"',
cancellable: true,
});
expect(tableBuilderTitleSpy).not.toHaveBeenCalled();
});

it("should attempt to perform the search (session node)", async () => {
const profile = createIProfile();
const node = createDatasetSessionNode(createISession(), profile);
Expand Down
10 changes: 10 additions & 0 deletions packages/zowe-explorer/src/trees/dataset/DatasetActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1802,6 +1802,11 @@ export class DatasetActions {
}
);

// Either the user cancelled the search, or a catestrophic error occurred and error handling has already been done.
if (response === undefined) {
return;
}

// Prepare a list of matches in a format the table expects
const matches = this.getSearchMatches(node, response, generateFullUri, searchString);

Expand Down Expand Up @@ -1942,6 +1947,11 @@ export class DatasetActions {
continueSearch: DatasetActions.continueSearchPrompt,
});

// The user cancelled the search
if (response.success === false && response.commandResponse?.includes("cancelled")) {
return;
}

// If there is no API response and success is false, the search didn't even begin searching data sets, and stopped during listing.
// Return an error to the user since we have no useful data. Otherwise, display partial results.
if (response.success === false && response.apiResponse == null) {
Expand Down

0 comments on commit a41eb4b

Please sign in to comment.