11import { fireEvent , render , screen , waitFor } from "@testing-library/react"
22import { useAuthDialogContext } from "Components/AuthDialog/AuthDialogContext"
3+ import { useAuthDialogTracking } from "Components/AuthDialog/Hooks/useAuthDialogTracking"
34import { AuthDialogLinkAccounts } from "Components/AuthDialog/Views/AuthDialogLinkAccounts"
45import { login } from "Utils/auth"
56
@@ -39,15 +40,24 @@ jest.mock("Components/AuthDialog/AuthDialogContext", () => ({
3940 useAuthDialogContext : jest . fn ( ) ,
4041} ) )
4142
43+ jest . mock ( "Components/AuthDialog/Hooks/useAuthDialogTracking" , ( ) => ( {
44+ useAuthDialogTracking : jest . fn ( ) ,
45+ } ) )
46+
4247describe ( "AuthDialogLinkAccounts" , ( ) => {
4348 const mockDispatch = jest . fn ( )
49+ const mockErrorMessageViewed = jest . fn ( )
4450
4551 beforeEach ( ( ) => {
4652 mockDispatch . mockClear ( )
53+ mockErrorMessageViewed . mockClear ( )
4754 ; ( useAuthDialogContext as jest . Mock ) . mockReturnValue ( {
4855 dispatch : mockDispatch ,
4956 state : { values : { } , options : { } } ,
5057 } )
58+ ; ( useAuthDialogTracking as jest . Mock ) . mockReturnValue ( {
59+ errorMessageViewed : mockErrorMessageViewed ,
60+ } )
5161 } )
5262
5363 it ( "renders the password form when only email provider is available" , ( ) => {
@@ -105,6 +115,13 @@ describe("AuthDialogLinkAccounts", () => {
105115 } )
106116
107117 expect ( screen . queryByText ( "Yes, Link Accounts" ) ) . not . toBeInTheDocument ( )
118+ expect ( mockErrorMessageViewed ) . toHaveBeenCalledWith ( {
119+ error_code : "two_factor_authentication_enabled" ,
120+ title : "Two-factor authentication enabled" ,
121+ message :
122+ "Social account linking is not available while two-factor authentication is enabled on your Artsy account." ,
123+ flow : "Link Accounts" ,
124+ } )
108125
109126 fireEvent . click ( screen . getByText ( "Go Back" ) )
110127
@@ -130,5 +147,69 @@ describe("AuthDialogLinkAccounts", () => {
130147 await waitFor ( ( ) => {
131148 expect ( screen . getByText ( "Go Back" ) ) . toBeInTheDocument ( )
132149 } )
150+
151+ expect ( mockErrorMessageViewed ) . toHaveBeenCalledWith (
152+ expect . objectContaining ( {
153+ error_code : "two_factor_authentication_enabled" ,
154+ } ) ,
155+ )
156+ } )
157+
158+ it ( "shows a link error and tracks when account linking fails after sign-in" , async ( ) => {
159+ render ( < AuthDialogLinkAccounts /> )
160+ ; ( login as jest . Mock ) . mockResolvedValueOnce ( { linkingError : true } )
161+
162+ const passwordInput = screen . getByPlaceholderText ( "Enter your password" )
163+ fireEvent . change ( passwordInput , { target : { value : "secret" } } )
164+
165+ // eslint-disable-next-line testing-library/no-node-access
166+ const button = screen . getByText ( "Yes, Link Accounts" ) . parentNode !
167+ fireEvent . click ( button )
168+
169+ await waitFor (
170+ ( ) => {
171+ expect (
172+ screen . getByText ( / c o u l d n .t l i n k y o u r G o o g l e a c c o u n t / ) ,
173+ ) . toBeInTheDocument ( )
174+ } ,
175+ { timeout : 3000 } ,
176+ )
177+
178+ expect ( mockErrorMessageViewed ) . toHaveBeenCalledWith ( {
179+ error_code : "link_accounts_error" ,
180+ title : "Account linking failed" ,
181+ message : expect . stringContaining ( "link your Google account" ) ,
182+ flow : "Link Accounts" ,
183+ } )
184+ } )
185+
186+ it ( "shows a generic error and tracks when login fails with an API error" , async ( ) => {
187+ render ( < AuthDialogLinkAccounts /> )
188+ ; ( login as jest . Mock ) . mockRejectedValueOnce (
189+ new Error ( "invalid email or password" ) ,
190+ )
191+
192+ const passwordInput = screen . getByPlaceholderText ( "Enter your password" )
193+ fireEvent . change ( passwordInput , { target : { value : "wrong" } } )
194+
195+ // eslint-disable-next-line testing-library/no-node-access
196+ const button = screen . getByText ( "Yes, Link Accounts" ) . parentNode !
197+ fireEvent . click ( button )
198+
199+ await waitFor ( ( ) => {
200+ expect (
201+ screen . getByText (
202+ "The email or password you entered is invalid. Please try again." ,
203+ ) ,
204+ ) . toBeInTheDocument ( )
205+ } )
206+
207+ expect ( mockErrorMessageViewed ) . toHaveBeenCalledWith ( {
208+ error_code : "invalid email or password" ,
209+ title : "Account linking failed" ,
210+ message :
211+ "The email or password you entered is invalid. Please try again." ,
212+ flow : "Link Accounts" ,
213+ } )
133214 } )
134215} )
0 commit comments