|
1 | 1 | import { render } from "@testing-library/react"; |
2 | 2 | import { CheckboxRadioSelectionContainerProps } from "../../typings/CheckboxRadioSelectionProps"; |
3 | 3 | import CheckboxRadioSelection from "../CheckboxRadioSelection"; |
| 4 | +import { CheckboxSelection } from "../components/CheckboxSelection/CheckboxSelection"; |
| 5 | +import { MultiSelector } from "../helpers/types"; |
4 | 6 |
|
5 | 7 | // Mock the selector to avoid implementation dependencies for basic tests |
6 | 8 | jest.mock("../helpers/getSelector", () => ({ |
@@ -89,3 +91,94 @@ describe("CheckboxRadioSelection", () => { |
89 | 91 | expect(widget?.className).toContain("widget-checkbox-radio-selection"); |
90 | 92 | }); |
91 | 93 | }); |
| 94 | + |
| 95 | +function makeMultiSelector(overrides: Partial<MultiSelector> = {}): MultiSelector { |
| 96 | + return { |
| 97 | + type: "multi", |
| 98 | + status: "available", |
| 99 | + readOnly: false, |
| 100 | + currentId: [], |
| 101 | + clearable: false, |
| 102 | + customContentType: "no", |
| 103 | + validation: undefined, |
| 104 | + updateProps: jest.fn(), |
| 105 | + setValue: jest.fn(), |
| 106 | + getOptions: jest.fn(() => ["option1", "option2", "option3"]), |
| 107 | + options: { |
| 108 | + status: "available", |
| 109 | + searchTerm: "", |
| 110 | + isLoading: false, |
| 111 | + getAll: jest.fn(() => ["option1", "option2", "option3"]), |
| 112 | + setSearchTerm: jest.fn(), |
| 113 | + onAfterSearchTermChange: jest.fn(), |
| 114 | + _updateProps: jest.fn(), |
| 115 | + _optionToValue: jest.fn(), |
| 116 | + _valueToOption: jest.fn() |
| 117 | + }, |
| 118 | + caption: { |
| 119 | + get: jest.fn((v: string) => `Caption ${v}`), |
| 120 | + render: jest.fn((v: string | null | number | null) => `Caption ${v}`), |
| 121 | + emptyCaption: "Select an option", |
| 122 | + formatter: undefined |
| 123 | + }, |
| 124 | + ...overrides |
| 125 | + }; |
| 126 | +} |
| 127 | + |
| 128 | +const baseCheckboxProps = { |
| 129 | + inputId: "test-checkbox", |
| 130 | + tabIndex: 0, |
| 131 | + ariaRequired: { status: "available" as const, value: false } as any, |
| 132 | + ariaLabel: undefined, |
| 133 | + groupName: undefined, |
| 134 | + noOptionsText: "No options" |
| 135 | +}; |
| 136 | + |
| 137 | +describe("CheckboxSelection – read-only text mode", () => { |
| 138 | + it("hides unselected options and their inputs", () => { |
| 139 | + const selector = makeMultiSelector({ readOnly: true, currentId: ["option1"] }); |
| 140 | + const { queryByDisplayValue } = render( |
| 141 | + <CheckboxSelection {...baseCheckboxProps} selector={selector} readOnlyStyle="text" /> |
| 142 | + ); |
| 143 | + |
| 144 | + // unselected options should not appear at all |
| 145 | + expect(queryByDisplayValue("option2")).toBeNull(); |
| 146 | + expect(queryByDisplayValue("option3")).toBeNull(); |
| 147 | + }); |
| 148 | + |
| 149 | + it("renders no <input> for the selected option in text mode", () => { |
| 150 | + const selector = makeMultiSelector({ readOnly: true, currentId: ["option1"] }); |
| 151 | + const { queryAllByRole } = render( |
| 152 | + <CheckboxSelection {...baseCheckboxProps} selector={selector} readOnlyStyle="text" /> |
| 153 | + ); |
| 154 | + |
| 155 | + expect(queryAllByRole("checkbox")).toHaveLength(0); |
| 156 | + }); |
| 157 | + |
| 158 | + it("renders selected option caption text", () => { |
| 159 | + const selector = makeMultiSelector({ readOnly: true, currentId: ["option1"] }); |
| 160 | + const { getByText } = render( |
| 161 | + <CheckboxSelection {...baseCheckboxProps} selector={selector} readOnlyStyle="text" /> |
| 162 | + ); |
| 163 | + |
| 164 | + expect(getByText("Caption option1")).toBeTruthy(); |
| 165 | + }); |
| 166 | + |
| 167 | + it("renders all inputs when not read-only", () => { |
| 168 | + const selector = makeMultiSelector({ readOnly: false, currentId: ["option1"] }); |
| 169 | + const { getAllByRole } = render( |
| 170 | + <CheckboxSelection {...baseCheckboxProps} selector={selector} readOnlyStyle="text" /> |
| 171 | + ); |
| 172 | + |
| 173 | + expect(getAllByRole("checkbox")).toHaveLength(3); |
| 174 | + }); |
| 175 | + |
| 176 | + it("renders inputs in bordered read-only mode", () => { |
| 177 | + const selector = makeMultiSelector({ readOnly: true, currentId: ["option1"] }); |
| 178 | + const { getAllByRole } = render( |
| 179 | + <CheckboxSelection {...baseCheckboxProps} selector={selector} readOnlyStyle="bordered" /> |
| 180 | + ); |
| 181 | + |
| 182 | + expect(getAllByRole("checkbox")).toHaveLength(3); |
| 183 | + }); |
| 184 | +}); |
0 commit comments