开玩笑地测试功能

时间:2018-09-02 12:31:41

标签: javascript unit-testing jestjs

我如何使用笑话来测试下面的功能?

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
    }
  }

JavaScript测试的新手。非常感谢您的帮助。让我知道您是否还有其他需要。

1 个答案:

答案 0 :(得分:1)

我只是假设并添加了一些未提供的细节。

这是灯箱项目

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;
   }
  }
 }
}

这是开玩笑的测试

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);
  });
});

为此,我使用了codeandbox https://codesandbox.io/s/n4mlqkr4qj

这是一项基本测试,仅检查lightboxUrl方法正在返回的网址,或者如果没有componentId,则返回undefined,因为已提供了整个src,并且我做了一些假设基本

相关问题