Skip to content

Commit

Permalink
test: 添加单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
kaze-k committed Aug 1, 2024
1 parent b52efcb commit a04e7d1
Show file tree
Hide file tree
Showing 9 changed files with 392 additions and 1 deletion.
61 changes: 61 additions & 0 deletions src/components/__tests__/base/Button/BlockButton.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import "@testing-library/jest-dom"
import { fireEvent, render, screen } from "@testing-library/react"

import BlockButton from "~/components/base/Button/BlockButton"

describe("BlockButton", (): void => {
test("renders with children", (): void => {
render(<BlockButton>Click me</BlockButton>)
expect(screen.getByText("Click me")).toBeInTheDocument()
})

test("calls onClick handler when clickable and button is clicked", (): void => {
const handleClick: jest.Mock = jest.fn()
render(
<BlockButton
clickable
onClick={handleClick}
>
Click me
</BlockButton>,
)

fireEvent.click(screen.getByText("Click me"))
expect(handleClick).toHaveBeenCalledTimes(1)
})

test("does not call onClick handler when not clickable", (): void => {
const handleClick: jest.Mock = jest.fn()
render(
<BlockButton
clickable={false}
onClick={handleClick}
>
Click me
</BlockButton>,
)

fireEvent.click(screen.getByText("Click me"))
expect(handleClick).not.toHaveBeenCalled()
})

test("applies width and height correctly", (): void => {
const { container } = render(
<BlockButton
btnWidth={"100px"}
btnHeight={"50px"}
>
Click me
</BlockButton>,
)
const button: HTMLButtonElement = container.querySelector("button")

expect(button).toHaveStyle("width: 100px")
expect(button).toHaveStyle("height: 50px")
})

test("renders with title attribute", (): void => {
render(<BlockButton title="This is a button">Click me</BlockButton>)
expect(screen.getByTitle("This is a button")).toBeInTheDocument()
})
})
37 changes: 37 additions & 0 deletions src/components/__tests__/base/Button/RoundButton.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import "@testing-library/jest-dom"
import { fireEvent, render, screen } from "@testing-library/react"

import RoundButton from "~/components/base/Button/RoundButton"

describe("RoundButton", (): void => {
it("renders with children", (): void => {
render(<RoundButton title="Test Button">Click Me</RoundButton>)
expect(screen.getByText("Click Me")).toBeInTheDocument()
})

it("handles click event", (): void => {
const handleClick: jest.Mock = jest.fn()
render(<RoundButton onClick={handleClick}>Click Me</RoundButton>)
fireEvent.click(screen.getByText("Click Me"))
expect(handleClick).toHaveBeenCalledTimes(1)
})

it("does not call onClick when clickable is false", (): void => {
const handleClick: jest.Mock = jest.fn()
render(
<RoundButton
onClick={handleClick}
clickable={false}
>
Click Me
</RoundButton>,
)
fireEvent.click(screen.getByText("Click Me"))
expect(handleClick).not.toHaveBeenCalled()
})

it("renders with the correct title attribute", (): void => {
render(<RoundButton title="Test Button">Click Me</RoundButton>)
expect(screen.getByTitle("Test Button")).toBeInTheDocument()
})
})
48 changes: 48 additions & 0 deletions src/components/__tests__/base/IconFont/IconFont.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import "@testing-library/jest-dom"
import { render } from "@testing-library/react"
import "jest-styled-components"

import IconFont from "~/components/base/IconFont"

describe("IconFont", (): void => {
it("renders with the correct icon content", (): void => {
const { container } = render(<IconFont icon="users" />)
expect(container.firstChild).toHaveStyle({
"font-family": "iconfont",
})
expect(container.firstChild).toHaveStyleRule("content", '"\\e658"', {
modifier: "::before",
})
})

it("renders with the correct block property", (): void => {
const { container } = render(
<IconFont
icon="views"
block
/>,
)
expect(container.firstChild).toHaveStyle({
display: "block",
})
})

it("renders with the correct size", (): void => {
const { container } = render(
<IconFont
icon="heart"
size="2em"
/>,
)
expect(container.firstChild).toHaveStyle({
"font-size": "2em",
})
})

it("renders with the correct default size", (): void => {
const { container } = render(<IconFont icon="like" />)
expect(container.firstChild).toHaveStyle({
"font-size": "1.3em",
})
})
})
34 changes: 34 additions & 0 deletions src/components/__tests__/base/Main/Main.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import "@testing-library/jest-dom"
import { render } from "@testing-library/react"
import "jest-styled-components"

