1
1
mirror of https://github.com/theoludwig/p61-project.git synced 2024-07-17 07:00:12 +02:00
p61-project/presentation/react/hooks/__tests__/useBoolean.test.ts

87 lines
1.9 KiB
TypeScript
Raw Normal View History

2024-02-16 22:51:50 +01:00
import { act, renderHook } from "@testing-library/react-native"
2024-03-16 00:36:44 +01:00
import { useBoolean } from "@/presentation/react/hooks/useBoolean"
2024-02-16 22:51:50 +01:00
2024-05-02 01:08:27 +02:00
describe("presentation/react/hooks/useBoolean", () => {
afterEach(() => {
2024-02-16 22:51:50 +01:00
jest.clearAllMocks()
})
const initialValues = [true, false]
for (const initialValue of initialValues) {
it(`should set the initial value to ${initialValue}`, () => {
2024-05-02 01:08:27 +02:00
// Arrange - Given
2024-02-16 22:51:50 +01:00
const { result } = renderHook(() => {
return useBoolean({ initialValue })
})
2024-05-02 01:08:27 +02:00
// Assert - Then
2024-02-16 22:51:50 +01:00
expect(result.current.value).toBe(initialValue)
})
}
it("should by default set the initial value to false", () => {
2024-05-02 01:08:27 +02:00
// Arrange - Given
2024-02-16 22:51:50 +01:00
const { result } = renderHook(() => {
return useBoolean()
})
2024-05-02 01:08:27 +02:00
// Assert - Then
2024-02-16 22:51:50 +01:00
expect(result.current.value).toBe(false)
})
it("should toggle the value", async () => {
2024-05-02 01:08:27 +02:00
// Arrange - Given
2024-02-16 22:51:50 +01:00
const { result } = renderHook(() => {
return useBoolean({ initialValue: false })
})
2024-05-02 01:08:27 +02:00
// Act - When
2024-02-16 22:51:50 +01:00
await act(() => {
return result.current.toggle()
})
2024-05-02 01:08:27 +02:00
// Assert - Then
2024-02-16 22:51:50 +01:00
expect(result.current.value).toBe(true)
2024-05-02 01:08:27 +02:00
// Act - When
2024-02-16 22:51:50 +01:00
await act(() => {
return result.current.toggle()
})
2024-05-02 01:08:27 +02:00
// Assert - Then
2024-02-16 22:51:50 +01:00
expect(result.current.value).toBe(false)
})
it("should set the value to true", async () => {
2024-05-02 01:08:27 +02:00
// Arrange - Given
2024-02-16 22:51:50 +01:00
const { result } = renderHook(() => {
return useBoolean({ initialValue: false })
})
2024-05-02 01:08:27 +02:00
// Act - When
2024-02-16 22:51:50 +01:00
await act(() => {
return result.current.setTrue()
})
2024-05-02 01:08:27 +02:00
// Assert - Then
2024-02-16 22:51:50 +01:00
expect(result.current.value).toBe(true)
})
it("should set the value to false", async () => {
2024-05-02 01:08:27 +02:00
// Arrange - Given
2024-02-16 22:51:50 +01:00
const { result } = renderHook(() => {
return useBoolean({ initialValue: true })
})
2024-05-02 01:08:27 +02:00
// Act - When
2024-02-16 22:51:50 +01:00
await act(() => {
return result.current.setFalse()
})
2024-05-02 01:08:27 +02:00
// Assert - Then
2024-02-16 22:51:50 +01:00
expect(result.current.value).toBe(false)
})
})