|
| 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 | +}) |
0 commit comments