@@ -4,6 +4,7 @@ import { fireEvent, render, waitFor } from "@testing-library/react";
44
55import { AccountForm } from "./AccountForm" ;
66import React from "react" ;
7+ import { SessionProvider } from "next-auth/react" ;
78import { useIsDesktop } from "@/hooks/useIsDesktop" ;
89
910jest . mock ( "@/hooks/useIsDesktop" , ( ) => ( {
@@ -19,6 +20,30 @@ jest.mock("./SocialsForm", () => ({
1920 SocialsForm : ( ) => < div > SocialsForm</ div > ,
2021} ) ) ;
2122
23+ jest . mock ( "@/graphql/deleteUser" , ( ) => ( {
24+ deleteUser : jest . fn ( ) ,
25+ } ) ) ;
26+
27+ jest . mock ( "next-auth/react" , ( ) => ( {
28+ ...jest . requireActual ( "next-auth/react" ) ,
29+ useSession : ( ) => ( {
30+ data : {
31+ user : {
32+ id : "test-user-id" ,
33+ 34+ } ,
35+ } ,
36+ status : "authenticated" ,
37+ } ) ,
38+ signOut : jest . fn ( ) ,
39+ } ) ) ;
40+
41+ jest . mock ( "next/navigation" , ( ) => ( {
42+ useRouter : ( ) => ( {
43+ push : jest . fn ( ) ,
44+ } ) ,
45+ } ) ) ;
46+
2247const mockProps = {
2348 name : "John Doe" ,
2449 slug : "john-doe" ,
@@ -30,14 +55,18 @@ const mockProps = {
3055 siteImage : "https://example.com/image.png" ,
3156} ;
3257
58+ const renderWithSession = ( component : React . ReactElement ) => {
59+ return render ( < SessionProvider > { component } </ SessionProvider > ) ;
60+ } ;
61+
3362describe ( "AccountForm" , ( ) => {
3463 beforeEach ( ( ) => {
3564 jest . clearAllMocks ( ) ;
3665 ( useIsDesktop as jest . Mock ) . mockReturnValue ( true ) ;
3766 } ) ;
3867
3968 it ( "renders correctly" , ( ) => {
40- const { container, getByLabelText } = render ( < AccountForm { ...mockProps } /> ) ;
69+ const { container, getByLabelText } = renderWithSession ( < AccountForm { ...mockProps } /> ) ;
4170
4271 expect ( getByLabelText ( "Full Name" ) ) . toBeInTheDocument ( ) ;
4372 expect ( getByLabelText ( "URL Name" ) ) . toBeInTheDocument ( ) ;
@@ -49,7 +78,7 @@ describe("AccountForm", () => {
4978 } ) ;
5079
5180 it ( "handles input changes and validation" , async ( ) => {
52- const { getByLabelText, getByText } = render ( < AccountForm { ...mockProps } /> ) ;
81+ const { getByLabelText, getByText } = renderWithSession ( < AccountForm { ...mockProps } /> ) ;
5382
5483 const slugInput = getByLabelText ( "URL Name" ) ;
5584 fireEvent . change ( slugInput , { target : { value : "invalid slug" } } ) ;
@@ -69,7 +98,7 @@ describe("AccountForm", () => {
6998 } ) ,
7099 ) as jest . Mock ;
71100
72- const { getByLabelText, getByText } = render ( < AccountForm { ...mockProps } /> ) ;
101+ const { getByLabelText, getByText } = renderWithSession ( < AccountForm { ...mockProps } /> ) ;
73102
74103 fireEvent . change ( getByLabelText ( "Full Name" ) , { target : { value : "Jane Doe" } } ) ;
75104 fireEvent . change ( getByLabelText ( "URL Name" ) , { target : { value : "jane-doe" } } ) ;
@@ -90,7 +119,7 @@ describe("AccountForm", () => {
90119 } ) ,
91120 ) as jest . Mock ;
92121
93- const { getByLabelText, getByText } = render ( < AccountForm { ...mockProps } /> ) ;
122+ const { getByLabelText, getByText } = renderWithSession ( < AccountForm { ...mockProps } /> ) ;
94123
95124 fireEvent . change ( getByLabelText ( "Full Name" ) , { target : { value : "Jane Doe" } } ) ;
96125 fireEvent . change ( getByLabelText ( "URL Name" ) , { target : { value : "jane-doe" } } ) ;
@@ -104,7 +133,7 @@ describe("AccountForm", () => {
104133 } ) ;
105134
106135 it ( "displays error message on form submission failure for invalid name" , async ( ) => {
107- const { getByLabelText, getByText } = render ( < AccountForm { ...mockProps } /> ) ;
136+ const { getByLabelText, getByText } = renderWithSession ( < AccountForm { ...mockProps } /> ) ;
108137
109138 fireEvent . change ( getByLabelText ( "Full Name" ) , { target : { value : " " } } ) ;
110139 fireEvent . click ( getByText ( "Save" ) ) ;
@@ -115,7 +144,7 @@ describe("AccountForm", () => {
115144 } ) ;
116145
117146 it ( "displays error message on form submission failure for invalid slug" , async ( ) => {
118- const { getByLabelText, getByText } = render ( < AccountForm { ...mockProps } /> ) ;
147+ const { getByLabelText, getByText } = renderWithSession ( < AccountForm { ...mockProps } /> ) ;
119148
120149 fireEvent . change ( getByLabelText ( "URL Name" ) , { target : { value : " " } } ) ;
121150 fireEvent . click ( getByText ( "Save" ) ) ;
@@ -126,7 +155,7 @@ describe("AccountForm", () => {
126155 } ) ;
127156
128157 it ( "displays error message on form submission failure for invalid email" , async ( ) => {
129- const { getByLabelText, getByText } = render ( < AccountForm { ...mockProps } /> ) ;
158+ const { getByLabelText, getByText } = renderWithSession ( < AccountForm { ...mockProps } /> ) ;
130159
131160 const emailInput = getByLabelText ( "Display Email" ) ;
132161 fireEvent . change ( emailInput , { target : { value : "invalid-email" } } ) ;
0 commit comments