reactjs - 代码拆分 ReactJS 组件以分离 API 调用函数在导入提取的函数时测试失败
问题描述
这是目录结构
resources/js/containers/Grid
├── Grid.js
├── getAllBlogsPreview.js
└── package.json
getAllBlogsPreview
被导入到 Grid
import getAllBlogsPreview from "./getAllBlogsPreview";
是一个调用axis并返回带有一些数据的结果的函数。
export default function getAllBlogsPreview({ blogs = [], showGrid = false }) {
Axios.get("/api/blogs")
.then(response => {
blogs = response.data;
showGrid = false;
})
.catch(error => {
console.log(error);
});
let result = {
blogs: blogs,
showGrid: showGrid
};
return result;
}
将其移出,因为无法使用componentDidMount
方法和某些refreshTable
方法直接执行这些 api 调用来测试组件。所以现在在我拥有的组件中
componentDidMount() {
this.updateBlogsTable();
}
updateBlogsTable() {
let result = getAllBlogsPreview();
this.setState({ blogs: result.blogs });
this.setState({ showGrid: result.showGrid });
}
这个想法是我应该能够模拟 Grid 的实现,getAllBlogsPreview
从而测试 Grid 而不会被解决承诺所困。
测试失败,因为它尝试getAllBlogsPreview
从测试文件本身加载
// the component to test
import Grid from "../../containers/Grid/Grid";
import getAllBlogsPreview from "../../containers/Grid/getAllBlogsPreview";
jest.mock("getAllBlogsPreview");
describe("Blog Grid", () => {
const result = {
blogs: {
data: [
{
title: "title one",
published: false,
publish_date: null,
slug: "title-one"
}
],
links: {
self: "link-value",
first: "http://adminpanel.test/api/blogs?page=1",
last: null,
prev: null,
next: null
},
meta: {
current_page: 1,
from: 1,
path: "http://adminpanel.test/api/blogs",
per_page: 20,
to: 2
}
},
showGrid: true
};
const getAllBlogsPreviewSpy = getAllBlogsPreview;
beforeEach(() => {
getAllBlogsPreviewSpy.mockImplementation(() => result);
});
afterEach(() => {
getAllBlogsPreviewSpy.mockRestore();
});
错误
FAIL UnitTests resources/js/tests/Blogs/Grid.test.js
● Test suite failed to run
Cannot find module 'getAllBlogsPreview' from 'Grid.test.js'
9 | import Grid from "../../containers/Grid/Grid";
10 | import getAllBlogsPreview from "../../containers/Grid/getAllBlogsPreview";
> 11 | jest.mock("getAllBlogsPreview");
| ^
12 |
13 | describe("Blog Grid", () => {
14 | const result = {
at Resolver.resolveModule (../../node_modules/jest-resolve/build/index.js:276:11)
at Object.<anonymous> (tests/Blogs/Grid.test.js:11:6)
解决方案
您应该模拟模块,而不仅仅是名称(它不只通过该名称知道模块,它需要一个路径):
jest.mock("../../containers/Grid/getAllBlogsPreview");
推荐阅读
- html - 在移动设备上更改容器的位置
- python - 连接到 gmail python3 的 smtp 错误 534
- salesforce - 使用 Salesforce 创建动态自定义字段
- amazon-web-services - 使用 route53 的 FQDN
- ruby - 为什么 `reduce` 不评估单元素数组的块?
- oauth - Azure DataFactory V2 - 带有 OAuth 令牌的 Salesforce Linkedservice
- java - 输入:- I Love India 输出:- I1 Love4 India5
- regex - 如何在正则表达式中编写 If 条件?
- database - 当我尝试连接到 Oracle TNS 时发现此错误:协议适配器错误
- python - 为状态更改的每条记录发送单独的邮件