- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在我的应用程序中编写一些集成测试。
我有几个 API 调用,它们是在 componentDidMount 和按钮单击时调用的。 API 响应保存在 Store 上,从而更新 View 。
如何测试与 API 的集成?
这是我的 WIP 代码:
let wrapped
const options = {
disableLifecycleMethods: true
}
const mockPlanet = {
name: "Yavin IV",
climate: "temperate, tropical",
terrain: "jungle, rainforests",
population: "1000",
films: ["A New Hope"],
url: "https://swapi.co/api/planets/3/"
}
const initialPlanet = {
name: "Alderaan",
climate: "temperate",
terrain: "grasslands, mountains",
population: "2000000000",
films: ["A New Hope", "Revenge of the Sith"],
url: "https://swapi.co/api/planets/2/"
}
const mockStore = configureMockStore([thunk])
const store = mockStore({
planets:{
planetCount: 1,
activePlanet: initialPlanet,
planetCache: [],
loading: false,
error: null
}
})
const address = /https:\/\/swapi.co\/api\/planets/[0-9]/
beforeEach(()=>{
moxios.install()
moxios.stubRequest(
address,
{
status: 200,
response: mockPlanet
}
)
})
afterEach(()=>{
moxios.uninstall()
})
it('Fetch planet on click',()=>{
wrapped = mount(
<Provider store={store}>
<App/>
</Provider>, options
)
wrapped.find('.button-get-planet').first().simulate('click')
wrapped.update()
// What should I test here?
})
提前致谢。
最佳答案
那里有一些陷阱:
redux-mock-store
does not run reducer 。要测试真实的 reducer 和真实的操作(否则你的 axios
的模拟将永远不会应用于组件),你需要使用真实的存储。axios
模拟是基于 Promise 的,您需要 moxios.wait
或setTimeout()
/await <anything>
在运行检查之前。否则,当您尝试验证某些内容时,Promise 将无法实现。 wrapper.update()
因为一旦你处理了#1 和#2 项,它就会由 redux 运行在测试中,您必须验证您的组件在数据成功加载后是否符合预期。
// simulate click
// check some "Loader" is displayed if expected
await Promise.resolve(); // just to flush microtasks queue - or use moxios.wait()
// check real data is rendered accordingly
关于javascript - 使用 Redux 和 Enzyme 在 React 中编写集成测试的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58547911/
我的页面上有多个按钮: Home Action 1 Action 2 如何通过其文本选择“操作 2”按钮,以便单击它?我知道可能还有其他方法来选择该按钮,但我专门寻找通过文
我目前正在学习使用 Enzyme 进行单元测试。 似乎有一种奇怪的行为: enzyme 似乎随机地不识别某些成分。让我给你举个例子: 这是我的SafeContainer组件: import React
我在使用 Jest/Enzyme 编写的测试用例时遇到了一些问题。我有一个 React/Redux 组件,正在尝试编写一个基本测试,但出现以下错误: Invariant Violation: Reac
在我的项目中使用 enzyme 3 时出现以下错误(之前使用 enzyme 2 并且一切正常): Enzyme Internal Error: configured enzyme adapter di
我正在致力于使用摩卡、 enzyme 创建 react 组件的单元测试。下面是一个示例组件。 Foo.js class Foo extends React.Component { custom
我已经设置了 jest 和 enzyme,但无法运行。它提示 EnzymeAdapter 基类。 我试过了 configure({}) with import { configure } from '
使用 React+Enzyme+Jest 您好,我使用 .innerText 属性来获取特定元素的值,请参阅我的代码的第 5 行: _modifyProfileField (event) {
我正在使用 airbnb 的 enzyme 库进行 react 测试,我只想检索当前节点的文本,不包括来自任何子节点的任何文本。 const component = hellothere 如果我做
我一直在查看很多使用 Enzyme 和 Jest 测试 React 和 Redux 应用程序的示例,但很少有人提到代码清理。当使用 shallow 或 mount 时,您是否不需要明确调用 unmou
从文档来看,这样的事情应该是可能的: const wrapper = mount(); expect(wrapper.find('.foo')).to.have.length(1); 但就我而言,这会
有关enzyme 测试库中adapter 用途的任何文档。 import { configure } from 'enzyme'; import Adapter from 'enzyme-adapte
我需要访问内部组件的状态,使其在单击事件时处于事件状态,我的问题是 Enzyme 在使用 mount 时不允许这样做,这只能通过 来实现如 here 中提到的 enzyme 的浅渲染,也如上所述,我尝
我正在尝试使用 Enzyme 来测试组件的方法。我知道执行此操作的典型方法是使用 Enzyme 的 instance() 方法。 问题是,这只适用于root组件,我的组件需要包装在两个上下文提供程序中
我正在尝试测试当通过简单的 bool 值更新状态时,在父组件中其子组件是否正确呈现。在父组件下方: class Parent extends Component { ... render()
我的渲染函数中有一个 FileInput } className= 'fileInput' /> 我需要编写一个文件上传测试,当我模拟更改函数时,它调用函数file
我通过 create-react-app 创建了一个新的 React 应用程序,我想为我在应用程序中创建的名为“MessageBox”的组件编写单元测试。这是我编写的单元测试: import Mess
昨天我将React项目升级到了v16.0,但是我发现Enzyme有一些问题 Error: Enzyme Internal Error: Enzyme expects an adap
我有一个具有多种渲染方法的组件。我怎样才能测试它们呢?使用 enzyme 和 Jest 进行 react 。 一些伪代码作为我的组件结构的示例,因为该组件非常大。 class MyComponent
我正在使用 Jest 和 Enzyme 来测试我的 react 组件。其中一项测试如下所示: import DropDown from './DropdDown'; import SelectButt
我正在尝试测试轮子事件。我在加载事件中添加滚轮监听器。 this.domElement = document.getElementById('myImg'); if (this.domElement)
我是一名优秀的程序员,十分优秀!