Skip to content

Commit

Permalink
Merge pull request #6979 from Sage/FE-6804-detail-rtl
Browse files Browse the repository at this point in the history
refactor(detail): convert enzyme tests to RTL
  • Loading branch information
nuria1110 authored Sep 25, 2024
2 parents 3e7c116 + f7e770f commit 5a7f30a
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 120 deletions.
2 changes: 1 addition & 1 deletion src/__internal__/utils/helpers/tags/tags-specs/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { elementsTagTest, rootTagTest } from "./tags-specs";
export { default } from "./tags-specs";
15 changes: 3 additions & 12 deletions src/__internal__/utils/helpers/tags/tags-specs/tags-specs.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import { ReactWrapper, ShallowWrapper } from "enzyme";

export const elementsTagTest = (
wrapper: ReactWrapper | ShallowWrapper,
elements: string[]
) => {
elements.forEach((element) => {
it(`include 'data-element="${element}"'`, () => {
expect(wrapper.find({ "data-element": element }).length).toEqual(1);
});
});
};

export const rootTagTest = (
const rootTagTest = (
rootNode: ReactWrapper | ShallowWrapper,
comp: string,
elem?: string,
Expand All @@ -21,3 +10,5 @@ export const rootTagTest = (
expect(rootNode.prop("data-element")).toEqual(elem);
expect(rootNode.prop("data-role")).toEqual(role);
};

export default rootTagTest;
6 changes: 5 additions & 1 deletion src/components/detail/detail.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ export const Detail = ({
</StyledDetailContent>

{footnote && (
<StyledDetailFootnote data-element="footnote" hasIcon={!!icon}>
<StyledDetailFootnote
data-element="footnote"
data-role="footnote"
hasIcon={!!icon}
>
{footnote}
</StyledDetailFootnote>
)}
Expand Down
101 changes: 0 additions & 101 deletions src/components/detail/detail.spec.tsx

This file was deleted.

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

test("renders children", () => {
render(<Detail>foo</Detail>);

expect(screen.getByText("foo")).toBeVisible();
});

test("renders with provided `footnote`", () => {
render(<Detail footnote="extra info">foo</Detail>);

const footnoteText = within(screen.getByTestId("footnote")).getByText(
"extra info"
);

expect(footnoteText).toBeVisible();
});

test("renders with provided `icon`", () => {
render(<Detail icon="bin">foo</Detail>);

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

expect(icon).toBeVisible();
expect(icon).toHaveAttribute("type", "bin");
});

test("renders with provided data- tags", () => {
render(
<Detail data-element="bar" data-role="baz">
foo
</Detail>
);

expect(screen.getByTestId("baz")).toHaveAttribute("data-element", "bar");
});

// coverage
test("renders with expected styles when `icon` and `footnote` are passed", () => {
render(
<Detail icon="bin" footnote="extra info">
foo
</Detail>
);

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

expect(footnote).toHaveStyle("margin-left: 26px");
});

testStyledSystemMargin((props) => <Detail {...props}>foo</Detail>);
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Icon from "../icon";
import guid from "../../__internal__/utils/helpers/guid";
import { Select, Option } from "../select";
import useMediaQuery from "../../hooks/useMediaQuery";
import { rootTagTest } from "../../__internal__/utils/helpers/tags/tags-specs";
import rootTagTest from "../../__internal__/utils/helpers/tags/tags-specs";

jest.mock("../../hooks/useMediaQuery", () => {
return {
Expand Down
2 changes: 1 addition & 1 deletion src/components/tabs/tabs.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { act } from "react-dom/test-utils";
import TabTitle from "./__internal__/tab-title/tab-title.component";
import { Tabs, Tab, TabsProps } from ".";
import TabContext from "./tab/__internal__/tab.context";
import { rootTagTest } from "../../__internal__/utils/helpers/tags/tags-specs";
import rootTagTest from "../../__internal__/utils/helpers/tags/tags-specs";
import StyledTabs, { StyledTabsProps } from "./tabs.style";
import StyledTab from "./tab/tab.style";
import { StyledTabTitleButton } from "./__internal__/tab-title/tab-title.style";
Expand Down
2 changes: 1 addition & 1 deletion src/components/tile/tile-footer/tile-footer.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
testStyledSystemPadding,
} from "../../../__spec_helper__/__internal__/test-utils";
import StyledTileFooter from "./tile-footer.style";
import { rootTagTest } from "../../../__internal__/utils/helpers/tags/tags-specs";
import rootTagTest from "../../../__internal__/utils/helpers/tags/tags-specs";

describe("TileFooter", () => {
let wrapper: ReactWrapper;
Expand Down
2 changes: 1 addition & 1 deletion src/components/tile/tile-header/tile-header.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
testStyledSystemPadding,
} from "../../../__spec_helper__/__internal__/test-utils";
import StyledTileHeader from "./tile-header.style";
import { rootTagTest } from "../../../__internal__/utils/helpers/tags/tags-specs";
import rootTagTest from "../../../__internal__/utils/helpers/tags/tags-specs";

describe("TileHeader", () => {
let wrapper: ReactWrapper;
Expand Down
2 changes: 1 addition & 1 deletion src/components/tile/tile.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
testStyledSystemHeight,
} from "../../__spec_helper__/__internal__/test-utils";
import { TileProps } from "./tile.component";
import { rootTagTest } from "../../__internal__/utils/helpers/tags/tags-specs";
import rootTagTest from "../../__internal__/utils/helpers/tags/tags-specs";
import StyledTile from "./tile.style";
import { TILE_HIGHLIGHT_VARIANTS } from "./tile.config";

Expand Down

0 comments on commit 5a7f30a

Please sign in to comment.