react-native - 测试用提供者包裹的反应原生屏幕
问题描述
我正在尝试实施我的第一个测试,但我的提供者包装组件时遇到了问题。
我有一个简单NotificationsScreen.tsx
的显示来自我的通知NotificationsContext
:
NotificationsScreen.tsx
const NotificationScreen = (): ReactElement => {
const {notifications, loadMoreNotifications} = useNotification();
return (
<FlatList data={notifications}
keyExtractor={((item) => item.id)}
renderItem={({item}) => <NotificationRow notification={item}/>}
onEndReached={loadMoreNotifications}
/>
);
};
NotificationProvider.tsx :
export const NotificationContext = createContext<NotificationContextInterface>(initialState);
const NotificationProvider = (props: PropsWithChildren<any>) => {
const [notifications, setNotifications] = useState();
const notificationApi = useNotificationApi();
const [badgeCount, setBadgeCount] = useState<number>(0);
/**
* On provider mounted, get user notifications.
*/
useEffect(() => {
notificationApi.index()
.then((response) => {
setNotifications(response);
setBadgeCount(response.length);
});
}, []);
/**
* Mark a notification as read.
*
* @param {Notification} notification - The notification marked as read.
*/
const notificationRead = (notification: Notification) => {
...
};
/**
* Load more notifications
*
* @return {Promise<void>}
*/
const loadMoreNotifications = (): Promise<void> => {
...
};
return (
<NotificationContext.Provider
value={{
notifications,
notificationReceived,
setNotificationChunk,
notificationRead,
loadMoreNotifications,
badgeCount,
notificationGranted
}}>
{props.children}
</NotificationContext.Provider>
);
};
UseNotification.tsx
const useNotification = (): NotificationContextInterface => useContext<any>(NotificationContext);
export default useNotification;
我尝试在测试开始时实现提供者模拟:
jest.mock("../../providers/NotificationProvider", () => {
const React2 = require("react");
const FakeContext = React2.createContext({});
const notifications: Notification[] = [
{
id: "abc",
created_at: "2021-03-01",
data: {
title: "Notification title.",
description: "Notification description.",
resource_id: 0
},
type: "orderCreated",
formatted: {
created_at: "5 minutes ago"
}
},
{
id: "def",
created_at: "2021-03-01",
data: {
title: "Notification title.",
description: "Notification description.",
resource_id: 0
},
type: "orderCreated",
formatted: {
created_at: "10 minutes ago"
}
}
];
const notificationProviderProps: NotificationContextInterface = {
notifications,
setNotificationChunk: jest.fn(),
notificationReceived: jest.fn(),
notificationRead: jest.fn(),
loadMoreNotifications: jest.fn(),
badgeCount: notifications.length,
notificationGranted: true
};
return ({children}: { children: any, value: any }) => {
return <FakeContext.Provider value={notificationProviderProps}>
{children}
</FakeContext.Provider>;
};
});
然后尝试渲染我的屏幕:
it("Render default", () => {
const screen = create(
<NotificationProvider>
<NotificationScreen/>
</NotificationProvider>
);
});
但不幸的是,我的屏幕似乎无法访问该上下文......这是日志跟踪:
TypeError: (0 , _useNotification2.default) 不是函数
我做错了什么?
解决方案
推荐阅读
- javascript - 如何在反应功能组件中的 AgGrid Cell 中添加按钮和下拉列表
- samsung-mobile - 如何与拥有不同三星帐户的其他人共享胶囊修订 ID?
- python - 如何在函数中操作 DataFrame 名称?
- angular - Swiper.js bulletClass 分页在 Angular 中不起作用
- assembly - 在内部 PC 扬声器上播放的音符关闭键
- javascript - 使用 React 在本地存储中设置默认数据的正确方法是什么?
- openscript - 问题:处理异常后OpenScript没有进入下一次迭代
- javascript - 如何在“深度 2”层次结构中使用道具
- php - 如果 cond 精确计数,如何制作?
- flutter - 为什么我的小部件上的参数传递为空?