From e71330edd6db62d3623e7652ccc9d3ac6e3cc822 Mon Sep 17 00:00:00 2001 From: Jose Tovar Date: Tue, 28 Jan 2025 12:19:13 -0400 Subject: [PATCH] test: improve style with Given-When-Then --- utils/normalizeFilePath.test.js | 74 +++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 26 deletions(-) diff --git a/utils/normalizeFilePath.test.js b/utils/normalizeFilePath.test.js index 50cd1d4..746421c 100644 --- a/utils/normalizeFilePath.test.js +++ b/utils/normalizeFilePath.test.js @@ -7,41 +7,63 @@ describe("Normalize file path tests", () => { originalPlatform = process.platform; }); - afterAll(() => { + afterEach(() => { + // Restore original platform Object.defineProperty(process, "platform", { value: originalPlatform, }); }); - - it("should normalize file path for Windows", () => { - Object.defineProperty(process, "platform", { - value: "win32", + describe("Given a Windows Environment", () => { + beforeEach(() => { + // Simulate Windows environment + Object.defineProperty(process, "platform", { + value: "win32", + }); }); - const filePath = - "file:///C:/Users/pepe/Documents/jobus/blazer/misc/i18n-populator/test-configs/test-config.json"; - const expectedPath = - "C:/Users/pepe/Documents/jobus/blazer/misc/i18n-populator/test-configs"; - expect(normalizeFilePath(filePath)).toBe(expectedPath); - }); + describe("When file path is provided", () => { - it("should normalize file path for Unix", () => { - Object.defineProperty(process, "platform", { - value: "linux", + it("should normalize file path", () => { + const filePath = + "file:///C:/Users/pepe/Documents/jobus/blazer/misc/i18n-populator/test-configs/test-config.json"; + const expectedPath = + "C:/Users/pepe/Documents/jobus/blazer/misc/i18n-populator/test-configs"; + expect(normalizeFilePath(filePath)).toBe(expectedPath); + }); + }) + }) + + describe("Given a Unix-like system", () => { + beforeEach(() => { + // Simulate Unix environment + Object.defineProperty(process, "platform", { + value: "linux", + }); }); - const filePath = - "file://home/pepe/Documents/jobus/blazer/misc/i18n-populator/test-configs/test-config.json"; - const expectedPath = - "home/pepe/Documents/jobus/blazer/misc/i18n-populator/test-configs"; - expect(normalizeFilePath(filePath)).toBe(expectedPath); - }); + describe("When File Path is provided", () => { - it("should throw an error if no string is provided", () => { - expect(() => normalizeFilePath("")).toThrow("No string provided"); - }); + it("should normalize file path", () => { + const filePath = + "file://home/pepe/Documents/jobus/blazer/misc/i18n-populator/test-configs/test-config.json"; + const expectedPath = + "home/pepe/Documents/jobus/blazer/misc/i18n-populator/test-configs"; + expect(normalizeFilePath(filePath)).toBe(expectedPath); + }); + }) + }) - it("should handle undefined input gracefully", () => { - expect(() => normalizeFilePath(undefined)).toThrow("No string provided"); - }); + describe("Given any system", () => { + + describe("When File Path is not valid", () => { + + it("should throw an error if no string is provided", () => { + expect(() => normalizeFilePath("")).toThrow("No string provided"); + }); + + it("should handle undefined input gracefully", () => { + expect(() => normalizeFilePath(undefined)).toThrow("No string provided"); + }); + }) + }) });