NextJs 통합 테스트 - React Context
import { createContext } from "react";
// ToastContext.tsx
// React Context Types
export type ToastStyle = "succeed" | "failed" | "busy";
export type ToastState = {
isShown: boolean;
message: string;
style: ToastStyle;
};
// React Context
export const initialState: ToastState = {
isShown: false,
message: "",
style: "succeed",
};
export const ToastStateContext = createContext(initialState);
export type ToastAction = {
showToast: (state?: Partial<Omit<ToastState, "isShown">>) => void;
hideToast: () => void;
};
export const initialAction: ToastAction = {
showToast: () => {},
hideToast: () => {},
};
export const ToastActionContext = createContext(initialAction);테스트 코드
1번째 방법
테스트용 컴포넌트를 만들어 인터랙션 실행
2번째 방법
초기값 주입하여 렌더링된 내용 확인
2번째 방법보다, 1번째 방법이 useToastAction을 포함하므로 더 넓은 범위의 통합 테스트라고 할 수 있다.
Last updated