Skip to content

Commit

Permalink
Merge pull request #6962 from Sage/FE-6803-fieldset-formfield-rtl
Browse files Browse the repository at this point in the history
refactor(form-field, fieldset): convert enzyme tests to RTL
  • Loading branch information
nuria1110 committed Sep 20, 2024
2 parents 600ac5f + 36d72ca commit 44d8cff
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 509 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>
));
2 changes: 1 addition & 1 deletion src/__internal__/form-field/form-field.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ const FormField = ({

return (
<FormFieldStyle {...tagComponent(dataComponent, rest)} {...marginProps}>
<FieldLineStyle inline={inlineLabel}>
<FieldLineStyle data-role="field-line" inline={inlineLabel}>
{reverse && children}

{label && (
Expand Down
Loading

0 comments on commit 44d8cff

Please sign in to comment.