Skip to content

Commit f83b0f1

Browse files
Merge pull request #55 from SaidShah/add-tests-for-molecules-radiolist
added tests for radio list
2 parents e77c8b6 + e9b4d7b commit f83b0f1

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

src/molecules/lists/radio-list.test.tsx

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import React from "react";
22
import { render } from "@testing-library/react";
3-
import { RadioList } from "./radio-list";
3+
import {
4+
RadioList,
5+
RadioListClassName,
6+
RadioListButtonStyleClassName,
7+
RadioListStyles,
8+
} from "./radio-list";
49
import faker from "faker";
510

611
describe("RadioList", () => {
@@ -16,4 +21,32 @@ describe("RadioList", () => {
1621
// Assert
1722
expect(getByText(expected)).not.toBeNull();
1823
});
24+
25+
test("when items prop is empty, radio-list returns null", () => {
26+
// Arrange & Act
27+
const { container } = render(<RadioList items={[]} />);
28+
const result = container.querySelector("." + RadioListClassName);
29+
30+
// Assert
31+
expect(result).toBeNil();
32+
});
33+
34+
test(`when type prop is ${RadioListStyles.Button}, renders with class name ${RadioListButtonStyleClassName}`, () => {
35+
// Arrange
36+
const expected = faker.random.words();
37+
38+
// Act
39+
const { container } = render(
40+
<RadioList
41+
items={[<span>{expected}</span>]}
42+
style={RadioListStyles.Button}
43+
/>
44+
);
45+
const result = container.querySelector(
46+
"." + RadioListButtonStyleClassName
47+
);
48+
49+
// Assert
50+
expect(result).not.toBeNil();
51+
});
1952
});

src/molecules/lists/radio-list.tsx

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,51 @@
11
import * as React from "react";
22
import { UnorderedList } from "./unordered-list";
33

4-
const COMPONENT_CLASS = "c-radio-list";
4+
// -----------------------------------------------------------------------------------------
5+
// #region Constants
6+
// -----------------------------------------------------------------------------------------
7+
8+
export const RadioListClassName = "c-radio-list";
9+
export const RadioListButtonStyleClassName = "-button-style";
10+
11+
// #endregion Constants
12+
13+
// -----------------------------------------------------------------------------------------
14+
// #region Interfaces
15+
// -----------------------------------------------------------------------------------------
516

617
export interface RadioListProps {
718
items: JSX.Element[];
819
style?: RadioListStyles;
920
}
1021

22+
// #endregion Interfaces
23+
24+
// -----------------------------------------------------------------------------------------
25+
// #region Enums
26+
// -----------------------------------------------------------------------------------------
27+
1128
export enum RadioListStyles {
1229
Default = "default",
1330
Button = "button",
1431
}
1532

33+
// #endregion Enums
34+
35+
// -----------------------------------------------------------------------------------------
36+
// #region Component
37+
// -----------------------------------------------------------------------------------------
38+
1639
const RadioList: React.FunctionComponent<RadioListProps> = (props) => {
1740
const { items, style } = props;
1841

1942
if (items.length === 0) {
2043
return null;
2144
}
2245

23-
const classNames = [COMPONENT_CLASS];
46+
const classNames = [RadioListClassName];
2447
if (style === RadioListStyles.Button) {
25-
classNames.push("-button-style");
48+
classNames.push(RadioListButtonStyleClassName);
2649
}
2750

2851
return (
@@ -32,4 +55,12 @@ const RadioList: React.FunctionComponent<RadioListProps> = (props) => {
3255
);
3356
};
3457

58+
// #endregion Component
59+
60+
// -----------------------------------------------------------------------------------------
61+
// #region Exports
62+
// -----------------------------------------------------------------------------------------
63+
3564
export { RadioList };
65+
66+
// #endregion Exports

0 commit comments

Comments
 (0)