11import React from "react"
22import { render , screen } from "@testing-library/react"
33import { MemoryRouter } from "react-router"
4+ import { useAuth } from "@clerk/clerk-react"
45import { ListingDetailsApply } from "../../../modules/listingDetailsAside/ListingDetailsApply"
56import { openSaleListing } from "../../data/RailsSaleListing/listing-sale-open"
67import { closedRentalListing } from "../../data/RailsRentalListing/listing-rental-closed"
78import { habitatListing } from "../../data/RailsSaleListing/listing-sale-habitat"
89import { setupUserContext } from "../../__util__/accountUtils"
9- import { getAddProfilePath } from "../../../util/routeUtil"
10+ import { getAddProfilePath , getSignInPath } from "../../../util/routeUtil"
1011import { useFeatureFlag } from "../../../hooks/useFeatureFlag"
1112import { UNLEASH_FLAG } from "../../../modules/constants"
1213
@@ -68,6 +69,17 @@ describe("ListingDetailsApply", () => {
6869 } ) )
6970 } )
7071
72+ it ( "redirects signed out users to sign in" , ( ) => {
73+ setupUserContext ( { loggedIn : false } )
74+
75+ render ( < ListingDetailsApply listing = { openSaleListing } /> )
76+
77+ expect ( screen . getByRole ( "link" , { name : / a p p l y o n l i n e / i } ) ) . toHaveAttribute (
78+ "href" ,
79+ getSignInPath ( )
80+ )
81+ } )
82+
7183 it ( "redirects signed in users without a completed profile to the add profile page" , ( ) => {
7284 setupUserContext ( { loggedIn : true , hasProfile : false } )
7385
@@ -78,5 +90,44 @@ describe("ListingDetailsApply", () => {
7890 getAddProfilePath ( )
7991 )
8092 } )
93+
94+ it ( "does not render apply online while signed-in profile state is still loading" , ( ) => {
95+ const context = setupUserContext ( { loggedIn : true , hasProfile : false } )
96+ context . initialStateLoaded = false
97+ ; ( useAuth as jest . Mock ) . mockReturnValue ( {
98+ isLoaded : true ,
99+ isSignedIn : true ,
100+ } )
101+
102+ render ( < ListingDetailsApply listing = { openSaleListing } /> )
103+
104+ expect ( screen . queryByRole ( "link" , { name : / a p p l y o n l i n e / i } ) ) . toBeNull ( )
105+ } )
106+
107+ it ( "uses the listing application link for signed in users with a profile" , ( ) => {
108+ setupUserContext ( { loggedIn : true , hasProfile : true } )
109+ ; ( useAuth as jest . Mock ) . mockReturnValue ( {
110+ isLoaded : true ,
111+ isSignedIn : true ,
112+ } )
113+
114+ render ( < ListingDetailsApply listing = { openSaleListing } /> )
115+
116+ expect ( screen . getByRole ( "link" , { name : / a p p l y o n l i n e / i } ) . getAttribute ( "href" ) ) . toBe (
117+ `/listings/${ openSaleListing . listingID } /apply-welcome/intro`
118+ )
119+ } )
120+
121+ it ( "does not render apply online while Clerk auth is loading" , ( ) => {
122+ setupUserContext ( { loggedIn : false } )
123+ ; ( useAuth as jest . Mock ) . mockReturnValue ( {
124+ isLoaded : false ,
125+ isSignedIn : false ,
126+ } )
127+
128+ render ( < ListingDetailsApply listing = { openSaleListing } /> )
129+
130+ expect ( screen . queryByRole ( "link" , { name : / a p p l y o n l i n e / i } ) ) . toBeNull ( )
131+ } )
81132 } )
82133} )
0 commit comments