javascript - 如何将我的全局状态导入 react-testing-library?
问题描述
我的问题是当我尝试将弹出窗口的全局状态导入测试文件并尝试使用它时。然后它告诉我我使用错误的钩子并且它不起作用。
我的测试代码:
import React from 'react'
import Popup from '../Popup'
import { render } from '@testing-library/react'
import '@testing-library/jest-dom/extend-expect'
import 'mutationobserver-shim'
import { usePopupState } from '../../../constants/States'
describe('testing PopupComponent', () => {
it('should add correctly', () => {
const { setOpenPopup } = usePopupState()
setOpenPopup(true)
const { getByTestId } = render(<Popup />)
expect(getByTestId('textfield-name')).toBeEmpty()
expect(getByTestId('add-popup-container')).toBeInTheDocument()
})
})
我的全球状态代码:
export interface IPopupState {
openPopup: boolean,
setOpenPopup:(state: boolean) => void
}
export const usePopupState = create<IPopupState>(set => ({
openPopup: false,
setOpenPopup: (state: boolean) => set({ openPopup: state }),
}))
如何使用 react-testing-library 解决这个问题?
解决方案
推荐阅读
- angular - ng serve 上的 Angular 12 块名称
- android - Jetpack Compose,如何在 Scaffold 中的 .showSnackbar() 中更改小吃店动作的颜色?
- r-markdown - “无法加载图片或 PDF 文件”仅在一个系统中编织 rmd 时
- c# - 有没有办法在VS2019上自动添加很多属性?
- sql - For LOOP with group by insert into ORA-06550 Column NOT ALLOWED HERE 错误
- oracle - 使用 af:inputfile 组件访问页面时 ADF 中的 ArrayIndexOutOfBoundsException
- python - python的强类型枚举(mypy)
- python - Vue.js 不使用 Flask 后端返回的数据呈现 html 文件
- django - 在同一个项目中使用 django 和 npm 有意义吗?
- python - conda 包版本控制的“=”和“==”有什么区别?