Skip to content

Commit

Permalink
Add test coverage for getAssociatedWorkflowUriFromTestsUri
Browse files Browse the repository at this point in the history
  • Loading branch information
davelopez committed Jun 9, 2024
1 parent 42d6114 commit 842af7a
Showing 1 changed file with 54 additions and 3 deletions.
57 changes: 54 additions & 3 deletions client/tests/unit/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
import { it } from "@jest/globals"; // This is a workaround for type clashes between jest and mocha
import { URI } from "vscode-uri";
import { isFormat2WorkflowDocument, isNativeWorkflowDocument, isWorkflowTestsDocument } from "../../src/common/utils";
import {
getAssociatedWorkflowUriFromTestsUri,
isFormat2WorkflowDocument,
isNativeWorkflowDocument,
isWorkflowTestsDocument,
} from "../../src/common/utils";

// Partially mimics vscode FileStat interface
interface FileStat {
size: number;
}

const FILES_IN_WORKSPACE = ["workflow1.ga", "workflow2.gxwf.yaml", "workflow3.gxwf.yml"];

jest.mock(
"vscode",
() => ({
workspace: {
// Mock properties and methods of `workspace` as needed for your tests
fs: {
stat: (uri: URI) => {
const file = FILES_IN_WORKSPACE.find((f) => URI.parse(f).path === uri.path);
if (file) {
return Promise.resolve<FileStat>({ size: file.length });
}
throw new Error(`File not found: ${uri.path}`);
},
},
},
// Add other vscode namespaces and members you need to mock
}),
{ virtual: true }
);
Expand Down Expand Up @@ -59,4 +78,36 @@ describe("Common Utils", () => {
expect(isFormat2WorkflowDocument(URI.parse(input))).toBe(expected);
});
});

describe("getAssociatedWorkflowUriFromTestsUri", () => {
it("should return undefined if the input URI is not a workflow tests document", async () => {
const uri = URI.parse("test.txt");
const result = await getAssociatedWorkflowUriFromTestsUri(uri);
expect(result).toBeUndefined();
});

it("should return the associated (.ga) workflow document URI if it exists in workspace", async () => {
const uri = URI.parse("workflow1-test.yaml");
const result = await getAssociatedWorkflowUriFromTestsUri(uri);
expect(result?.path.endsWith("workflow1.ga")).toBe(true);
});

it("should return the associated (yaml) workflow document URI if it exists in workspace", async () => {
const uri = URI.parse("workflow2-test.yaml");
const result = await getAssociatedWorkflowUriFromTestsUri(uri);
expect(result?.path.endsWith("workflow2.gxwf.yaml")).toBe(true);
});

it("should return the associated (yml) workflow document URI if it exists in workspace", async () => {
const uri = URI.parse("workflow3-tests.yaml");
const result = await getAssociatedWorkflowUriFromTestsUri(uri);
expect(result?.path.endsWith("workflow3.gxwf.yml")).toBe(true);
});

it("should return undefined if the associated workflow document does not exist in workspace", async () => {
const uri = URI.parse("nonexistent-test.yaml");
const result = await getAssociatedWorkflowUriFromTestsUri(uri);
expect(result).toBeUndefined();
});
});
});

0 comments on commit 842af7a

Please sign in to comment.