Skip to content

Commit a99bae7

Browse files
emilyjablonskimcgarrye
authored andcommitted
test: listing intro test (bloom-housing#5550)
1 parent da9e5e6 commit a99bae7

2 files changed

Lines changed: 163 additions & 6 deletions

File tree

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import React from "react"
2+
import { rest } from "msw"
3+
import { setupServer } from "msw/node"
4+
import userEvent from "@testing-library/user-event"
5+
import { screen } from "@testing-library/react"
6+
import { FormProvider, useForm } from "react-hook-form"
7+
import {
8+
FeatureFlagEnum,
9+
Jurisdiction,
10+
} from "@bloom-housing/shared-helpers/src/types/backend-swagger"
11+
import { mockNextRouter, render } from "../../../../testUtils"
12+
import { formDefaults, FormListing } from "../../../../../src/lib/listings/formTypes"
13+
import ListingIntro from "../../../../../src/components/listings/PaperListingForm/sections/ListingIntro"
14+
15+
const FormComponent = ({ children, values }: { values?: FormListing; children }) => {
16+
const formMethods = useForm<FormListing>({
17+
defaultValues: { ...formDefaults, ...values },
18+
shouldUnregister: false,
19+
})
20+
return <FormProvider {...formMethods}>{children}</FormProvider>
21+
}
22+
23+
const server = setupServer()
24+
25+
// Enable API mocking before tests.
26+
beforeAll(() => {
27+
server.listen()
28+
mockNextRouter()
29+
})
30+
31+
// Reset any runtime request handlers we may add during the tests.
32+
afterEach(() => server.resetHandlers())
33+
34+
// Disable API mocking after the tests are done.
35+
afterAll(() => server.close())
36+
37+
describe("ListingIntro", () => {
38+
const adminUserWithJurisdictions = {
39+
jurisdictions: [
40+
{
41+
id: "jurisdiction1",
42+
name: "jurisdictionWithJurisdictionAdmin",
43+
featureFlags: [],
44+
},
45+
],
46+
}
47+
48+
it("should render the ListingIntro section with one jurisdiction", async () => {
49+
document.cookie = "access-token-available=True"
50+
server.use(
51+
rest.get("http://localhost/api/adapter/user", (_req, res, ctx) => {
52+
return res(ctx.json(adminUserWithJurisdictions))
53+
})
54+
)
55+
56+
render(
57+
<FormComponent>
58+
<ListingIntro
59+
requiredFields={[]}
60+
jurisdictions={[
61+
{
62+
id: "JurisdictionA",
63+
name: "JurisdictionA",
64+
} as unknown as Jurisdiction,
65+
]}
66+
/>
67+
</FormComponent>
68+
)
69+
70+
await screen.findByRole("heading", { level: 2, name: "Listing intro" })
71+
expect(
72+
screen.getByText("Let's get started with some basic information about your listing.")
73+
).toBeInTheDocument()
74+
expect(screen.getByRole("textbox", { name: "Listing name *" })).toBeInTheDocument()
75+
expect(screen.queryByRole("combobox", { name: "Jurisdiction *" })).not.toBeInTheDocument()
76+
expect(screen.getByRole("textbox", { name: "Housing developer" })).toBeInTheDocument()
77+
})
78+
79+
it("should render the ListingIntro section with multiple jurisdictions and required developer", async () => {
80+
document.cookie = "access-token-available=True"
81+
server.use(
82+
rest.get("http://localhost/api/adapter/user", (_req, res, ctx) => {
83+
return res(ctx.json(adminUserWithJurisdictions))
84+
})
85+
)
86+
87+
render(
88+
<FormComponent>
89+
<ListingIntro
90+
requiredFields={["developer"]}
91+
jurisdictions={[
92+
{
93+
id: "JurisdictionA",
94+
name: "JurisdictionA",
95+
} as unknown as Jurisdiction,
96+
{
97+
id: "JurisdictionB",
98+
name: "JurisdictionB",
99+
} as unknown as Jurisdiction,
100+
]}
101+
/>
102+
</FormComponent>
103+
)
104+
105+
expect(screen.getByRole("textbox", { name: "Listing name *" })).toBeInTheDocument()
106+
expect(screen.getByRole("combobox", { name: "Jurisdiction *" })).toBeInTheDocument()
107+
expect(screen.getByRole("textbox", { name: "Housing developer *" })).toBeInTheDocument()
108+
109+
await userEvent.selectOptions(
110+
screen.getByRole("combobox", { name: "Jurisdiction *" }),
111+
screen.getByRole("option", { name: "JurisdictionA" })
112+
)
113+
114+
expect(screen.getByRole("textbox", { name: "Housing developer *" })).toBeInTheDocument()
115+
})
116+
117+
it("should render appropriate text when housing developer owner feature flag is on", async () => {
118+
document.cookie = "access-token-available=True"
119+
server.use(
120+
rest.get("http://localhost/api/adapter/user", (_req, res, ctx) => {
121+
return res(
122+
ctx.json({
123+
jurisdictions: [
124+
{
125+
id: "JurisdictionA",
126+
name: "jurisdictionWithJurisdictionAdmin",
127+
featureFlags: [{ name: FeatureFlagEnum.enableHousingDeveloperOwner, active: true }],
128+
},
129+
],
130+
})
131+
)
132+
})
133+
)
134+
135+
render(
136+
<FormComponent>
137+
<ListingIntro
138+
requiredFields={[]}
139+
jurisdictions={[
140+
{
141+
id: "JurisdictionA",
142+
name: "JurisdictionA",
143+
featureFlags: [{ name: FeatureFlagEnum.enableHousingDeveloperOwner, active: true }],
144+
} as unknown as Jurisdiction,
145+
]}
146+
/>
147+
</FormComponent>
148+
)
149+
await screen.findByRole("textbox", { name: "Housing developer / owner" })
150+
expect(screen.getByRole("textbox", { name: "Housing developer / owner" })).toBeInTheDocument()
151+
expect(screen.queryByRole("textbox", { name: "Housing developer" })).not.toBeInTheDocument()
152+
})
153+
})

sites/partners/src/components/listings/PaperListingForm/sections/ListingIntro.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import React, { useContext } from "react"
22
import { useFormContext } from "react-hook-form"
3+
import { t, Field, SelectOption, Select } from "@bloom-housing/ui-components"
4+
import { Grid } from "@bloom-housing/ui-seeds"
35
import {
46
FeatureFlagEnum,
57
Jurisdiction,
68
} from "@bloom-housing/shared-helpers/src/types/backend-swagger"
7-
import { t, Field, SelectOption, Select } from "@bloom-housing/ui-components"
8-
import { Grid } from "@bloom-housing/ui-seeds"
9+
import { AuthContext } from "@bloom-housing/shared-helpers"
910
import {
1011
fieldMessage,
1112
fieldHasError,
@@ -14,13 +15,17 @@ import {
1415
} from "../../../../lib/helpers"
1516
import SectionWithGrid from "../../../shared/SectionWithGrid"
1617
import styles from "../ListingForm.module.scss"
17-
import { AuthContext } from "@bloom-housing/shared-helpers"
1818

1919
interface ListingIntroProps {
2020
jurisdictions: Jurisdiction[]
2121
requiredFields: string[]
2222
}
2323

24+
const getDeveloperLabel = (jurisdiction: string, enableHousingDeveloperOwner: boolean) => {
25+
if (!jurisdiction) return t("listings.developer")
26+
return enableHousingDeveloperOwner ? t("listings.housingDeveloperOwner") : t("listings.developer")
27+
}
28+
2429
const ListingIntro = (props: ListingIntroProps) => {
2530
const formMethods = useFormContext()
2631
const { doJurisdictionsHaveFeatureFlagOn } = useContext(AuthContext)
@@ -103,6 +108,7 @@ const ListingIntro = (props: ListingIntroProps) => {
103108
}
104109
},
105110
"aria-required": fieldIsRequired("jurisdictions", props.requiredFields),
111+
"aria-hidden": !!defaultJurisdiction,
106112
}}
107113
/>
108114
</Grid.Cell>
@@ -112,9 +118,7 @@ const ListingIntro = (props: ListingIntroProps) => {
112118
register={register}
113119
{...defaultFieldProps(
114120
"developer",
115-
enableHousingDeveloperOwner
116-
? t("listings.housingDeveloperOwner")
117-
: t("listings.developer"),
121+
getDeveloperLabel(jurisdiction, enableHousingDeveloperOwner),
118122
props.requiredFields,
119123
errors,
120124
clearErrors

0 commit comments

Comments
 (0)