diff options
author | HampusM <hampus@hampusmat.com> | 2021-07-12 15:27:24 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2021-07-12 15:27:24 +0200 |
commit | 29ac9af81d94f7de298d98d9597c73ddd1c93bb0 (patch) | |
tree | e12208712ce99085783b2c53db691009823b4c3f /test/unit/repository.unit.test.ts | |
parent | a4c70c283c0a77e123694fb2d3687f2efff3eef7 (diff) |
Improved the unit tests
Diffstat (limited to 'test/unit/repository.unit.test.ts')
-rw-r--r-- | test/unit/repository.unit.test.ts | 137 |
1 files changed, 96 insertions, 41 deletions
diff --git a/test/unit/repository.unit.test.ts b/test/unit/repository.unit.test.ts index 2a68a29..86ac55e 100644 --- a/test/unit/repository.unit.test.ts +++ b/test/unit/repository.unit.test.ts @@ -1,83 +1,138 @@ import { Repository } from "server/src/git/repository"; +import { Commit } from "server/src/git/commit"; +import { Tree } from "server/src/git/tree"; +import { Branch } from "server/src/git/branch"; +import { Tag } from "server/src/git/tag"; +import { EnvironmentVariables, expectCommitProperties } from "../util"; + +const env = process.env as EnvironmentVariables; + +function expectRepositoryProperties(repository: Repository) { + expect(repository).toHaveProperty("base_dir"); + expect(repository).toHaveProperty("description"); + expect(repository).toHaveProperty("name"); + expect(repository).toHaveProperty("name.full"); + expect(repository).toHaveProperty("name.short"); + expect(repository).toHaveProperty("owner"); +} describe("Repository", () => { - test("Open existing repository", async () => { - const openRepository = jest.fn(() => Repository.open(process.env.BASE_DIR, process.env.AVAIL_REPO)); + it("Opens a repository successfully", async () => { + expect.assertions(8); - await openRepository(); - - expect(openRepository).toReturn(); + const repository = await Repository.open(process.env.BASE_DIR, env.AVAIL_REPO); + + expect(repository).toBeDefined(); + expect(repository).toBeInstanceOf(Repository); + + expectRepositoryProperties(repository); }); - test("Open nonexistant repository throws", async () => { - expect(Repository.open(process.env.BASE_DIR, process.env.UNAVAIL_REPO)).rejects.toThrow(); + it("Fails to open a nonexistant repository", async () => { + expect.assertions(1); + + await expect(Repository.open(process.env.BASE_DIR, env.UNAVAIL_REPO)).rejects.toThrow(); }); - test("Open all repositories", async () => { - const openAllRepositories = jest.fn(() => Repository.openAll(process.env.BASE_DIR)); + it("Opens all repositories", async () => { + expect.hasAssertions(); + + const all_repositories = await Repository.openAll(env.BASE_DIR); - await openAllRepositories(); + expect(all_repositories).toBeDefined(); - expect(openAllRepositories).toReturn(); + for(const repository of all_repositories) { + expect(repository).toBeDefined(); + expect(repository).toBeInstanceOf(Repository) + + expectRepositoryProperties(repository); + } }); - describe("Functions", () => { + describe("Methods", () => { let repository: Repository; beforeAll(async () => { - repository = await Repository.open(process.env.BASE_DIR, process.env.AVAIL_REPO); + repository = await Repository.open(process.env.BASE_DIR, env.AVAIL_REPO); }); - test("Lookup if an existing object exists", async () => { - const exists = await repository.lookupExists(process.env.AVAIL_OBJECT); + it("Looks up if an object that exists exist", async () => { + expect.assertions(1); - expect(exists).toBeTruthy(); + await expect(repository.lookupExists(env.AVAIL_OBJECT)).resolves.toBeTruthy(); }); - test("Lookup if an nonexistant object exists", async () => { - const exists = await repository.lookupExists(process.env.UNAVAIL_OBJECT); + it("Looks up if an nonexistant object exists", async () => { + expect.assertions(1); - expect(exists).toBeFalsy(); + await expect(repository.lookupExists(env.UNAVAIL_OBJECT)).resolves.toBeFalsy(); }); - test("Get latest commit", async () => { - const getLatestCommit = jest.fn(() => repository.latestCommit()); + it("Gets the latest commit", async () => { + expect.assertions(8); + + const latest_commit = await repository.latestCommit(); + + expect(latest_commit).toBeDefined(); + expect(latest_commit).toBeInstanceOf(Commit); - await getLatestCommit(); - - expect(getLatestCommit).toReturn(); + expectCommitProperties(latest_commit); }); - test("Get commits", async () => { - const getCommits = jest.fn(() => repository.commits()); + it("Gets the commits", async () => { + expect.hasAssertions(); - await getCommits(); + const commits = await repository.commits(); - expect(getCommits).toReturn(); + expect(commits).toBeDefined(); + + for(const commit of commits) { + expect(commit).toBeDefined(); + expect(commit).toBeInstanceOf(Commit); + + expectCommitProperties(commit); + } }); - test("Get tree", async () => { - const getTree = jest.fn(() => repository.tree()); + it("Gets the tree", async () => { + expect.assertions(2); - await getTree(); + const tree = await repository.tree(); - expect(getTree).toReturn(); + expect(tree).toBeDefined(); + expect(tree).toBeInstanceOf(Tree); }); - test("Get branches", async () => { - const getBranches = jest.fn(() => repository.branches()); + it("Gets the branches", async () => { + expect.hasAssertions(); + + const branches = await repository.branches(); + + expect(branches).toBeDefined(); - await getBranches(); + for(const branch of branches) { + expect(branch).toBeDefined(); + expect(branch).toBeInstanceOf(Branch); - expect(getBranches).toReturn(); + expect(branch).toHaveProperty("id"); + expect(branch).toHaveProperty("name"); + } }); - test("Get tags", async () => { - const getTags = jest.fn(() => repository.tags()); + it("Gets the tags", async () => { + expect.hasAssertions(); + + const tags = await repository.tags(); + + expect(tags).toBeDefined(); + + for(const tag of tags) { + expect(tag).toBeDefined(); + expect(tag).toBeInstanceOf(Tag); - await getTags(); - - expect(getTags).toReturn(); + expect(tag).toHaveProperty("id"); + expect(tag).toHaveProperty("name"); + } }); }); });
\ No newline at end of file |