Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript react测试库呈现测试中声明的功能组件_Javascript_Reactjs_Jestjs_React Testing Library - Fatal编程技术网

Javascript react测试库呈现测试中声明的功能组件

Javascript react测试库呈现测试中声明的功能组件,javascript,reactjs,jestjs,react-testing-library,Javascript,Reactjs,Jestjs,React Testing Library,是否不能声明一些react函数组件,这些组件通过react测试库嵌套和呈现其中一个 以下是我的尝试: RestaurantIdContext.js RestaurantIdContext.test.js 任何帮助都将不胜感激,谢谢 您在命名的导入中忘记了Id) 您在命名的导入中忘记了Id) 这应该是可能的,我在测试中已经做过很多次了。我想还有别的问题。检查您是否正在导出/导入所有内容correctly@GiorgioPolvara-Gpx你是绝对正确的!我在指定的导入中忘记了一个Id。。如果你建

是否不能声明一些react函数组件,这些组件通过
react测试库嵌套和
呈现其中一个

以下是我的尝试:

RestaurantIdContext.js

RestaurantIdContext.test.js


任何帮助都将不胜感激,谢谢

您在命名的导入中忘记了
Id

您在命名的导入中忘记了
Id

这应该是可能的,我在测试中已经做过很多次了。我想还有别的问题。检查您是否正在导出/导入所有内容correctly@GiorgioPolvara-Gpx你是绝对正确的!我在指定的导入中忘记了一个
Id
。。如果你建议它作为答案,那么我会把它标记为已解决。好的,建议:这应该是可能的,我在测试中已经做了很多次了。我想还有别的问题。检查您是否正在导出/导入所有内容correctly@GiorgioPolvara-Gpx你是绝对正确的!我在指定的导入中忘记了一个
Id
。。如果你建议它作为一个答案,那么我会把它标记为已解决
import React from "react"

const RestaurantIdContext = React.createContext(null)

export const RestaurantIdProvider = RestaurantIdContext.Provider
export const RestaurantIdConsumer = RestaurantIdContext.Consumer
export default RestaurantIdContext
import React, { useContext } from "react"
import { render } from "@testing-library/react"
import "@testing-library/jest-dom/extend-expect"
import RestaurantIdContext, { RestaurantProvider, RestaurantConsumer } from "./RestaurantIdContext"

describe("RestaurantIdContext", () => {
  it("makes it possible to get the restaurant ID", () => {
    function NestedComponent() {
      const restaurantId = useContext(RestaurantIdContext)

      return <p>Restaurant ID: {restaurantId}</p>
    }

    function GreatGrandParentComponent() {
      return (
        <div>
          <RestaurantProvider value="123456">
            <div>
              <div>
                <NestedComponent />
              </div>
            </div>
          </RestaurantProvider>
        </div>
      )
    }

    const { getByText } = render(<GreatGrandParentComponent />)

    expect(getByText("Restaurant ID").textContent).toBe("Restaurant ID: 123456")
  })
})
  ● RestaurantIdContext › makes it possible to get the restaurant ID

    Invariant Violation: Element type is invalid: expected a string (for built-in compo
nents) or a class/function (for composite components) but got: undefined. You likely fo
rgot to export your component from the file it's defined in, or you might have mixed up
 default and named imports.

    Check the render method of `GreatGrandParentComponent`.

      26 |     }
      27 | 
    > 28 |     const { getByText } = render(<GreatGrandParentComponent />)
         |                           ^
      29 | 
      30 |     expect(getByText("Restaurant ID").textContent).toBe("Restaurant ID: 123456")
      31 |   })