unit-testing - 测试 GraphQL Apollo 和 Vue.js
问题描述
我正在尝试对 Vue.js 和 Apollo 应用程序中的组件进行单元测试。组件的一般行为是查询服务器,将响应加载到data
Vue 组件中。当按下按钮时,运行 GraphQL 突变,然后使用响应数据更新数据。这是组件的存根。
Vue.extend({
$apollo: {
person: {
query: gql`query($id: ID!) {
person(id: $id) {
name
id
}
}`,
fetch-policy: 'network-only',
}
},
data() {
return {
person: {},
newPersonId: null,
}
},
methods: {
getNewPerson() {
this.$apollo.mutate({
mutation: gql`mutation($person: Person!) {
savePerson(person: $Person) {
person {
id
}
}
}`,
update: (store, { data }) {
////////////////////////////
// test-what-happens-here //
////////////////////////////
}
})
}
}
});
我的直觉说要编写的适当测试是
it('renders the name in correctly on load')
it('sends the correct data with the mutation')
it('does the right thing when the server responds') <--- Most Important
it('does the right thing when the server responds with an error') <--- Most Important
我编写了一些测试,但似乎没有一个是测试组件往返的好模式。
如果这是一个 axios 请求,我会模拟响应以发送 200 和正确的数据,并断言 axios 请求是使用正确的 JSON 调用的,并且组件看起来是正确的,基于存根响应。
我一辈子都无法用 vue-apollo 解决这个问题。
他们有一个不错的测试部分,但是使用 apollo 的“简单测试”无法测试请求的结果,而且这些graphql-tools
测试实际上并没有利用 apollo,因此它错过了相当多的行为。
您将如何在上面的代码中测试更新功能?
解决方案
推荐阅读
- memory - C/C++ 中本地函数的内存填充和对齐
- c# - WinForms - 加载表单时如何使用 PaintEventArgs 运行函数?
- python-3.x - def 函数比较两个字符串
- python - Python 多个 onclick 事件
- assembly - 如何在特定列打印包含多行的整个字符串?
- haskell - 泛型函数中的 Haskell 类型构造函数
- rdf - 在 RDF 中制作元三元组的正确方法是什么?
- python - Django 模板 html 文件找不到 WebGL Build 和 TemplateData 文件夹
- python - 如何在 VSCode 中禁用所有 python linting
- javascript - 为什么这种 Array.sort 行为在 Chrome 和 Node.js 中不同