Skip to content

Commit bb0a440

Browse files
committed
fix: apply button
1 parent d83491c commit bb0a440

3 files changed

Lines changed: 59 additions & 12 deletions

File tree

app/javascript/__tests__/modules/listingDetailsAside/ListingDetailsApply.test.tsx

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import React from "react"
22
import { render, screen } from "@testing-library/react"
33
import { MemoryRouter } from "react-router"
4+
import { useAuth } from "@clerk/clerk-react"
45
import { ListingDetailsApply } from "../../../modules/listingDetailsAside/ListingDetailsApply"
56
import { openSaleListing } from "../../data/RailsSaleListing/listing-sale-open"
67
import { closedRentalListing } from "../../data/RailsRentalListing/listing-rental-closed"
78
import { habitatListing } from "../../data/RailsSaleListing/listing-sale-habitat"
89
import { setupUserContext } from "../../__util__/accountUtils"
9-
import { getAddProfilePath } from "../../../util/routeUtil"
10+
import { getAddProfilePath, getSignInPath } from "../../../util/routeUtil"
1011
import { useFeatureFlag } from "../../../hooks/useFeatureFlag"
1112
import { 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: /apply online/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: /apply online/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: /apply online/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: /apply online/i })).toBeNull()
131+
})
81132
})
82133
})

app/javascript/__tests__/pages/SignInFlow.test.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ import {
1010
restoreWindowLocation,
1111
} from "../__util__/renderUtils"
1212
import { setupUserContext } from "../__util__/accountUtils"
13-
import { AUTH_FLOW } from "../../modules/constants"
13+
import { AUTH_FLOW, UNLEASH_FLAG } from "../../modules/constants"
1414
import { authorizeHousingCounselor, getProfile } from "../../api/authApiService"
1515
import { useFeatureFlag } from "../../hooks/useFeatureFlag"
16-
import { UNLEASH_FLAG } from "../../modules/constants"
1716

1817
jest.mock("../../hooks/useFeatureFlag", () => ({
1918
useFeatureFlag: jest.fn(() => ({

app/javascript/modules/listingDetailsAside/ListingDetailsApply.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,13 @@ const ApplyButton = ({ href }: { href: string }) => (
8181
const ClerkApplyOnlineButton = ({ applyLink }: { applyLink: string }) => {
8282
const { isLoaded, isSignedIn } = useAuth()
8383
const { profile, initialStateLoaded } = useContext(UserContext)
84-
let redirectOrApplyUrl = ""
85-
if (isLoaded && isSignedIn && initialStateLoaded && profile) {
86-
redirectOrApplyUrl = applyLink
87-
} else if (isLoaded && !isSignedIn) {
88-
redirectOrApplyUrl = getSignInPath()
89-
} else if (isLoaded && isSignedIn && initialStateLoaded && !profile) {
90-
redirectOrApplyUrl = getAddProfilePath()
91-
}
9284

93-
return <ApplyButton href={redirectOrApplyUrl} />
85+
if (isLoaded && isSignedIn && profile) return <ApplyButton href={applyLink} />
86+
if (isLoaded && !isSignedIn) return <ApplyButton href={getSignInPath()} />
87+
if (isLoaded && isSignedIn && initialStateLoaded && !profile)
88+
return <ApplyButton href={getAddProfilePath()} />
89+
90+
return null
9491
}
9592

9693
/**

0 commit comments

Comments
 (0)