Node/Koa2[62]:关注和取消-单元测试
·
Yin灏
数据准备
//test/testUserInfo.js
module.exports = {
Z_ID: 1,
Z_USER_NAME: "zhangsan",
Z_COOKIE: "XXXXX",
L_ID: 1,
L_USER_NAME: "lisi",
L_COOKIE: "XXXXX",
};
具体代码
// test/user/relation.test.js
const server = require("../server");
const { getFans, getFollowers } = require("../../src/controller/user-relation");
const {
Z_ID,
Z_USER_NAME,
Z_COOKIE,
L_ID,
L_USER_NAME,
L_COOKIE,
} = require("../testUserInfo");
// 不管张三有没有关注李四,先让张三取消关注李四
test("无论如何,先取消关注", async () => {
const res = await server
.post("/api/profile/unFollow")
.send({
userId: L_ID,
})
.set("cookie", Z_COOKIE);
expect(1).toBe(1);
});
// 添加关注
test("张三关注李四,应该成功", async () => {
const res = await server
.post("/api/profile/follow")
.send({
userId: L_ID,
})
.set("cookie", Z_COOKIE);
expect(res.body.error).toBe(0);
});
// 获取粉丝
test("获取李四的粉丝,应该有张三", async () => {
const result = await getFans(L_ID);
const { count, fansList } = result.data;
const hasUserName = fansList.some((fanInfo) => {
return fanInfo.userName === Z_USER_NAME;
});
expect(count > 0).toBe(true);
expect(hasUserName).toBe(true);
});
// 获取关注人
test("获取张三的关注人,应该有李四", async () => {
const result = await getFollowers(Z_ID);
const { count, followersList } = result.data;
const hasUserName = followersList.some((followersInfo) => {
return followersInfo.userName === L_USER_NAME;
});
expect(count > 0).toBe(true);
expect(hasUserName).toBe(true);
});
// 取消关注
test("张三取消关注李四,应该成功", async () => {
const res = await server
.post("/api/profile/unFollow")
.send({
userId: L_ID,
})
.set("cookie", Z_COOKIE);
expect(res.body.errno).toBe(0);
});