github.test.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { describe, expect, it, vi } from "vitest";
  2. import { retrieveLatestGitHubCommit } from "../../src/api/github";
  3. describe("retrieveLatestGitHubCommit", () => {
  4. const { githubGetMock } = vi.hoisted(() => ({
  5. githubGetMock: vi.fn(),
  6. }));
  7. vi.mock("../../src/api/github-axios-instance", () => ({
  8. github: {
  9. get: githubGetMock,
  10. },
  11. }));
  12. it("should return the latest commit when repository has commits", async () => {
  13. const mockCommit = {
  14. sha: "123abc",
  15. commit: {
  16. message: "Initial commit",
  17. },
  18. };
  19. githubGetMock.mockResolvedValueOnce({
  20. data: [mockCommit],
  21. });
  22. const result = await retrieveLatestGitHubCommit("user/repo");
  23. expect(result).toEqual(mockCommit);
  24. });
  25. it("should return null when repository is empty", async () => {
  26. const error = new Error("Repository is empty");
  27. (error as any).response = { status: 409 };
  28. githubGetMock.mockRejectedValueOnce(error);
  29. const result = await retrieveLatestGitHubCommit("user/empty-repo");
  30. expect(result).toBeNull();
  31. });
  32. it("should throw error for other error cases", async () => {
  33. const error = new Error("Network error");
  34. (error as any).response = { status: 500 };
  35. githubGetMock.mockRejectedValueOnce(error);
  36. await expect(retrieveLatestGitHubCommit("user/repo")).rejects.toThrow();
  37. });
  38. });