[英]Test a function with jest
How can I test a function like the one below using jest? 如何使用Jest测试以下功能?
lightboxUrl: function () {
var componentId = this.options.componentId
if (componentId) {
var album = this.model.get('album')
var shortcut = this.model.get('shortcut')
return '/lightbox/' + componentId + '/' + album + '/' + shortcut
}
}
New to javascript Testing. javascript测试的新手。 Any help greatly appreciated.Let me know if you need anything else.
非常感谢您的帮助。让我知道您是否还有其他需要。
i just assumed and stubbed some of the not provided details. 我只是假设并添加了一些未提供的细节。
Here is the lightbox item 这是灯箱项目
export default function lightboxParent(componentId) {
return {
options: {
componentId,
},
model: {
get: id => (id === "album" ? "album" : "shortcut"),
},
lightboxUrl: function() {
var componentId = this.options.componentId;
if (componentId) {
var album = this.model.get("album");
var shortcut = this.model.get("shortcut");
return "/lightbox/" + componentId + "/" + album + "/" + shortcut;
}
}
}
}
Here is the jest test 这是开玩笑的测试
describe("lightbox url", () => {
it("should be valid url", () => {
expect(lightboxParent(1).lightboxUrl()).toBe(
"/lightbox/1/album/shortcut"
);
});
it("should not resolve valid url, no componentId", () => {
expect(lightboxParent().lightboxUrl()).toBe(undefined);
});
});
i used codesandbox for this, https://codesandbox.io/s/n4mlqkr4qj 我为此使用了codesandbox, https: //codesandbox.io/s/n4mlqkr4qj
This is a basic test just checking the url that the lightboxUrl
method is returning or if no componentId, that it's undefined
, as the entire src was provided and i made a few assumptions i just left it basic 这是一个基本测试,仅检查
lightboxUrl
方法正在返回的url或是否没有componentId( undefined
,因为提供了整个src,并且我做了一些假设,我只是将其保留为基本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.