From b2f84b51c823f835087f83c904ca1f1ca02a5447 Mon Sep 17 00:00:00 2001 From: Krzysztof Wolski Date: Thu, 1 Sep 2022 16:43:28 +0200 Subject: [PATCH] Reduce duplication in the test code --- src/APL/fileAPL.test.ts | 52 ++++++----------------------- src/APL/vercelAPL.test.ts | 70 ++++++++++++--------------------------- 2 files changed, 33 insertions(+), 89 deletions(-) diff --git a/src/APL/fileAPL.test.ts b/src/APL/fileAPL.test.ts index c1758c8..9558f15 100644 --- a/src/APL/fileAPL.test.ts +++ b/src/APL/fileAPL.test.ts @@ -3,6 +3,11 @@ import { afterEach, describe, expect, it, vi } from "vitest"; import { FileAPL, loadDataFromFile, saveDataToFile } from "./fileAPL"; +const stubAuthData = { + domain: "example.com", + token: "example-token", +}; + describe("APL", () => { describe("FileAPL", () => { describe("get", () => { @@ -11,11 +16,6 @@ describe("APL", () => { }); it("Returns auth data for existing domain", async () => { - const stubAuthData = { - domain: "example.com", - token: "example-token", - }; - vi.spyOn(fsPromises, "access").mockResolvedValue(); vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData)); @@ -25,11 +25,6 @@ describe("APL", () => { }); it("Returns undefined for unknown domain", async () => { - const stubAuthData = { - domain: "example.com", - token: "example-token", - }; - vi.spyOn(fsPromises, "access").mockResolvedValue(); vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData)); @@ -45,11 +40,6 @@ describe("APL", () => { }); it("Should override file when called with known domain", async () => { - const stubAuthData = { - domain: "example.com", - token: "example-token", - }; - vi.spyOn(fsPromises, "access").mockResolvedValue(); vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData)); const spyWriteFile = vi.spyOn(fsPromises, "writeFile").mockResolvedValue(); @@ -62,11 +52,6 @@ describe("APL", () => { }); it("Should not delete data when called with unknown domain", async () => { - const stubAuthData = { - domain: "example.com", - token: "example-token", - }; - vi.spyOn(fsPromises, "access").mockResolvedValue(); vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData)); @@ -86,11 +71,6 @@ describe("APL", () => { }); it("Should return list with one item when auth data are existing", async () => { - const stubAuthData = { - domain: "example.com", - token: "example-token", - }; - vi.spyOn(fsPromises, "access").mockResolvedValue(); vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData)); @@ -118,15 +98,9 @@ describe("APL", () => { it("Save existing auth data to file", async () => { const spyWriteFile = vi.spyOn(fsPromises, "writeFile").mockResolvedValue(); - await saveDataToFile("test.json", { domain: "example.com", token: "example-token" }); + await saveDataToFile("test.json", stubAuthData); - expect(spyWriteFile).toBeCalledWith( - "test.json", - JSON.stringify({ - domain: "example.com", - token: "example-token", - }) - ); + expect(spyWriteFile).toBeCalledWith("test.json", JSON.stringify(stubAuthData)); }); it("Save empty file when no auth data provided", async () => { @@ -151,10 +125,6 @@ describe("APL", () => { it("Load existing auth data", async () => { vi.spyOn(fsPromises, "access").mockResolvedValue(); - const stubAuthData = { - domain: "example.com", - token: "example-token", - }; vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData)); expect(await loadDataFromFile("test.json")).toStrictEqual(stubAuthData); }); @@ -167,19 +137,19 @@ describe("APL", () => { it("Should return undefined when auth token is missing", async () => { vi.spyOn(fsPromises, "access").mockResolvedValue(); - const stubAuthData = { + const stubInvalidAuthData = { domain: "example.com", }; - vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData)); + vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubInvalidAuthData)); expect(await loadDataFromFile("test.json")).toBe(undefined); }); it("Should return undefined when domain is missing", async () => { vi.spyOn(fsPromises, "access").mockResolvedValue(); - const stubAuthData = { + const stubInvalidAuthData = { token: "token", }; - vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData)); + vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubInvalidAuthData)); expect(await loadDataFromFile("test.json")).toBe(undefined); }); }); diff --git a/src/APL/vercelAPL.test.ts b/src/APL/vercelAPL.test.ts index b4b3386..320e15d 100644 --- a/src/APL/vercelAPL.test.ts +++ b/src/APL/vercelAPL.test.ts @@ -8,6 +8,16 @@ import { VercelAPL, } from "./vercelAPL"; +const aplConfig = { + deploymentToken: "token", + registerAppURL: "http://example.com", +}; + +const stubAuthData = { + domain: "example.com", + token: "example-token", +}; + describe("APL", () => { describe("VercelAPL", () => { describe("constructor", () => { @@ -31,18 +41,12 @@ describe("APL", () => { }); it("Constructor with config values", async () => { - expect( - () => - new VercelAPL({ - deploymentToken: "token", - registerAppURL: "http://example.com", - }) - ).not.toThrow(); + expect(() => new VercelAPL(aplConfig)).not.toThrow(); }); it("Constructor with config values from environment variables", async () => { - process.env[SALEOR_REGISTER_APP_URL] = "http://example.com"; - process.env[SALEOR_DEPLOYMENT_TOKEN] = "token"; + process.env[SALEOR_REGISTER_APP_URL] = aplConfig.registerAppURL; + process.env[SALEOR_DEPLOYMENT_TOKEN] = aplConfig.deploymentToken; expect(() => new VercelAPL()).not.toThrow(); }); @@ -56,47 +60,28 @@ describe("APL", () => { }); it("Read existing auth data", async () => { - const stubAuthData = { - domain: "example.com", - token: "example-token", - }; - process.env[TOKEN_VARIABLE_NAME] = stubAuthData.token; process.env[DOMAIN_VARIABLE_NAME] = stubAuthData.domain; - const apl = new VercelAPL({ - deploymentToken: "token", - registerAppURL: "http://example.com", - }); + const apl = new VercelAPL(); expect(await apl.get(stubAuthData.domain)).toStrictEqual(stubAuthData); }); it("Return undefined when unknown domain requested", async () => { - const stubAuthData = { - domain: "example.com", - token: "example-token", - }; - process.env[TOKEN_VARIABLE_NAME] = stubAuthData.token; process.env[DOMAIN_VARIABLE_NAME] = stubAuthData.domain; - const apl = new VercelAPL({ - deploymentToken: "token", - registerAppURL: "http://example.com", - }); + const apl = new VercelAPL(aplConfig); expect(await apl.get("unknown-domain.example.com")).toBe(undefined); }); it("Return undefined when no data is defined", async () => { - process.env[TOKEN_VARIABLE_NAME] = undefined; - process.env[DOMAIN_VARIABLE_NAME] = undefined; + delete process.env[TOKEN_VARIABLE_NAME]; + delete process.env[DOMAIN_VARIABLE_NAME]; - const apl = new VercelAPL({ - deploymentToken: "token", - registerAppURL: "http://example.com", - }); + const apl = new VercelAPL(aplConfig); expect(await apl.get("example.com")).toBe(undefined); }); @@ -112,30 +97,19 @@ describe("APL", () => { }); it("Read existing auth data", async () => { - const stubAuthData = { - domain: "example.com", - token: "example-token", - }; - process.env[TOKEN_VARIABLE_NAME] = stubAuthData.token; process.env[DOMAIN_VARIABLE_NAME] = stubAuthData.domain; - const apl = new VercelAPL({ - deploymentToken: "token", - registerAppURL: "http://example.com", - }); + const apl = new VercelAPL(aplConfig); expect(await apl.getAll()).toStrictEqual([stubAuthData]); }); it("Return empty list when no auth data are existing", async () => { - process.env[TOKEN_VARIABLE_NAME] = undefined; - process.env[DOMAIN_VARIABLE_NAME] = undefined; + delete process.env[TOKEN_VARIABLE_NAME]; + delete process.env[DOMAIN_VARIABLE_NAME]; - const apl = new VercelAPL({ - deploymentToken: "token", - registerAppURL: "http://example.com", - }); + const apl = new VercelAPL(aplConfig); expect(await apl.getAll()).toStrictEqual([]); });