尝试测试使用 Mobx 的 React 应用程序时出错

发布于 2025-01-10 09:17:32 字数 2062 浏览 2 评论 0原文

我正在尝试在我的 React 应用程序上编写测试并从上下文中收到错误。 我收到的错误是:无法读取未定义的属性“Provider” 我的测试看起来像这样:

 import { render, screen } from "@testing-library/react"
import userEvent from "@testing-library/user-event";
import EmailLoginForm from "../features/users/EmailLoginForm";



test('if no email and no password, button disabled', async () =>{

    render(<EmailLoginForm/>);  

    expect(await screen.findByRole('button', {  name: /login/i})).toBeDisabled();

      userEvent.type(screen.getByPlaceholderText(/email id/i),"[email protected]");
      userEvent.type(screen.getByPlaceholderText(/password/i),"testpw");

      expect(await screen.findByRole('button',{name :/login/i})).toBeEnabled();
})

我的主商店看起来像这样:

   import {createContext, useContext} from "react";
//..some imports that are not relevant


interface Store{
    // .. interface of which stores
}

export const store: Store = {
    //.. store declerations
    
}

export const StoreContext = createContext(store);

export function useStore(){
    return useContext(StoreContext);
}

我的 EmailLoginForm 是一个观察者。

index.tsx 看起来像这样:

  import React from 'react';
import ReactDOM from 'react-dom';
import '../src/app/layout/App';
import App from '../src/app/layout/App';
import reportWebVitals from './reportWebVitals';
import { createBrowserHistory } from 'history';
import { Router } from 'react-router-dom';
import './app/layout/index.css'
import { store, StoreContext } from "./app/stores/store"
import ScrollToTop from './app/layout/ScrollToTop';

export const history = createBrowserHistory();



ReactDOM.render(
  <StoreContext.Provider value={store}>
    <Router history={history}>
      <ScrollToTop />

          <App />

    </Router>
  </StoreContext.Provider>,
  document.getElementById('root')
);

我在这里很迷茫,不知道问题出在哪里以及如何解决它。 任何帮助将不胜感激。

I am trying to write tests on my React app and receiving an error from the context.
The error I receive is :Cannot read property 'Provider' of undefined
My test looks like that:

 import { render, screen } from "@testing-library/react"
import userEvent from "@testing-library/user-event";
import EmailLoginForm from "../features/users/EmailLoginForm";



test('if no email and no password, button disabled', async () =>{

    render(<EmailLoginForm/>);  

    expect(await screen.findByRole('button', {  name: /login/i})).toBeDisabled();

      userEvent.type(screen.getByPlaceholderText(/email id/i),"[email protected]");
      userEvent.type(screen.getByPlaceholderText(/password/i),"testpw");

      expect(await screen.findByRole('button',{name :/login/i})).toBeEnabled();
})

My main store looks like that:

   import {createContext, useContext} from "react";
//..some imports that are not relevant


interface Store{
    // .. interface of which stores
}

export const store: Store = {
    //.. store declerations
    
}

export const StoreContext = createContext(store);

export function useStore(){
    return useContext(StoreContext);
}

My EmailLoginForm is an observer.

The index.tsx looks like that:

  import React from 'react';
import ReactDOM from 'react-dom';
import '../src/app/layout/App';
import App from '../src/app/layout/App';
import reportWebVitals from './reportWebVitals';
import { createBrowserHistory } from 'history';
import { Router } from 'react-router-dom';
import './app/layout/index.css'
import { store, StoreContext } from "./app/stores/store"
import ScrollToTop from './app/layout/ScrollToTop';

export const history = createBrowserHistory();



ReactDOM.render(
  <StoreContext.Provider value={store}>
    <Router history={history}>
      <ScrollToTop />

          <App />

    </Router>
  </StoreContext.Provider>,
  document.getElementById('root')
);

I am pretty lost here and don't know where is the issue and how to fix it.
Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文