Reduce duplication in the test code

This commit is contained in:
Krzysztof Wolski 2022-09-01 16:43:28 +02:00
parent 10b3171b9d
commit b2f84b51c8
2 changed files with 33 additions and 89 deletions

View file

@ -3,6 +3,11 @@ import { afterEach, describe, expect, it, vi } from "vitest";
import { FileAPL, loadDataFromFile, saveDataToFile } from "./fileAPL"; import { FileAPL, loadDataFromFile, saveDataToFile } from "./fileAPL";
const stubAuthData = {
domain: "example.com",
token: "example-token",
};
describe("APL", () => { describe("APL", () => {
describe("FileAPL", () => { describe("FileAPL", () => {
describe("get", () => { describe("get", () => {
@ -11,11 +16,6 @@ describe("APL", () => {
}); });
it("Returns auth data for existing domain", async () => { it("Returns auth data for existing domain", async () => {
const stubAuthData = {
domain: "example.com",
token: "example-token",
};
vi.spyOn(fsPromises, "access").mockResolvedValue(); vi.spyOn(fsPromises, "access").mockResolvedValue();
vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData)); vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData));
@ -25,11 +25,6 @@ describe("APL", () => {
}); });
it("Returns undefined for unknown domain", async () => { it("Returns undefined for unknown domain", async () => {
const stubAuthData = {
domain: "example.com",
token: "example-token",
};
vi.spyOn(fsPromises, "access").mockResolvedValue(); vi.spyOn(fsPromises, "access").mockResolvedValue();
vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData)); vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData));
@ -45,11 +40,6 @@ describe("APL", () => {
}); });
it("Should override file when called with known domain", async () => { 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, "access").mockResolvedValue();
vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData)); vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData));
const spyWriteFile = vi.spyOn(fsPromises, "writeFile").mockResolvedValue(); const spyWriteFile = vi.spyOn(fsPromises, "writeFile").mockResolvedValue();
@ -62,11 +52,6 @@ describe("APL", () => {
}); });
it("Should not delete data when called with unknown domain", async () => { 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, "access").mockResolvedValue();
vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData)); 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 () => { 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, "access").mockResolvedValue();
vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData)); vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData));
@ -118,15 +98,9 @@ describe("APL", () => {
it("Save existing auth data to file", async () => { it("Save existing auth data to file", async () => {
const spyWriteFile = vi.spyOn(fsPromises, "writeFile").mockResolvedValue(); 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( expect(spyWriteFile).toBeCalledWith("test.json", JSON.stringify(stubAuthData));
"test.json",
JSON.stringify({
domain: "example.com",
token: "example-token",
})
);
}); });
it("Save empty file when no auth data provided", async () => { it("Save empty file when no auth data provided", async () => {
@ -151,10 +125,6 @@ describe("APL", () => {
it("Load existing auth data", async () => { it("Load existing auth data", async () => {
vi.spyOn(fsPromises, "access").mockResolvedValue(); vi.spyOn(fsPromises, "access").mockResolvedValue();
const stubAuthData = {
domain: "example.com",
token: "example-token",
};
vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData)); vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData));
expect(await loadDataFromFile("test.json")).toStrictEqual(stubAuthData); expect(await loadDataFromFile("test.json")).toStrictEqual(stubAuthData);
}); });
@ -167,19 +137,19 @@ describe("APL", () => {
it("Should return undefined when auth token is missing", async () => { it("Should return undefined when auth token is missing", async () => {
vi.spyOn(fsPromises, "access").mockResolvedValue(); vi.spyOn(fsPromises, "access").mockResolvedValue();
const stubAuthData = { const stubInvalidAuthData = {
domain: "example.com", 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); expect(await loadDataFromFile("test.json")).toBe(undefined);
}); });
it("Should return undefined when domain is missing", async () => { it("Should return undefined when domain is missing", async () => {
vi.spyOn(fsPromises, "access").mockResolvedValue(); vi.spyOn(fsPromises, "access").mockResolvedValue();
const stubAuthData = { const stubInvalidAuthData = {
token: "token", 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); expect(await loadDataFromFile("test.json")).toBe(undefined);
}); });
}); });

View file

@ -8,6 +8,16 @@ import {
VercelAPL, VercelAPL,
} from "./vercelAPL"; } from "./vercelAPL";
const aplConfig = {
deploymentToken: "token",
registerAppURL: "http://example.com",
};
const stubAuthData = {
domain: "example.com",
token: "example-token",
};
describe("APL", () => { describe("APL", () => {
describe("VercelAPL", () => { describe("VercelAPL", () => {
describe("constructor", () => { describe("constructor", () => {
@ -31,18 +41,12 @@ describe("APL", () => {
}); });
it("Constructor with config values", async () => { it("Constructor with config values", async () => {
expect( expect(() => new VercelAPL(aplConfig)).not.toThrow();
() =>
new VercelAPL({
deploymentToken: "token",
registerAppURL: "http://example.com",
})
).not.toThrow();
}); });
it("Constructor with config values from environment variables", async () => { it("Constructor with config values from environment variables", async () => {
process.env[SALEOR_REGISTER_APP_URL] = "http://example.com"; process.env[SALEOR_REGISTER_APP_URL] = aplConfig.registerAppURL;
process.env[SALEOR_DEPLOYMENT_TOKEN] = "token"; process.env[SALEOR_DEPLOYMENT_TOKEN] = aplConfig.deploymentToken;
expect(() => new VercelAPL()).not.toThrow(); expect(() => new VercelAPL()).not.toThrow();
}); });
@ -56,47 +60,28 @@ describe("APL", () => {
}); });
it("Read existing auth data", async () => { it("Read existing auth data", async () => {
const stubAuthData = {
domain: "example.com",
token: "example-token",
};
process.env[TOKEN_VARIABLE_NAME] = stubAuthData.token; process.env[TOKEN_VARIABLE_NAME] = stubAuthData.token;
process.env[DOMAIN_VARIABLE_NAME] = stubAuthData.domain; process.env[DOMAIN_VARIABLE_NAME] = stubAuthData.domain;
const apl = new VercelAPL({ const apl = new VercelAPL();
deploymentToken: "token",
registerAppURL: "http://example.com",
});
expect(await apl.get(stubAuthData.domain)).toStrictEqual(stubAuthData); expect(await apl.get(stubAuthData.domain)).toStrictEqual(stubAuthData);
}); });
it("Return undefined when unknown domain requested", async () => { 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[TOKEN_VARIABLE_NAME] = stubAuthData.token;
process.env[DOMAIN_VARIABLE_NAME] = stubAuthData.domain; process.env[DOMAIN_VARIABLE_NAME] = stubAuthData.domain;
const apl = new VercelAPL({ const apl = new VercelAPL(aplConfig);
deploymentToken: "token",
registerAppURL: "http://example.com",
});
expect(await apl.get("unknown-domain.example.com")).toBe(undefined); expect(await apl.get("unknown-domain.example.com")).toBe(undefined);
}); });
it("Return undefined when no data is defined", async () => { it("Return undefined when no data is defined", async () => {
process.env[TOKEN_VARIABLE_NAME] = undefined; delete process.env[TOKEN_VARIABLE_NAME];
process.env[DOMAIN_VARIABLE_NAME] = undefined; delete process.env[DOMAIN_VARIABLE_NAME];
const apl = new VercelAPL({ const apl = new VercelAPL(aplConfig);
deploymentToken: "token",
registerAppURL: "http://example.com",
});
expect(await apl.get("example.com")).toBe(undefined); expect(await apl.get("example.com")).toBe(undefined);
}); });
@ -112,30 +97,19 @@ describe("APL", () => {
}); });
it("Read existing auth data", async () => { it("Read existing auth data", async () => {
const stubAuthData = {
domain: "example.com",
token: "example-token",
};
process.env[TOKEN_VARIABLE_NAME] = stubAuthData.token; process.env[TOKEN_VARIABLE_NAME] = stubAuthData.token;
process.env[DOMAIN_VARIABLE_NAME] = stubAuthData.domain; process.env[DOMAIN_VARIABLE_NAME] = stubAuthData.domain;
const apl = new VercelAPL({ const apl = new VercelAPL(aplConfig);
deploymentToken: "token",
registerAppURL: "http://example.com",
});
expect(await apl.getAll()).toStrictEqual([stubAuthData]); expect(await apl.getAll()).toStrictEqual([stubAuthData]);
}); });
it("Return empty list when no auth data are existing", async () => { it("Return empty list when no auth data are existing", async () => {
process.env[TOKEN_VARIABLE_NAME] = undefined; delete process.env[TOKEN_VARIABLE_NAME];
process.env[DOMAIN_VARIABLE_NAME] = undefined; delete process.env[DOMAIN_VARIABLE_NAME];
const apl = new VercelAPL({ const apl = new VercelAPL(aplConfig);
deploymentToken: "token",
registerAppURL: "http://example.com",
});
expect(await apl.getAll()).toStrictEqual([]); expect(await apl.getAll()).toStrictEqual([]);
}); });