代码之家  ›  专栏  ›  技术社区  ›  Ravi

用jest测试函数

  •  0
  • Ravi  · 技术社区  · 6 年前

    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 回复  |  直到 6 年前
        1
  •  1
  •   Shannon Johnstone    6 年前

    我只是假设了一些没有提供的细节。

    这是灯箱

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

    我用了密码沙盒, https://codesandbox.io/s/n4mlqkr4qj

    lightboxUrl 方法返回,或者如果没有组件,则返回 undefined