From 842af7afaf6e19b64e54da01d4d2df10ec1e8101 Mon Sep 17 00:00:00 2001 From: davelopez <46503462+davelopez@users.noreply.github.com> Date: Sun, 9 Jun 2024 17:11:37 +0200 Subject: [PATCH] Add test coverage for getAssociatedWorkflowUriFromTestsUri --- client/tests/unit/utils.test.ts | 57 +++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/client/tests/unit/utils.test.ts b/client/tests/unit/utils.test.ts index f3e3428..9728616 100644 --- a/client/tests/unit/utils.test.ts +++ b/client/tests/unit/utils.test.ts @@ -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({ size: file.length }); + } + throw new Error(`File not found: ${uri.path}`); + }, + }, }, - // Add other vscode namespaces and members you need to mock }), { virtual: true } ); @@ -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(); + }); + }); });