-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from Firebird1029/more-tests
Added more tests
- Loading branch information
Showing
11 changed files
with
293 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,130 @@ | ||
import { render, screen, fireEvent, waitFor } from "@testing-library/react"; | ||
import { useRouter } from "next/navigation"; | ||
import createClient from "@/utils/supabase/client"; | ||
import IndexPage from "../src/app/page"; | ||
import loginWithSpotify from "@/app/login/actions"; | ||
|
||
// Dependency mocks | ||
jest.mock("next/navigation", () => ({ | ||
useRouter: jest.fn(), | ||
})); | ||
|
||
jest.mock("../src/utils/supabase/client", () => ({ | ||
__esModule: true, | ||
default: jest.fn(), | ||
})); | ||
|
||
jest.mock("../src/app/login/actions", () => ({ | ||
__esModule: true, | ||
default: jest.fn(), | ||
})); | ||
|
||
describe("IndexPage", () => { | ||
beforeEach(() => { | ||
useRouter.mockReturnValue({ | ||
push: jest.fn(), | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("renders loading state when loggedIn is undefined", () => { | ||
render(<IndexPage />); | ||
expect(screen.getByText("Getting things set up...")).toBeInTheDocument(); | ||
}); | ||
|
||
test("redirects to error page when supabase.auth.getUser fails", async () => { | ||
const mockSupabase = { | ||
auth: { | ||
getUser: jest.fn().mockRejectedValue(new Error("Supabase error")), | ||
}, | ||
}; | ||
createClient.mockReturnValue(mockSupabase); | ||
|
||
render(<IndexPage />); | ||
|
||
await waitFor(() => { | ||
expect(useRouter().push).toHaveBeenCalledWith("/error"); | ||
}); | ||
}); | ||
|
||
test("sets loggedIn to true when user is already logged in", async () => { | ||
const mockSupabase = { | ||
auth: { | ||
getUser: jest.fn().mockResolvedValue({ data: { user: { id: 1 } } }), | ||
}, | ||
}; | ||
createClient.mockReturnValue(mockSupabase); | ||
|
||
render(<IndexPage />); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText("Continue to Account")).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
test("redirects to error page on sign-out error", async () => { | ||
global.fetch = jest.fn().mockRejectedValue(new Error("Network error")); | ||
|
||
render(<IndexPage />); | ||
|
||
fireEvent.click(screen.getByText("Logout")); | ||
|
||
await waitFor(() => { | ||
expect(useRouter().push).toHaveBeenCalledWith( | ||
"/error?message=Network error", | ||
); | ||
}); | ||
}); | ||
|
||
test("redirects to the home page on successful sign-out", async () => { | ||
global.fetch = jest.fn().mockResolvedValue({ ok: true }); | ||
Object.defineProperty(window, "location", { | ||
value: { | ||
href: "", | ||
}, | ||
writable: true, | ||
}); | ||
|
||
render(<IndexPage />); | ||
fireEvent.click(screen.getByText("Logout")); | ||
|
||
await waitFor(() => { | ||
expect(window.location.href).toBe("/"); | ||
}); | ||
}); | ||
|
||
test("tests loginWithSpotify on button click", async () => { | ||
// Ensures user is not logged in initially | ||
const mockSupabase = { | ||
auth: { | ||
getUser: jest.fn().mockResolvedValue({ data: { user: null } }), | ||
}, | ||
}; | ||
createClient.mockReturnValue(mockSupabase); | ||
|
||
render(<IndexPage />); | ||
|
||
fireEvent.click(screen.getByText("Log in with Spotify")); | ||
expect(loginWithSpotify).toHaveBeenCalled(); | ||
}); | ||
|
||
test("redirects to error page on sign-out failure", async () => { | ||
global.fetch = jest.fn().mockResolvedValue({ | ||
ok: false, | ||
statusText: "Forbidden", | ||
}); | ||
|
||
const router = useRouter(); | ||
render(<IndexPage />); | ||
|
||
fireEvent.click(screen.getByText("Logout")); | ||
|
||
// Wait for the async actions and effects to complete, then check for error | ||
await waitFor(() => { | ||
expect(router.push).toHaveBeenCalledWith("/error?message=Forbidden"); | ||
}); | ||
}); | ||
}); |
This file contains 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
This file contains 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
This file contains 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,13 @@ | ||
import React from "react"; | ||
import { render } from "@testing-library/react"; | ||
import RootLayout from "../src/app/layout"; | ||
|
||
describe("RootLayout", () => { | ||
test("RootLayout test", () => { | ||
render( | ||
<RootLayout> | ||
<div>Test</div> | ||
</RootLayout>, | ||
); | ||
}); | ||
}); |
This file contains 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,44 @@ | ||
import { middleware, config } from "../src/middleware"; | ||
import updateSession from "@/utils/supabase/middleware"; | ||
|
||
jest.mock("../src/utils/supabase/middleware", () => ({ | ||
__esModule: true, | ||
default: jest.fn(), | ||
})); | ||
|
||
describe("supabase-middleware", () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe("middleware", () => { | ||
test("calls updateSession with the request object", async () => { | ||
const request = { some: "request" }; | ||
const response = { some: "response" }; | ||
|
||
updateSession.mockResolvedValueOnce(response); | ||
|
||
const result = await middleware(request); | ||
|
||
expect(updateSession).toHaveBeenCalledWith(request); | ||
expect(result).toEqual(response); | ||
}); | ||
|
||
test("throws an error if updateSession throws an error", async () => { | ||
const request = { some: "request" }; | ||
const error = new Error("Something went wrong"); | ||
|
||
updateSession.mockRejectedValueOnce(error); | ||
|
||
await expect(middleware(request)).rejects.toThrow(error); | ||
}); | ||
}); | ||
|
||
describe("config", () => { | ||
test("matches the expected paths", () => { | ||
expect(config.matcher).toEqual([ | ||
"/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)", | ||
]); | ||
}); | ||
}); | ||
}); |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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