-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
249 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import { createRemixStub } from "@remix-run/testing"; | ||
import { render, screen, waitFor } from "@testing-library/react"; | ||
import { expect, test } from "vitest"; | ||
import "@testing-library/jest-dom"; | ||
import { userEvent } from "@testing-library/user-event"; | ||
import { authenticator } from "~/utils/auth.server"; | ||
import { prisma } from "~/utils/prisma"; | ||
import UserProfile, { loader, action } from "./index"; | ||
|
||
vi.mock("~/utils/auth.server", () => ({ | ||
authenticator: { | ||
isAuthenticated: vi.fn(), | ||
}, | ||
})); | ||
|
||
describe("UserProfile", () => { | ||
beforeEach(async () => { | ||
await prisma.user.create({ | ||
data: { | ||
userName: "testuser", | ||
displayName: "Test User", | ||
email: "[email protected]", | ||
icon: "https://example.com/icon.jpg", | ||
profile: "This is a test profile", | ||
pages: { | ||
create: [ | ||
{ | ||
title: "Public Page", | ||
slug: "public-page", | ||
isPublished: true, | ||
content: "This is a test content", | ||
}, | ||
{ | ||
title: "Private Page", | ||
slug: "private-page", | ||
isPublished: false, | ||
content: "This is a test content2", | ||
}, | ||
{ | ||
title: "Archived Page", | ||
slug: "archived-page", | ||
isArchived: true, | ||
content: "This is a test content3", | ||
}, | ||
], | ||
}, | ||
}, | ||
include: { pages: true }, | ||
}); | ||
}); | ||
|
||
test("loader returns correct data and menu is displayed for authenticated owner", async () => { | ||
// @ts-ignore | ||
vi.mocked(authenticator.isAuthenticated).mockResolvedValue({ | ||
id: 1, | ||
userName: "testuser", | ||
}); | ||
const RemixStub = createRemixStub([ | ||
{ | ||
path: "/:userName", | ||
Component: UserProfile, | ||
loader, | ||
}, | ||
]); | ||
|
||
render(<RemixStub initialEntries={["/testuser"]} />); | ||
|
||
expect(await screen.findByText("Test User")).toBeInTheDocument(); | ||
expect( | ||
await screen.findByText("This is a test profile"), | ||
).toBeInTheDocument(); | ||
expect(await screen.findByText("Public Page")).toBeInTheDocument(); | ||
expect(await screen.findByText("Private Page")).toBeInTheDocument(); | ||
expect(await screen.queryByText("Archived Page")).not.toBeInTheDocument(); | ||
const menuButtons = await screen.findAllByLabelText("More options"); | ||
expect(menuButtons.length).toBeGreaterThan(0); | ||
|
||
await userEvent.click(menuButtons[0]); | ||
|
||
expect(await screen.findByText("Edit")).toBeInTheDocument(); | ||
expect(await screen.findByText("Make Private")).toBeInTheDocument(); | ||
expect(await screen.findByText("Delete")).toBeInTheDocument(); | ||
}); | ||
|
||
test("loader returns correct data and menu is not displayed for unauthenticated visitor", async () => { | ||
// @ts-ignore | ||
vi.mocked(authenticator.isAuthenticated).mockResolvedValue(null); | ||
const RemixStub = createRemixStub([ | ||
{ | ||
path: "/:userName", | ||
Component: UserProfile, | ||
loader, | ||
}, | ||
]); | ||
render(<RemixStub initialEntries={["/testuser"]} />); | ||
|
||
expect(await screen.findByText("Test User")).toBeInTheDocument(); | ||
expect( | ||
await screen.findByText("This is a test profile"), | ||
).toBeInTheDocument(); | ||
expect(await screen.findByText("Public Page")).toBeInTheDocument(); | ||
expect(await screen.queryByText("Private Page")).not.toBeInTheDocument(); | ||
expect(await screen.queryByText("Archived Page")).not.toBeInTheDocument(); | ||
expect( | ||
await screen.queryByLabelText("More options"), | ||
).not.toBeInTheDocument(); | ||
}); | ||
|
||
test("action handles togglePublish correctly", async () => { | ||
// @ts-ignore | ||
vi.mocked(authenticator.isAuthenticated).mockResolvedValue({ | ||
id: 1, | ||
userName: "testuser", | ||
}); | ||
const RemixStub = createRemixStub([ | ||
{ | ||
path: "/:userName", | ||
Component: UserProfile, | ||
loader, | ||
action, | ||
}, | ||
]); | ||
render(<RemixStub initialEntries={["/testuser"]} />); | ||
|
||
const menuButtons = await screen.findAllByLabelText("More options"); | ||
expect(menuButtons.length).toBeGreaterThan(0); | ||
|
||
await userEvent.click(menuButtons[0]); | ||
|
||
expect(await screen.findByText("Edit")).toBeInTheDocument(); | ||
expect(await screen.findByText("Make Private")).toBeInTheDocument(); | ||
await userEvent.click(await screen.findByText("Make Private")); | ||
|
||
waitFor(() => { | ||
userEvent.click(menuButtons[0]); | ||
expect(screen.findByText("Make Public")).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
test("action handles archive correctly", async () => { | ||
// @ts-ignore | ||
vi.mocked(authenticator.isAuthenticated).mockResolvedValue({ | ||
id: 1, | ||
userName: "testuser", | ||
}); | ||
const RemixStub = createRemixStub([ | ||
{ | ||
path: "/:userName", | ||
Component: UserProfile, | ||
loader, | ||
action, | ||
}, | ||
]); | ||
render(<RemixStub initialEntries={["/testuser"]} />); | ||
|
||
const menuButtons = await screen.findAllByLabelText("More options"); | ||
expect(menuButtons.length).toBeGreaterThan(0); | ||
|
||
await userEvent.click(menuButtons[0]); | ||
|
||
expect(await screen.findByText("Delete")).toBeInTheDocument(); | ||
await userEvent.click(await screen.findByText("Delete")); | ||
expect( | ||
await screen.findByText( | ||
"This action cannot be undone. Are you sure you want to delete this page?", | ||
), | ||
).toBeInTheDocument(); | ||
|
||
await userEvent.click(await screen.findByText("Delete")); | ||
|
||
await waitFor(() => { | ||
expect(screen.queryByText("Test Page")).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
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
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,6 @@ | ||
import { vi } from "vitest"; | ||
|
||
vi.mock("~/utils/prisma", () => ({ | ||
// @ts-ignore | ||
prisma: vPrisma.client, | ||
})); |