经过多次研究,我从来没有找到一个具体的例子来帮助我理解这一点。
我想要实现的目标:模拟 api 并测试它是否正确渲染。
有人可以给我一个代码示例,看看如何用 jest 来具体测试它,这样我就可以将它应用到我项目中的所有其他函数中,我真的不明白怎么做。
谢谢您的帮助。
来自 api 文件夹的 REAL API
export const searchTrack = search => {
return fetch(
`${url}/search/tracks?q=${encodeURIComponent(
search
)}&limit=250&media=music`,
{
method: "GET",
headers: {
Authorization: Cookies.get("token")
}
}
)
.then(response => {
return response.json();
})
.then(jsonFormat => {
return jsonFormat.results;
})
.catch(() => {
console.error("fetch for search dont work");
});
};
调用 addTracksPlaylist.vue 方法
methods: {
search() {
if (this.term !== "") {
this.results = [];
this.searching = true;
apiPlaylist
.searchTrack(this.term)
.then(res => {
if (res.status != 401) {
this.searching = false;
this.results = res;
this.noResults = this.results.length === 0;
}
})
.catch(() => {
this.$router.push("/login");
});
}
}
}
我看到每个人都在 api 文件夹中创建了一个 __mocks__ 文件夹,所以我用 api GET return 创建了一个
{
wrapperType: "track",
kind: "song",
artistId: 461932,
collectionId: 196480323,
trackId: 196480329,
artistName: "Europe",
collectionName: "The Final Countdown",
trackName: "The Final Countdown",
collectionCensoredName: "The Final Countdown",
trackCensoredName: "The Final Countdown",
artistViewUrl: "https://itunes.apple.com/us/artist/europe/id461932?uo=4",
collectionViewUrl:
"https://itunes.apple.com/us/album/the-final-countdown/id196480323?i=196480329&uo=4",
trackViewUrl:
"https://itunes.apple.com/us/album/the-final-countdown/id196480323?i=196480329&uo=4",
previewUrl:
"http://a1815.phobos.apple.com/us/r1000/101/Music/70/f0/fd/mzm.hhpjhkpl.aac.p.m4a",
artworkUrl30:
},
{}
]
};
隔江千里
相关分类