|
| 1 | +import { createRemixStub } from "@remix-run/testing"; |
| 2 | +import { render, screen, waitFor } from "@testing-library/react"; |
| 3 | +import { expect, test } from "vitest"; |
| 4 | +import "@testing-library/jest-dom"; |
| 5 | +import { userEvent } from "@testing-library/user-event"; |
| 6 | +import { authenticator } from "~/utils/auth.server"; |
| 7 | +import { prisma } from "~/utils/prisma"; |
| 8 | +import UserProfile, { loader, action } from "./index"; |
| 9 | + |
| 10 | +vi.mock("~/utils/auth.server", () => ({ |
| 11 | + authenticator: { |
| 12 | + isAuthenticated: vi.fn(), |
| 13 | + }, |
| 14 | +})); |
| 15 | + |
| 16 | +describe("UserProfile", () => { |
| 17 | + beforeEach(async () => { |
| 18 | + await prisma.user.create({ |
| 19 | + data: { |
| 20 | + userName: "testuser", |
| 21 | + displayName: "Test User", |
| 22 | + email: "testuser@example.com", |
| 23 | + icon: "https://example.com/icon.jpg", |
| 24 | + profile: "This is a test profile", |
| 25 | + pages: { |
| 26 | + create: [ |
| 27 | + { |
| 28 | + title: "Public Page", |
| 29 | + slug: "public-page", |
| 30 | + isPublished: true, |
| 31 | + content: "This is a test content", |
| 32 | + }, |
| 33 | + { |
| 34 | + title: "Private Page", |
| 35 | + slug: "private-page", |
| 36 | + isPublished: false, |
| 37 | + content: "This is a test content2", |
| 38 | + }, |
| 39 | + { |
| 40 | + title: "Archived Page", |
| 41 | + slug: "archived-page", |
| 42 | + isArchived: true, |
| 43 | + content: "This is a test content3", |
| 44 | + }, |
| 45 | + ], |
| 46 | + }, |
| 47 | + }, |
| 48 | + include: { pages: true }, |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + test("loader returns correct data and menu is displayed for authenticated owner", async () => { |
| 53 | + // @ts-ignore |
| 54 | + vi.mocked(authenticator.isAuthenticated).mockResolvedValue({ |
| 55 | + id: 1, |
| 56 | + userName: "testuser", |
| 57 | + }); |
| 58 | + const RemixStub = createRemixStub([ |
| 59 | + { |
| 60 | + path: "/:userName", |
| 61 | + Component: UserProfile, |
| 62 | + loader, |
| 63 | + }, |
| 64 | + ]); |
| 65 | + |
| 66 | + render(<RemixStub initialEntries={["/testuser"]} />); |
| 67 | + |
| 68 | + expect(await screen.findByText("Test User")).toBeInTheDocument(); |
| 69 | + expect( |
| 70 | + await screen.findByText("This is a test profile"), |
| 71 | + ).toBeInTheDocument(); |
| 72 | + expect(await screen.findByText("Public Page")).toBeInTheDocument(); |
| 73 | + expect(await screen.findByText("Private Page")).toBeInTheDocument(); |
| 74 | + expect(await screen.queryByText("Archived Page")).not.toBeInTheDocument(); |
| 75 | + const menuButtons = await screen.findAllByLabelText("More options"); |
| 76 | + expect(menuButtons.length).toBeGreaterThan(0); |
| 77 | + |
| 78 | + await userEvent.click(menuButtons[0]); |
| 79 | + |
| 80 | + expect(await screen.findByText("Edit")).toBeInTheDocument(); |
| 81 | + expect(await screen.findByText("Make Private")).toBeInTheDocument(); |
| 82 | + expect(await screen.findByText("Delete")).toBeInTheDocument(); |
| 83 | + }); |
| 84 | + |
| 85 | + test("loader returns correct data and menu is not displayed for unauthenticated visitor", async () => { |
| 86 | + // @ts-ignore |
| 87 | + vi.mocked(authenticator.isAuthenticated).mockResolvedValue(null); |
| 88 | + const RemixStub = createRemixStub([ |
| 89 | + { |
| 90 | + path: "/:userName", |
| 91 | + Component: UserProfile, |
| 92 | + loader, |
| 93 | + }, |
| 94 | + ]); |
| 95 | + render(<RemixStub initialEntries={["/testuser"]} />); |
| 96 | + |
| 97 | + expect(await screen.findByText("Test User")).toBeInTheDocument(); |
| 98 | + expect( |
| 99 | + await screen.findByText("This is a test profile"), |
| 100 | + ).toBeInTheDocument(); |
| 101 | + expect(await screen.findByText("Public Page")).toBeInTheDocument(); |
| 102 | + expect(await screen.queryByText("Private Page")).not.toBeInTheDocument(); |
| 103 | + expect(await screen.queryByText("Archived Page")).not.toBeInTheDocument(); |
| 104 | + expect( |
| 105 | + await screen.queryByLabelText("More options"), |
| 106 | + ).not.toBeInTheDocument(); |
| 107 | + }); |
| 108 | + |
| 109 | + test("action handles togglePublish correctly", async () => { |
| 110 | + // @ts-ignore |
| 111 | + vi.mocked(authenticator.isAuthenticated).mockResolvedValue({ |
| 112 | + id: 1, |
| 113 | + userName: "testuser", |
| 114 | + }); |
| 115 | + const RemixStub = createRemixStub([ |
| 116 | + { |
| 117 | + path: "/:userName", |
| 118 | + Component: UserProfile, |
| 119 | + loader, |
| 120 | + action, |
| 121 | + }, |
| 122 | + ]); |
| 123 | + render(<RemixStub initialEntries={["/testuser"]} />); |
| 124 | + |
| 125 | + const menuButtons = await screen.findAllByLabelText("More options"); |
| 126 | + expect(menuButtons.length).toBeGreaterThan(0); |
| 127 | + |
| 128 | + await userEvent.click(menuButtons[0]); |
| 129 | + |
| 130 | + expect(await screen.findByText("Edit")).toBeInTheDocument(); |
| 131 | + expect(await screen.findByText("Make Private")).toBeInTheDocument(); |
| 132 | + await userEvent.click(await screen.findByText("Make Private")); |
| 133 | + |
| 134 | + waitFor(() => { |
| 135 | + userEvent.click(menuButtons[0]); |
| 136 | + expect(screen.findByText("Make Public")).toBeInTheDocument(); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + test("action handles archive correctly", async () => { |
| 141 | + // @ts-ignore |
| 142 | + vi.mocked(authenticator.isAuthenticated).mockResolvedValue({ |
| 143 | + id: 1, |
| 144 | + userName: "testuser", |
| 145 | + }); |
| 146 | + const RemixStub = createRemixStub([ |
| 147 | + { |
| 148 | + path: "/:userName", |
| 149 | + Component: UserProfile, |
| 150 | + loader, |
| 151 | + action, |
| 152 | + }, |
| 153 | + ]); |
| 154 | + render(<RemixStub initialEntries={["/testuser"]} />); |
| 155 | + |
| 156 | + const menuButtons = await screen.findAllByLabelText("More options"); |
| 157 | + expect(menuButtons.length).toBeGreaterThan(0); |
| 158 | + |
| 159 | + await userEvent.click(menuButtons[0]); |
| 160 | + |
| 161 | + expect(await screen.findByText("Delete")).toBeInTheDocument(); |
| 162 | + await userEvent.click(await screen.findByText("Delete")); |
| 163 | + expect( |
| 164 | + await screen.findByText( |
| 165 | + "This action cannot be undone. Are you sure you want to delete this page?", |
| 166 | + ), |
| 167 | + ).toBeInTheDocument(); |
| 168 | + |
| 169 | + await userEvent.click(await screen.findByText("Delete")); |
| 170 | + |
| 171 | + await waitFor(() => { |
| 172 | + expect(screen.queryByText("Test Page")).not.toBeInTheDocument(); |
| 173 | + }); |
| 174 | + }); |
| 175 | +}); |
0 commit comments