import Main from "~/components/base/Main"

describe("Main", (): void => {
it("renders with children", (): void => {
const { getByText } = render(
<Main className="custom-class">
<div>Test Child</div>
</Main>,
)
expect(getByText("Test Child")).toBeInTheDocument()
})

it("applies the passed className", (): void => {
const { container } = render(
<Main className="custom-class">
<div>Test Child</div>
</Main>,
)
expect(container.firstChild).toHaveClass("custom-class")
})

it("has the main class from style module", (): void => {
const { container } = render(
<Main className="custom-class">
<div>Test Child</div>
</Main>,
)
expect(container.firstChild).toHaveClass("main")
})
})
33 changes: 33 additions & 0 deletions src/components/__tests__/base/Row/Row.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import "@testing-library/jest-dom"
import { render } from "@testing-library/react"

import Row from "~/components/base/Row"

describe("Row", (): void => {
it("renders with title and text", (): void => {
const { getByTitle, getByText } = render(
<Row
title="Test Title"
text="Test Text"
>
<div>Child Element</div>
</Row>,
)

expect(getByTitle("Test Title")).toBeInTheDocument()
expect(getByText("Test Text")).toBeInTheDocument()
})

it("renders with children", (): void => {
const { getByText } = render(
<Row
title="Test Title"
text="Test Text"
>
<div>Child Element</div>
</Row>,
)

expect(getByText("Child Element")).toBeInTheDocument()
})
})
38 changes: 38 additions & 0 deletions src/components/__tests__/base/Switch/Switch.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import "@testing-library/jest-dom"
import { fireEvent, render } from "@testing-library/react"

import Switch from "~/components/base/Switch"

describe("Switch", (): void => {
it("should render correctly", (): void => {
const handleChange: jest.Mock = jest.fn()
const { getByRole } = render(
<Switch
id="test-switch"
checked={false}
onChange={handleChange}
/>,
)

const input: HTMLElement = getByRole("checkbox")
expect(input).toBeInTheDocument()
expect(input).toHaveAttribute("id", "test-switch")
expect(input).not.toBeChecked()
})

it("should handle check state changes", (): void => {
const handleChange: jest.Mock = jest.fn()
const { getByRole } = render(
<Switch
id="test-switch"
checked={false}
onChange={handleChange}
/>,
)

const input: HTMLElement = getByRole("checkbox")
fireEvent.click(input)

expect(handleChange).toHaveBeenCalled()
})
})
17 changes: 17 additions & 0 deletions src/components/__tests__/base/View/View.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import "@testing-library/jest-dom"
import { render, screen } from "@testing-library/react"

import View from "~/components/base/View"

describe("View", (): void => {
it("should render children correctly", (): void => {
render(
<View>
<span>Child Content</span>
</View>,
)

const childContent: HTMLElement = screen.getByText("Child Content")
expect(childContent).toBeInTheDocument()
})
})
3 changes: 2 additions & 1 deletion src/utils/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function formatNum(num: number): string {
* @return {*} {string} 返回中文星期日期
*/
function toChineseDay(day: number): string {
if (day < 1 || day > 7) return
const week: string[] = ["一", "二", "三", "四", "五", "六", "日"]
return week[day - 1]
}
Expand Down Expand Up @@ -103,7 +104,7 @@ function toTime(time: number, opt: ToTimeOpt = ToTimeOpt.M): number {
break

default:
result = time / 1000
result = time / 1000 / 60
}

return result
Expand Down
Loading

0 comments on commit a04e7d1

Please sign in to comment.