Skip to content

Commit 9377511

Browse files
committed
Tests for Button atom component
1 parent cabda79 commit 9377511

2 files changed

Lines changed: 72 additions & 9 deletions

File tree

src/atoms/buttons/button.test.tsx

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import React from "react";
2+
import { act, render, wait, fireEvent } from "@testing-library/react";
3+
import { Button } from "./button";
4+
import faker from "faker";
5+
import { MemoryRouter } from "react-router-dom";
6+
import { ButtonSizes } from "../constants/button-sizes";
7+
import { ButtonStyles } from "../constants/button-styles";
8+
9+
describe("button", () => {
10+
it("when default props, renders button children", async () => {
11+
// Arrange
12+
const expected = faker.random.words();
13+
14+
// Act
15+
const { getByText } = render(<Button>{expected}</Button>);
16+
17+
// Assert
18+
await wait(() => {
19+
expect(getByText(expected)).not.toBeNull();
20+
});
21+
});
22+
23+
it("when cssClassName provided, adds provided className to button", () => {
24+
// Arrange
25+
const expected = faker.random.word();
26+
27+
// Act
28+
const { container } = render(<Button cssClassName={expected}></Button>);
29+
30+
// Assert
31+
expect(container.firstChild.className).toContain(expected);
32+
});
33+
34+
it("when size provided, adds size className to button", () => {
35+
// Arrange
36+
const expected = ButtonSizes.Large;
37+
38+
// Act
39+
const { container } = render(<Button size={expected}></Button>);
40+
41+
// Assert
42+
expect(container.firstChild.className).toContain(`-${expected}`);
43+
});
44+
45+
it(`when style of '${ButtonStyles.None}', has className of only 'none'`, () => {
46+
// Arrange
47+
const expected = ButtonStyles.None;
48+
49+
// Act
50+
const { container } = render(<Button style={expected}></Button>);
51+
52+
// Assert
53+
expect(container.firstChild.className).toBe(`-${expected}`);
54+
});
55+
56+
it("when style provided, adds style className to button", () => {
57+
// Arrange
58+
const expected = ButtonStyles.Destructive;
59+
60+
// Act
61+
const { container } = render(<Button style={expected}></Button>);
62+
63+
// Assert
64+
expect(container.firstChild.className).toContain(`-${expected}`);
65+
});
66+
});

src/atoms/buttons/button.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ButtonSizes } from "../constants/button-sizes";
22
import { ButtonStyles } from "../constants/button-styles";
33
import { ButtonTypes } from "../constants/button-types";
44
import React, { forwardRef } from "react";
5+
import { StringUtils } from "andculturecode-javascript-core";
56

67
// -----------------------------------------------------------------------------------------
78
// #region Interfaces
@@ -54,22 +55,18 @@ const Button: React.RefForwardingComponent<
5455
value,
5556
} = props;
5657

57-
const classNames = ["c-button"];
58-
59-
if (style === ButtonStyles.None) {
60-
classNames[0] = "";
61-
}
58+
let classNames = style === ButtonStyles.None ? [] : ["c-button"];
6259

6360
if (size != null) {
64-
classNames.push(size);
61+
classNames.push(`-${size}`);
6562
}
6663

6764
if (style != null) {
68-
classNames.push(style);
65+
classNames.push(`-${style}`);
6966
}
7067

71-
if (cssClassName != null && cssClassName.length > 0) {
72-
classNames.push(cssClassName);
68+
if (StringUtils.hasValue(cssClassName)) {
69+
classNames.push(cssClassName!);
7370
}
7471

7572
return (

0 commit comments

Comments
 (0)