Skip to content

Commit

Permalink
Merge pull request #7009 from Sage/FE-6636
Browse files Browse the repository at this point in the history
refactor(tile): convert tests to RTL
  • Loading branch information
nineteen88 authored Oct 15, 2024
2 parents 96d41f2 + 8dc5a7c commit 8efe1aa
Show file tree
Hide file tree
Showing 9 changed files with 330 additions and 556 deletions.
121 changes: 0 additions & 121 deletions src/__spec_helper__/__internal__/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,123 +338,6 @@ const testStyledSystemMargin = (
);
};

const testStyledSystemPadding = (
component: (spacingProps?: PaddingProps) => JSX.Element,
defaults?: PaddingProps,
styleContainer?: (wrapper: ReactWrapper) => ReactWrapper,
assertOpts?: jest.Options
) => {
describe("default props", () => {
let wrapper: ReactWrapper;
let StyleElement: ReactWrapper;

beforeAll(() => {
wrapper = mount(component({ ...defaults }));
StyleElement = styleContainer ? styleContainer(wrapper) : wrapper;
});

it("should set the correct paddings", () => {
let padding;
let paddingLeft;
let paddingRight;
let paddingTop;
let paddingBottom;

if (defaults) {
padding = getDefaultValue(defaults.p);
paddingLeft = getDefaultValue(defaults.pl || defaults.px);
paddingRight = getDefaultValue(defaults.pr || defaults.px);
paddingTop = getDefaultValue(defaults.pt || defaults.py);
paddingBottom = getDefaultValue(defaults.pb || defaults.py);

assertStyleMatch(
{
padding,
paddingLeft,
paddingRight,
paddingTop,
paddingBottom,
},
StyleElement,
assertOpts
);
} else {
expect(StyleElement).not.toHaveStyleRule("paddingLeft");
expect(StyleElement).not.toHaveStyleRule("paddingRight");
expect(StyleElement).not.toHaveStyleRule("paddingTop");
expect(StyleElement).not.toHaveStyleRule("paddingBottom");
expect(StyleElement).not.toHaveStyleRule("padding");
}
});
});

describe.each(paddingProps)(
'when a custom spacing is specified using the "%s" styled system props',
(styledSystemProp, propName) => {
it(`then that ${propName} should have been set correctly`, () => {
const props = { [styledSystemProp]: 2 };
const wrapper = mount(component({ ...props }));

assertStyleMatch(
{ [propName]: "var(--spacing200)" },
styleContainer ? styleContainer(wrapper) : wrapper,
assertOpts
);
});
}
);
};

const testStyledSystemSpacing = (
component: (spacingProps?: MarginProps | PaddingProps) => JSX.Element,
defaults?: MarginProps | PaddingProps,
styleContainer?: (wrapper: ReactWrapper) => ReactWrapper,
assertOpts?: jest.Options
) => {
testStyledSystemMargin(
component,
defaults as MarginProps,
styleContainer,
assertOpts
);
testStyledSystemPadding(
component,
defaults as PaddingProps,
styleContainer,
assertOpts
);
};

const testStyledSystemWidth = (
component: (widthProperties?: { width: string }) => JSX.Element,
styleContainer?: (wrapper: ReactWrapper) => ReactWrapper
) => {
describe("when a width prop is specified using styled system props", () => {
it("then width should have been set correctly", () => {
const [styledSystemProp, propName, value] = widthProps;
const props = { [styledSystemProp]: value };
const wrapper = mount(component({ ...props }));
const StyleElement = styleContainer ? styleContainer(wrapper) : wrapper;
expect(StyleElement).toHaveStyleRule(propName, value);
});
});
};

