Skip to content

Commit a2f7e03

Browse files
Issue #36 Backfill tests for Molecules/LinkCard
Add tests for molecules linkcard
2 parents fb11025 + 244b059 commit a2f7e03

File tree

3 files changed

+95
-3
lines changed

3 files changed

+95
-3
lines changed
Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,59 @@
1+
import React from "react";
2+
import faker from "faker";
3+
import { LinkCard, LinkCardIconClassName } from "./link-card";
4+
import { TestUtils } from "../../tests/test-utils";
5+
import { LinkCardTypes } from "../constants/link-card-types";
6+
17
describe("LinkCard", () => {
2-
test.skip("TODO - https://github.com/AndcultureCode/AndcultureCode.JavaScript.React.Components/issues/36", () => {});
8+
test("when default props, renders LinkCard", () => {
9+
// Arrange
10+
const expected = faker.random.words();
11+
const label = faker.random.words();
12+
13+
// Act
14+
const { getByText } = TestUtils.renderWithRouter(
15+
<LinkCard label={label}>{expected}</LinkCard>
16+
);
17+
18+
//Assert
19+
expect(getByText(expected)).not.toBeNil();
20+
});
21+
22+
test(`when type prop is button, renders ${LinkCardTypes.Button}`, () => {
23+
// Arrange
24+
const expected = faker.random.words();
25+
const label = faker.random.words();
26+
const type = LinkCardTypes.Button;
27+
28+
// Act
29+
const { container } = TestUtils.renderWithRouter(
30+
<LinkCard label={label} type={type}>
31+
{expected}
32+
</LinkCard>
33+
);
34+
const result = container.getElementsByTagName("button")[0];
35+
36+
// Assert
37+
expect(result).not.toBeNil();
38+
});
39+
40+
test(`when default props and include icon, renders with class name ${LinkCardIconClassName}`, () => {
41+
// Arrange
42+
const expected = faker.random.words();
43+
const label = faker.random.words();
44+
45+
// Act
46+
const { container } = TestUtils.renderWithRouter(
47+
<LinkCard label={label} includeIcon={true}>
48+
{expected}
49+
</LinkCard>
50+
);
51+
const result = container.getElementsByClassName(
52+
LinkCardIconClassName
53+
)[0];
54+
55+
// Assert
56+
expect(result).not.toBeNil();
57+
expect(result.classList).toContain(LinkCardIconClassName);
58+
});
359
});

src/molecules/link-card/link-card.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { ParagraphSizes } from "../../atoms/constants/paragraph-sizes";
1313
// -------------------------------------------------------------------------------------------------
1414

1515
const COMPONENT_CLASS = "c-link-card";
16+
export const LinkCardIconClassName = "-with-icon";
1617

1718
// #endregion Constants
1819

@@ -39,12 +40,13 @@ export interface LinkCardProps {
3940
const LinkCard: React.FC<LinkCardProps> = (props: LinkCardProps) => {
4041
const cssClassNames = [COMPONENT_CLASS];
4142
if (props.includeIcon) {
42-
cssClassNames.push("-with-icon");
43+
cssClassNames.push(LinkCardIconClassName);
4344
}
4445

4546
const cssClassNamesFlat = cssClassNames.join(" ");
4647
const iconType = props.iconType ?? Icons.Lightbulb;
4748
const type = props.type ?? LinkCardTypes.Link;
49+
const to = props.to ?? "#";
4850

4951
const renderChildren = () => (
5052
<React.Fragment>
@@ -78,7 +80,7 @@ const LinkCard: React.FC<LinkCardProps> = (props: LinkCardProps) => {
7880
)}
7981
{// if
8082
type === LinkCardTypes.Link && (
81-
<Anchor cssClassName={cssClassNamesFlat} to={props.to}>
83+
<Anchor cssClassName={cssClassNamesFlat} to={to}>
8284
{renderChildren()}
8385
</Anchor>
8486
)}

src/tests/test-utils.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { MemoryRouter } from "react-router-dom";
2+
import { render } from "@testing-library/react";
3+
import { ReactElement } from "react";
4+
5+
// -----------------------------------------------------------------------------------------
6+
// #region Functions
7+
// -----------------------------------------------------------------------------------------
8+
9+
/**
10+
* Returns a React component wrapped in a MemoryRouter. Used for Components that
11+
* must be rendered inside a Route Component.
12+
*
13+
* @param component
14+
* @param route
15+
* @returns {ReactComponent}
16+
*/
17+
18+
const _renderWithRouter = (component: ReactElement, route: string = "/") => {
19+
window.history.pushState({}, "Test page", route);
20+
21+
return render(component, { wrapper: MemoryRouter });
22+
};
23+
24+
// #endregion Functions
25+
26+
// -----------------------------------------------------------------------------------------
27+
// #region Exports
28+
// -----------------------------------------------------------------------------------------
29+
30+
export const TestUtils = {
31+
renderWithRouter: _renderWithRouter,
32+
};
33+
34+
// #endregion Exports

0 commit comments

Comments
 (0)