-
Notifications
You must be signed in to change notification settings - Fork 0
🧪 Add test coverage for dashboard/year API endpoint #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { NextRequest } from "next/server"; | ||
|
|
||
| vi.mock("next-auth", () => ({ | ||
| getServerSession: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/auth", () => ({ | ||
| authOptions: {}, | ||
| })); | ||
|
|
||
| vi.mock("@/lib/githubViewer", () => ({ | ||
| fetchViewerLogin: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/githubYearInReview", () => ({ | ||
| fetchYearInReviewData: vi.fn(), | ||
| })); | ||
|
|
||
| describe("GET /api/dashboard/year", () => { | ||
| it("returns 401 if unauthorized", async () => { | ||
| const { getServerSession } = await import("next-auth"); | ||
| vi.mocked(getServerSession).mockResolvedValueOnce(null); | ||
|
|
||
| const { GET } = await import("./route"); | ||
| const req = new NextRequest("http://localhost/api/dashboard/year?year=2023"); | ||
|
|
||
| const response = await GET(req); | ||
| expect(response.status).toBe(401); | ||
|
|
||
| const data = await response.json(); | ||
| expect(data).toEqual({ error: "Unauthorized" }); | ||
| }); | ||
|
|
||
| it("returns 400 if year is invalid", async () => { | ||
| const { getServerSession } = await import("next-auth"); | ||
| vi.mocked(getServerSession).mockResolvedValueOnce({ | ||
| user: { login: "testuser" }, | ||
| accessToken: "testtoken", | ||
| expires: "9999-12-31T23:59:59.999Z", | ||
| }); | ||
|
Comment on lines
+37
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| const { GET } = await import("./route"); | ||
| const req = new NextRequest("http://localhost/api/dashboard/year?year=invalid"); | ||
|
|
||
| const response = await GET(req); | ||
| expect(response.status).toBe(400); | ||
|
|
||
| const data = await response.json(); | ||
| expect(data).toEqual({ error: "Invalid year" }); | ||
| }); | ||
|
|
||
| it("handles error path when fetching data fails", async () => { | ||
| const { getServerSession } = await import("next-auth"); | ||
| vi.mocked(getServerSession).mockResolvedValueOnce({ | ||
| user: { login: "testuser" }, | ||
| accessToken: "testtoken", | ||
| expires: "9999-12-31T23:59:59.999Z", | ||
| }); | ||
|
|
||
| const { fetchYearInReviewData } = await import("@/lib/githubYearInReview"); | ||
| vi.mocked(fetchYearInReviewData).mockRejectedValueOnce(new Error("API Error")); | ||
|
|
||
| const { GET } = await import("./route"); | ||
| const req = new NextRequest("http://localhost/api/dashboard/year?year=2023"); | ||
|
|
||
| const response = await GET(req); | ||
| expect(response.status).toBe(500); | ||
|
|
||
| const data = await response.json(); | ||
| expect(data).toEqual({ error: "API Error" }); | ||
| }); | ||
|
|
||
| it("returns 200 and data on success", async () => { | ||
| const { getServerSession } = await import("next-auth"); | ||
| vi.mocked(getServerSession).mockResolvedValueOnce({ | ||
| user: { login: "testuser" }, | ||
| accessToken: "testtoken", | ||
| expires: "9999-12-31T23:59:59.999Z", | ||
| }); | ||
|
|
||
| const { fetchYearInReviewData } = await import("@/lib/githubYearInReview"); | ||
| const mockData = { totalContributions: 1000 }; | ||
| vi.mocked(fetchYearInReviewData).mockResolvedValueOnce(mockData as unknown as Awaited<ReturnType<typeof fetchYearInReviewData>>); | ||
|
|
||
| const { GET } = await import("./route"); | ||
| const req = new NextRequest("http://localhost/api/dashboard/year?year=2023"); | ||
|
|
||
| const response = await GET(req); | ||
| expect(response.status).toBe(200); | ||
|
|
||
| const data = await response.json(); | ||
| expect(data).toEqual(mockData); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test suite is a great start, but it doesn't cover all execution paths in the
GEThandler. The PR description claims 'full coverage', but some important cases are missed. Here are a few scenarios that should also be tested:accessToken. This should also result in a 401.yearquery parameter is not provided, causing the year to default to the current year.'invalid').session.user.loginis missing andfetchViewerLoginis called to get the username.Adding tests for these cases would make the test suite truly comprehensive.