const testStyledSystemHeight = (
component: (heightProperties?: { height: string }) => JSX.Element,
styleContainer?: (wrapper: ReactWrapper) => ReactWrapper
) => {
describe("when a height prop is specified using styled system props", () => {
it("then height should have been set correctly", () => {
const [styledSystemProp, propName, value] = heightProps;
const props = { [styledSystemProp]: value };
const wrapper = mount(component({ ...props }));
const StyleElement = styleContainer ? styleContainer(wrapper) : wrapper;
expect(StyleElement).toHaveStyleRule(propName, value);
});
});
};

const testStyledSystemGrid = (
component: (gridProperties?: GridProps) => JSX.Element,
elementQuery: () => HTMLElement
Expand Down Expand Up @@ -795,12 +678,8 @@ export {
click,
simulate,
mockMatchMedia,
testStyledSystemSpacing,
testStyledSystemMargin,
testStyledSystemPadding,
testStyledSystemColor,
testStyledSystemWidth,
testStyledSystemHeight,
testStyledSystemLayout,
testStyledSystemFlexBox,
testStyledSystemGrid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,3 @@ test("should render when children are passed", () => {

expect(screen.getByText("Tile Content")).toBeVisible();
});

test("should have `overflow` 'hidden' by default", () => {
render(<FlexTileContainer>Tile Content</FlexTileContainer>);

expect(screen.getByText("Tile Content")).toHaveStyle({
overflow: "hidden",
});
});

test("should render with the expected `overflow` styling when prop is passed", () => {
render(
<FlexTileContainer overflow="visible">Tile Content</FlexTileContainer>
);

expect(screen.getByText("Tile Content")).toHaveStyle({
overflow: "visible",
});
});
174 changes: 47 additions & 127 deletions src/components/tile/tile-content/tile-content.test.tsx
Original file line number Diff line number Diff line change
@@ -1,143 +1,63 @@
import React from "react";
import { render, screen } from "@testing-library/react";
import { PaddingProps } from "styled-system";
import TileContent from "./tile-content.component";
import {
testStyledSystemHeightRTL,
testStyledSystemSpacingRTL,
testStyledSystemWidthRTL,
} from "../../../__spec_helper__/__internal__/test-utils";
import TileContext from "../__internal__/tile.context";

describe("TileContent", () => {
testStyledSystemSpacingRTL(
(props) => (
<TileContent data-role="tile-content" {...props}>
Test
</TileContent>
),
() => screen.getByTestId("tile-content")
);
testStyledSystemWidthRTL(
(props) => (
<TileContent data-role="tile-content" {...props}>
Test
</TileContent>
),
() => screen.getByTestId("tile-content")
);
testStyledSystemHeightRTL(
(props) => (
<TileContent data-role="tile-content" {...props}>
Test
</TileContent>
),
() => screen.getByTestId("tile-content")
);

it("does not render when no children are passed", () => {
render(<TileContent data-role="tile-content" />);

expect(screen.queryByTestId("tile-content")).not.toBeInTheDocument();
});

it("does not render when falsy children are passed", () => {
render(<TileContent data-role="tile-content">{null}</TileContent>);

expect(screen.queryByTestId("tile-content")).not.toBeInTheDocument();
});

it("renders when children are passed", () => {
render(<TileContent>Tile Content</TileContent>);

expect(screen.getByText("Tile Content")).toBeVisible();
});

it.each([
[{}, "var(--spacing300)", undefined],
[{ p: 2 }, "var(--spacing200)", undefined],
[{ pl: 2, p: 1 }, "var(--spacing200)", "var(--spacing100)"],
[{ pr: 2, p: 1 }, "var(--spacing100)", "var(--spacing200)"],
[{ px: 2, p: 1 }, "var(--spacing200)", undefined],
] as [PaddingProps, string, string | undefined][])(
"applies the expected padding values when %s passed and in horizontal orientation",
(paddingPropsFromTile, leftValue, rightValue) => {
render(
<>
<TileContext.Provider
value={{ isHorizontal: true, paddingPropsFromTile }}
>
<TileContent>Tile Content 1</TileContent>
<TileContent>Tile Content 2</TileContent>
<TileContent>Tile Content 3</TileContent>
</TileContext.Provider>
</>
);

expect(screen.getByText("Tile Content 1")).toHaveStyle({
"padding-left": "0px",
"padding-right": rightValue || leftValue,
});

expect(screen.getByText("Tile Content 2")).toHaveStyle({
"padding-left": leftValue,
"padding-right": rightValue || leftValue,
});
testStyledSystemSpacingRTL(
(props) => (
<TileContent data-role="tile-content" {...props}>
Test
</TileContent>
),
() => screen.getByTestId("tile-content")
);
testStyledSystemWidthRTL(
(props) => (
<TileContent data-role="tile-content" {...props}>
Test
</TileContent>
),
() => screen.getByTestId("tile-content")
);
testStyledSystemHeightRTL(
(props) => (
<TileContent data-role="tile-content" {...props}>
Test
</TileContent>
),
() => screen.getByTestId("tile-content")
);

test("does not render when no children are passed", () => {
render(<TileContent data-role="tile-content" />);

expect(screen.queryByTestId("tile-content")).not.toBeInTheDocument();
});

expect(screen.getByText("Tile Content 3")).toHaveStyle({
"padding-left": leftValue,
"padding-right": "0px",
});
}
);
test("does not render when falsy children are passed", () => {
render(<TileContent data-role="tile-content">{null}</TileContent>);

it.each([
[{}, "var(--spacing300)", undefined],
[{ p: 2 }, "var(--spacing200)", undefined],
[{ pt: 2, p: 1 }, "var(--spacing200)", "var(--spacing100)"],
[{ pb: 2, p: 1 }, "var(--spacing100)", "var(--spacing200)"],
[{ py: 2, p: 1 }, "var(--spacing200)", undefined],
] as [PaddingProps, string, string | undefined][])(
"applies the expected padding values when %s passed and in vertical orientation",
(paddingPropsFromTile, topValue, bottomValue) => {
render(
<>
<TileContext.Provider
value={{ isHorizontal: false, paddingPropsFromTile }}
>
<TileContent>Tile Content 1</TileContent>
<TileContent>Tile Content 2</TileContent>
<TileContent>Tile Content 3</TileContent>
</TileContext.Provider>
</>
);
expect(screen.queryByTestId("tile-content")).not.toBeInTheDocument();
});

expect(screen.getByText("Tile Content 1")).toHaveStyle({
"padding-top": "0px",
"padding-bottom": bottomValue || topValue,
});
test("renders when children are passed", () => {
render(<TileContent>Tile Content</TileContent>);

expect(screen.getByText("Tile Content 2")).toHaveStyle({
"padding-top": topValue,
"padding-bottom": bottomValue || topValue,
});
expect(screen.getByText("Tile Content")).toBeVisible();
});

expect(screen.getByText("Tile Content 3")).toHaveStyle({
"padding-top": topValue,
"padding-bottom": "0px",
});
}
test("has proper data attributes applied", () => {
render(
<TileContent data-element="foo" data-role="bar">
Tile Content
</TileContent>
);

it("has proper data attributes applied", () => {
render(
<TileContent data-element="foo" data-role="bar">
Tile Content
</TileContent>
);
const element = screen.getByText("Tile Content");
expect(element).toHaveAttribute("data-component", "tile-content");
expect(element).toHaveAttribute("data-element", "foo");
expect(element).toHaveAttribute("data-role", "bar");
});
const element = screen.getByText("Tile Content");
expect(element).toHaveAttribute("data-component", "tile-content");
expect(element).toHaveAttribute("data-element", "foo");
expect(element).toHaveAttribute("data-role", "bar");
});
44 changes: 0 additions & 44 deletions src/components/tile/tile-footer/tile-footer.spec.tsx

This file was deleted.

Loading

0 comments on commit 8efe1aa

Please sign in to comment.