Skip to content

Commit

Permalink
refactor(fieldset): convert enzyme tests to RTL
Browse files Browse the repository at this point in the history
  • Loading branch information
nuria1110 committed Sep 17, 2024
1 parent bf4ef68 commit 37f407f
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 225 deletions.
225 changes: 0 additions & 225 deletions src/__internal__/fieldset/fieldset.spec.tsx

This file was deleted.

123 changes: 123 additions & 0 deletions src/__internal__/fieldset/fieldset.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import React from "react";
import { render, screen, within } from "@testing-library/react";
import Fieldset from ".";
import { testStyledSystemMargin } from "../../__spec_helper__/__internal__/test-utils";

test("renders with provided `children`", () => {
render(
<Fieldset>
<input />
</Fieldset>
);

const input = within(screen.getByRole("group")).getByRole("textbox");

expect(input).toBeVisible();
});

test("renders fieldset with provided `legend`", () => {
render(
<Fieldset legend="Legend">
<input />
</Fieldset>
);

const fieldset = screen.getByRole("group", { name: "Legend" });

expect(fieldset).toBeVisible();
});

test("sets child inputs as required when `isRequired` is true", () => {
render(
<Fieldset isRequired>
<input />
<input />
</Fieldset>
);

const inputs = screen.getAllByRole("textbox");

expect(inputs[0]).toBeRequired();
expect(inputs[1]).toBeRequired();
});

test("renders validation icon when `legend` and `error` are provided", () => {
render(
<Fieldset legend="Legend" error="error">
<input />
</Fieldset>
);

const icon = screen.getByTestId("icon-error");

expect(icon).toBeVisible();
});

test("renders validation icon when `legend` and `warning` are provided", () => {
render(
<Fieldset legend="Legend" warning="warning">
<input />
</Fieldset>
);

const icon = screen.getByTestId("icon-warning");

expect(icon).toBeVisible();
});

test("renders validation icon when `legend` and `info` are provided", () => {
render(
<Fieldset legend="Legend" info="info">
<input />
</Fieldset>
);

const icon = screen.getByTestId("icon-info");

expect(icon).toBeVisible();
});

// coverage
test("renders legend with provided `legendWidth` when `inline` is true", () => {
render(
<Fieldset legend="Legend" inline legendWidth={30}>
<input />
</Fieldset>
);

const legend = screen.getByTestId("legend");

expect(legend).toHaveStyle({ width: "30%" });
});

// coverage
test("renders with expected styles when `inline` is true and `align` is 'left'", () => {
render(
<Fieldset legend="Legend" inline legendAlign="left">
<input />
</Fieldset>
);

const legend = screen.getByTestId("legend");

expect(legend).toHaveStyle({ justifyContent: "flex-start" });
});

// coverage
test("renders with expected padding when `inline` is true and `legendSpacing` is 1", () => {
render(
<Fieldset legend="Legend" inline legendSpacing={1}>
<input />
</Fieldset>
);

const legend = screen.getByTestId("legend");

expect(legend).toHaveStyleRule("padding-right", "var(--spacing100)");
});

testStyledSystemMargin((props) => (
<Fieldset {...props}>
<input />
</Fieldset>
));
13 changes: 13 additions & 0 deletions src/__internal__/label/label.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,16 @@ test.each(["error", "warning", "info"])(
);
}
);

// coverage
test("renders with expected styles when `inline` is true and `align` is 'left'", () => {
render(
<Label inline align="left">
foo
</Label>
);

expect(screen.getByTestId("label-container")).toHaveStyle({
justifyContent: "flex-start",
});
});

0 comments on commit 37f407f

Please sign in to comment.