Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(web): add useManageSwitchState (#471)
Co-authored-by: 長田淳史 <> Co-authored-by: keiya sasaki <keiya.s.0210@gmail.com>
- Loading branch information
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { renderHook, act } from "@testing-library/react"; | ||
import { expect, test } from "vitest"; | ||
|
||
import useManageSwitchState, { Props } from "./hooks"; | ||
|
||
type SwitchField = { | ||
id: string; | ||
name: string; | ||
active: boolean; | ||
}; | ||
|
||
const props: Props<SwitchField> = { | ||
fields: [ | ||
{ id: "1", name: "First component", active: true }, | ||
{ id: "2", name: "Second component", active: false }, | ||
{ id: "3", name: "Third component", active: false }, | ||
], | ||
}; | ||
|
||
test("1. confirmation of initial state", () => { | ||
const { result } = renderHook(() => useManageSwitchState(props)); | ||
expect(result.current.fields[0].id).toBe("1"); | ||
expect(result.current.fields[1].id).toBe("2"); | ||
expect(result.current.fields[2].id).toBe("3"); | ||
expect(result.current.fields[0].name).toBe("First component"); | ||
expect(result.current.fields[1].name).toBe("Second component"); | ||
expect(result.current.fields[2].name).toBe("Third component"); | ||
expect(result.current.fields[0].active).toBe(true); | ||
expect(result.current.fields[1].active).toBe(false); | ||
expect(result.current.fields[2].active).toBe(false); | ||
}); | ||
|
||
test("2. only the second should be active", () => { | ||
const { result } = renderHook(() => useManageSwitchState(props)); | ||
act(() => { | ||
result.current.handleActivate("2"); | ||
}); | ||
expect(result.current.fields[0].active).toBe(false); | ||
expect(result.current.fields[1].active).toBe(true); | ||
expect(result.current.fields[2].active).toBe(false); | ||
}); | ||
|
||
test("3. only the third should be active", () => { | ||
const { result } = renderHook(() => useManageSwitchState(props)); | ||
act(() => { | ||
result.current.handleActivate("3"); | ||
}); | ||
expect(result.current.fields[0].active).toBe(false); | ||
expect(result.current.fields[1].active).toBe(false); | ||
expect(result.current.fields[2].active).toBe(true); | ||
}); | ||
|
||
test("4. status should remain the same", () => { | ||
const { result } = renderHook(() => useManageSwitchState(props)); | ||
act(() => { | ||
result.current.handleActivate("1"); | ||
}); | ||
expect(result.current.fields[0].active).toBe(true); | ||
expect(result.current.fields[1].active).toBe(false); | ||
expect(result.current.fields[2].active).toBe(false); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useState, useCallback } from "react"; | ||
|
||
type SwitchField<T> = { | ||
id: string; | ||
active?: boolean; | ||
} & T; | ||
|
||
export type Props<T> = { | ||
fields: SwitchField<T>[]; | ||
}; | ||
|
||
export const useManageSwitchState = <T,>({ fields }: Props<T>) => { | ||
const [fieldsState, setFieldsState] = useState(fields); | ||
const handleActivate = useCallback((id: string) => { | ||
setFieldsState(f => | ||
f.map(v => ({ | ||
...v, | ||
active: v.id === id, | ||
})), | ||
); | ||
}, []); | ||
return { handleActivate, fields: fieldsState }; | ||
}; | ||
|
||
export default useManageSwitchState; |