2022-01-03 08:55:09 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-01-03 08:55:09 +00:00
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-01-03 08:55:09 +00:00
|
|
|
*/
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import React from "react";
|
2024-10-14 16:11:58 +00:00
|
|
|
import { fireEvent, render, RenderResult } from "jest-matrix-react";
|
2022-01-03 08:55:09 +00:00
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
import StyledRadioGroup from "../../../../../src/components/views/elements/StyledRadioGroup";
|
2022-01-03 08:55:09 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("<StyledRadioGroup />", () => {
|
2022-01-03 08:55:09 +00:00
|
|
|
const optionA = {
|
2022-12-12 11:24:14 +00:00
|
|
|
value: "Anteater",
|
2022-01-03 08:55:09 +00:00
|
|
|
label: <span>Anteater label</span>,
|
2022-12-12 11:24:14 +00:00
|
|
|
description: "anteater description",
|
|
|
|
className: "a-class",
|
2022-01-03 08:55:09 +00:00
|
|
|
};
|
|
|
|
const optionB = {
|
2022-12-12 11:24:14 +00:00
|
|
|
value: "Badger",
|
2022-01-03 08:55:09 +00:00
|
|
|
label: <span>Badger label</span>,
|
|
|
|
};
|
|
|
|
const optionC = {
|
2022-12-12 11:24:14 +00:00
|
|
|
value: "Canary",
|
2022-01-03 08:55:09 +00:00
|
|
|
label: <span>Canary label</span>,
|
|
|
|
description: <span>Canary description</span>,
|
|
|
|
};
|
|
|
|
const defaultDefinitions = [optionA, optionB, optionC];
|
|
|
|
const defaultProps = {
|
2022-12-12 11:24:14 +00:00
|
|
|
name: "test",
|
|
|
|
className: "test-class",
|
2022-01-03 08:55:09 +00:00
|
|
|
definitions: defaultDefinitions,
|
|
|
|
onChange: jest.fn(),
|
|
|
|
};
|
2022-10-24 08:06:20 +00:00
|
|
|
const getComponent = (props = {}) => render(<StyledRadioGroup {...defaultProps} {...props} />);
|
2022-01-03 08:55:09 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
const getInputByValue = (component: RenderResult, value: string) =>
|
|
|
|
component.container.querySelector<HTMLInputElement>(`input[value="${value}"]`);
|
|
|
|
const getCheckedInput = (component: RenderResult) =>
|
|
|
|
component.container.querySelector<HTMLInputElement>("input[checked]");
|
2022-01-03 08:55:09 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders radios correctly when no value is provided", () => {
|
2022-01-03 08:55:09 +00:00
|
|
|
const component = getComponent();
|
|
|
|
|
2022-10-24 08:06:20 +00:00
|
|
|
expect(component.asFragment()).toMatchSnapshot();
|
|
|
|
expect(getCheckedInput(component)).toBeFalsy();
|
2022-01-03 08:55:09 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("selects correct button when value is provided", () => {
|
2022-01-03 08:55:09 +00:00
|
|
|
const component = getComponent({
|
|
|
|
value: optionC.value,
|
|
|
|
});
|
|
|
|
|
2023-02-15 13:36:22 +00:00
|
|
|
expect(getCheckedInput(component)?.value).toEqual(optionC.value);
|
2022-01-03 08:55:09 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("selects correct buttons when definitions have checked prop", () => {
|
|
|
|
const definitions = [{ ...optionA, checked: true }, optionB, { ...optionC, checked: false }];
|
2022-01-03 08:55:09 +00:00
|
|
|
const component = getComponent({
|
2022-12-12 11:24:14 +00:00
|
|
|
value: optionC.value,
|
|
|
|
definitions,
|
2022-01-03 08:55:09 +00:00
|
|
|
});
|
|
|
|
|
2022-10-24 08:06:20 +00:00
|
|
|
expect(getInputByValue(component, optionA.value)).toBeChecked();
|
|
|
|
expect(getInputByValue(component, optionB.value)).not.toBeChecked();
|
2022-01-03 08:55:09 +00:00
|
|
|
// optionC.checked = false overrides value matching
|
2022-10-24 08:06:20 +00:00
|
|
|
expect(getInputByValue(component, optionC.value)).not.toBeChecked();
|
2022-01-03 08:55:09 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("disables individual buttons based on definition.disabled", () => {
|
|
|
|
const definitions = [optionA, { ...optionB, disabled: true }, { ...optionC, disabled: true }];
|
2022-01-03 08:55:09 +00:00
|
|
|
const component = getComponent({ definitions });
|
2022-10-24 08:06:20 +00:00
|
|
|
expect(getInputByValue(component, optionA.value)).not.toBeDisabled();
|
|
|
|
expect(getInputByValue(component, optionB.value)).toBeDisabled();
|
|
|
|
expect(getInputByValue(component, optionC.value)).toBeDisabled();
|
2022-01-03 08:55:09 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("disables all buttons with disabled prop", () => {
|
2022-01-03 08:55:09 +00:00
|
|
|
const component = getComponent({ disabled: true });
|
2022-10-24 08:06:20 +00:00
|
|
|
expect(getInputByValue(component, optionA.value)).toBeDisabled();
|
|
|
|
expect(getInputByValue(component, optionB.value)).toBeDisabled();
|
|
|
|
expect(getInputByValue(component, optionC.value)).toBeDisabled();
|
2022-01-03 08:55:09 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("calls onChange on click", () => {
|
2022-01-03 08:55:09 +00:00
|
|
|
const onChange = jest.fn();
|
|
|
|
const component = getComponent({
|
|
|
|
value: optionC.value,
|
|
|
|
onChange,
|
|
|
|
});
|
|
|
|
|
2023-02-15 13:36:22 +00:00
|
|
|
fireEvent.click(getInputByValue(component, optionB.value)!);
|
2022-01-03 08:55:09 +00:00
|
|
|
|
|
|
|
expect(onChange).toHaveBeenCalledWith(optionB.value);
|
|
|
|
});
|
|
|
|
});
|