Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/714 add test slider component #806

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion slash/react/src/Form/Slider/SliderInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ const SliderInput = ({
...sliderProps
}: Props) => {
const generatedId = useId();
const newId = useMemo(() => id ?? generatedId, [generatedId, id]);
const newId = useMemo(() => {
if (id && id !== "") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

En faisant simplement ça il va prendre en compte la valeur null, undefined et empty string

Suggested change
if (id && id !== "") {
if (id) {

return id;
}
return generatedId;
}, [generatedId, id]);

return (
<Field
Expand Down
244 changes: 244 additions & 0 deletions slash/react/src/Form/Slider/__tests__/Slider.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
import { render, screen } from "@testing-library/react";
import { axe } from "jest-axe";
import userEvent, { UserEvent } from "@testing-library/user-event";
import { Slider } from "../Slider";

const options = [
{ label: "64", value: 64 },
{ label: "128", value: 128 },
{ label: "256", value: 256 },
{ label: "1024", value: 1024 },
{ label: "2048", value: 2048 },
{ label: "4096", value: 4096 },
];

const optionsWithoutLabel = [
{ value: 64 },
{ value: 128 },
{ value: 256 },
{ value: 1024 },
{ value: 2048 },
{ value: 4096 },
];

const sliderIsActive = (value: string) => {
expect(screen.getByText(value)).toHaveClass(
"rc-slider-mark-text rc-slider-mark-text-active",
{
exact: true,
},
);
};

const sliderIsNotActive = (value: string) => {
expect(screen.getByText(value)).toHaveClass("rc-slider-mark-text", {
exact: true,
});
};

describe("Slider", () => {
let user: UserEvent;
beforeEach(() => {
user = userEvent.setup();
});
Comment on lines +40 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Le userEvent.setup n'est pas forcément nécessaire je pense


it("should value 64 have active class", () => {
let valueSlider: number = 64;
// Act
render(
<Slider
id="slider-id"
name="slider-name"
options={options}
value={valueSlider}
onChange={({ value }) => {
valueSlider = value;
}}
/>,
);

// Assert
sliderIsActive("64");
sliderIsNotActive("128");
sliderIsNotActive("256");
sliderIsNotActive("1024");
sliderIsNotActive("2048");
sliderIsNotActive("4096");
});

it("should have default value 128", () => {
let valueSlider: number = 256;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

la value est 256 et non 128 ?

// Act
render(
<Slider
id="slider-id"
name="slider-name"
options={options}
defaultValue={valueSlider}
onChange={({ value }) => {
valueSlider = value;
}}
/>,
);

// Assert
sliderIsActive("64");
sliderIsActive("128");
sliderIsActive("256");
sliderIsNotActive("1024");
sliderIsNotActive("2048");
sliderIsNotActive("4096");
});

it("should have active class until 2048 value", async () => {
// Act
render(
<Slider
id="slider-id"
name="slider-name"
options={options}
onChange={() => console.log("some change")}
/>,
);

const selectValueSlider = screen.getByText("1024");
await user.click(selectValueSlider);

// Assert
sliderIsActive("64");
sliderIsActive("128");
sliderIsActive("256");
sliderIsActive("1024");
sliderIsNotActive("2048");
sliderIsNotActive("4096");
});
Comment on lines +93 to +114
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

donc par défaut c'est actif jusque 2048 sans avoir de value de définit ?


it("should be disabled with classModifier", async () => {
// Act
render(
<Slider
id="slider-id"
name="slider-name"
value={256}
options={options}
onChange={() => console.log("some change")}
classModifier="disabled"
/>,
);

const selectValueSlider = screen.getByText("1024");
await user.click(selectValueSlider);

// Assert
sliderIsActive("64");
sliderIsActive("128");
sliderIsActive("256");
sliderIsNotActive("1024");
sliderIsNotActive("2048");
sliderIsNotActive("4096");
});
Comment on lines +116 to +139
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quand c'est disabled on est pas censé pouvoir bouger le curseur si ?


it("should be disabled with props", async () => {
// Act
render(
<Slider
id="slider-id"
name="slider-name"
value={256}
options={options}
onChange={() => console.log("some change")}
disabled
/>,
);

const selectValueSlider = screen.getByText("1024");
await user.click(selectValueSlider);

// Assert
sliderIsActive("64");
sliderIsActive("128");
sliderIsActive("256");
sliderIsNotActive("1024");
sliderIsNotActive("2048");
sliderIsNotActive("4096");
});
Comment on lines +141 to +164
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quand c'est disabled on est pas censé pouvoir bouger le curseur si ?


it("should have aria label handle", async () => {
// Act
render(
<Slider
id="slider-id"
name="slider-name"
defaultValue={256}
ariaLabelForHandle="aria-label-handle"
options={options}
onChange={() => console.log("some change")}
/>,
);

// Assert
const ariaLabelHandle = screen.getByLabelText("aria-label-handle");
expect(ariaLabelHandle).toBeInTheDocument();
});

it("should have options without label", async () => {
// Act
render(
<Slider
id="slider-id"
name="slider-name"
options={optionsWithoutLabel}
onChange={() => console.log("some change")}
/>,
);

const selectValueSlider = screen.getByText("1024");
await user.click(selectValueSlider);

// Assert
sliderIsActive("64");
sliderIsActive("128");
sliderIsActive("256");
sliderIsActive("1024");
sliderIsNotActive("2048");
sliderIsNotActive("4096");
});
Comment on lines +184 to +205
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ça ne teste rien de plus que d'autres tests au dessus non ?


it("should have Slider with onChangeComplet", async () => {
// Act
const { container } = render(
<Slider
id="slider-id"
name="slider-name"
ariaLabelForHandle="slider-label"
options={optionsWithoutLabel}
onChangeComplete={(selectedValue: {
id: string;
name: string;
value: number;
}) => {
console.log(selectedValue);
}}
/>,
);

// Assert
expect(await axe(container)).toHaveNoViolations();
});
Comment on lines +207 to +227
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c'est un test d'accessibilité


it("shouldn't have an accesibility violation <Slider />", async () => {
// Act
const { container } = render(
<Slider
id="slider-id"
name="slider-name"
ariaLabelForHandle="slider-label"
options={optionsWithoutLabel}
onChange={() => console.log("some change")}
/>,
);

// Assert
expect(await axe(container)).toHaveNoViolations();
});
});
Loading
Loading