Skip to content

Commit

Permalink
chore(web): add useManageSwitchState (#471)
Browse files Browse the repository at this point in the history
Co-authored-by: 長田淳史 <>
Co-authored-by: keiya sasaki <keiya.s.0210@gmail.com>
  • Loading branch information
atnagata and keiya01 committed Jun 9, 2023
1 parent 47c0a3a commit 97f7f2f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
61 changes: 61 additions & 0 deletions web/src/beta/hooks/useManageSwitchState/hooks.test.tsx
@@ -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);
});
25 changes: 25 additions & 0 deletions web/src/beta/hooks/useManageSwitchState/hooks.tsx
@@ -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;

0 comments on commit 97f7f2f

Please sign in to comment.