aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2021-07-22 20:27:20 +0200
committerHampusM <hampus@hampusmat.com>2021-07-22 20:27:20 +0200
commit8520e2547b8ef2b10bfd92f554465e5ae14062e0 (patch)
treeef94f9c0f931f54e7a117e01c720b2968a285afa
parent73042c59c1adcdc527a82606b4be6dd873e81636 (diff)
Added unit tests for the misc functions
-rw-r--r--test/unit/misc.unit.test.ts77
1 files changed, 77 insertions, 0 deletions
diff --git a/test/unit/misc.unit.test.ts b/test/unit/misc.unit.test.ts
new file mode 100644
index 0000000..4e54ac4
--- /dev/null
+++ b/test/unit/misc.unit.test.ts
@@ -0,0 +1,77 @@
+import { findAsync, getDirectory, getFile } from "server/src/git/misc";
+import { BaseError } from "../../packages/server/src/git/error";
+import { EnvironmentVariables } from "../util";
+
+const env = process.env as EnvironmentVariables;
+
+describe("Miscellaneous functions", () => {
+ describe("FindAsync", () => {
+ const data: Promise<string>[] = [
+ new Promise((resolve) => {
+ resolve("hello!");
+ }),
+ new Promise((resolve) => {
+ resolve("Dad i'm hungry");
+ }),
+ new Promise((resolve) => {
+ resolve("Hi, hungry. I'm dad")
+ })
+ ];
+
+ it("Should find an existent item in an array", async () => {
+ expect.assertions(2);
+
+ const result = await findAsync(data, async (item) => {
+ return await item === "Dad i'm hungry";
+ });
+
+ expect(result).toBeDefined();
+ expect(result).toEqual("Dad i'm hungry");
+ });
+
+ it("Should fail to find a nonexistent item in an array", async () => {
+ expect.assertions(1);
+
+ const result = await findAsync(data, async (item) => {
+ return await item === "Hello there";
+ });
+
+ expect(result).toBeUndefined();
+ });
+ });
+
+ describe("getFile", () => {
+ it("Should return the content of a file in a bare git repository", async () => {
+ expect.assertions(2);
+
+ const content = await getFile(env.BASE_DIR, env.AVAIL_REPO, "description");
+
+ expect(content).toBeDefined();
+ expect(content).toEqual("Unnamed repository; edit this file 'description' to name the repository.");
+ });
+
+ it("Should fail to return the content of a nonexistent file in a bare repository", async () => {
+ expect.assertions(1);
+
+ await expect(getFile(env.BASE_DIR, env.AVAIL_REPO, "myselfasteem")).rejects.toBeInstanceOf(BaseError);
+ });
+ });
+
+ describe("getDirectory", () => {
+ it("Should return the content of a directory", async () => {
+ expect.assertions(3);
+
+ const dir = await getDirectory(`${env.BASE_DIR}/${env.AVAIL_REPO}/refs`);
+
+ expect(dir).toBeDefined();
+ expect(dir).toContain("heads");
+ expect(dir).toContain("tags");
+ });
+
+ it("Should fail to return the content of a nonexistent directory", async () => {
+ expect.assertions(1);
+
+ await expect(getDirectory(`${env.BASE_DIR}/${env.AVAIL_REPO}/something`)).rejects.toBeInstanceOf(BaseError);
+ });
+ });
+}); \ No newline at end of file