aboutsummaryrefslogtreecommitdiff
path: root/test/unit/misc.unit.test.ts
blob: d3d793a8e1a6669b1409c0338fb1c55a96c8f299 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { findAsync, getDirectory, getFile } from "server/src/git/misc";
import { ServerError } from "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.GIT_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.GIT_DIR, env.AVAIL_REPO, "myselfasteem")).rejects.toBeInstanceOf(ServerError);
		});
	});

	describe("getDirectory", () => {
		it("Should return the content of a directory", async() => {
			expect.assertions(3);

			const dir = await getDirectory(`${env.GIT_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.GIT_DIR}/${env.AVAIL_REPO}/something`)).rejects.toBeInstanceOf(ServerError);
		});
	});
});