diff --git a/src/components/forms/SliderWithLabelForm.tsx b/src/components/forms/SliderWithLabelForm.tsx index 647b98c..9255b36 100644 --- a/src/components/forms/SliderWithLabelForm.tsx +++ b/src/components/forms/SliderWithLabelForm.tsx @@ -1,55 +1,47 @@ -import { useEffect, useState } from "react"; - import { Slider } from "@/components/ui/slider"; -export interface SliderWithLabelFormProps { - label: string; - minLabel: string; - maxLabel: string; +import { FormState, Question } from "./FormRenderer"; - minValue: number; - maxValue: number; - step: number; +// prettier-ignore +export type SliderWithLabelFormProps = + Omit & + FormState; - value: number[]; - onChange: (value: number[]) => void; -} +/** + * 기본 인자 + */ export const SliderWithLabelForm = ({ - label, - minLabel, - maxLabel, - minValue, - maxValue, - step, - - value, - onChange, + questionText, + options, + formState, + setFormState, }: SliderWithLabelFormProps) => { - const [sliderValue, setSliderValue] = useState(value); - - useEffect( - function onSliderValueChange() { - onChange(sliderValue); - }, - [sliderValue], - ); + const min = options[0]; + const max = options[options.length - 1]; return (
-

{label}

+

{questionText}

{ + setFormState({ + ...formState, + [questionText]: [{ value: value[0] }], + }); + }} + min={min.value} + max={max.value} + step={1} />
- {minLabel} - {maxLabel} + {min.label} + {max.label}
diff --git a/src/components/forms/__test__/SliderWithLabelForm.test.tsx b/src/components/forms/__test__/SliderWithLabelForm.test.tsx new file mode 100644 index 0000000..cc4feea --- /dev/null +++ b/src/components/forms/__test__/SliderWithLabelForm.test.tsx @@ -0,0 +1,50 @@ +import { SetStateAction } from "react"; + +import { FormProps, FormState, Option } from "../FormRenderer"; +import { SliderWithLabelForm } from "../SliderWithLabelForm"; +import { render, screen } from "@testing-library/react"; + +describe("", () => { + const formState = { + "test-question-slider": [{ value: 2 }], + }; + const setFormState = jest.fn(); + + const options = [ + { label: "label1", value: 1 }, + { label: "label2", value: 2 }, + { label: "label3", value: 3 }, + ]; + + beforeAll(() => { + global.ResizeObserver = class ResizeObserver { + observe() {} + unobserve() {} + disconnect() {} + }; + }); + + beforeEach(() => { + render( + , + ); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + test("should render Container properly", () => { + expect(screen.getByTestId("slider-with-label-form")).toBeInTheDocument(); + }); + + test("should render min, max label properly", () => { + expect(screen.getByText("label1")).toBeInTheDocument(); + expect(screen.getByText("label3")).toBeInTheDocument(); + }); +});