带有Mocha的单元测试事件

问题描述 投票:0回答:1

不确定从哪里开始,或者用mocha和chai测试以下代码的最佳方法是什么:

module.exports = function trackCustomDyEvents(events){
let $;

trackEvents();

function trackEvents() {
    events.forEach((event) => {
        $(document).on(event.type, event.element, () => {
            if (!event.predicate()) return;
            window.DY.API('event', {
                name: event.name
            });
        });
    });
}

};

node.js mocha chai
1个回答
0
投票

这里是单元测试解决方案,您需要一个sinon.js之类的存根库,并且我假设您使用jquery

index.js

window.DY = window.DY || {};
window.DY.API = function(event, obj) {
  // Your real implementation
};

module.exports = function trackCustomDyEvents(events) {
  const $ = require("jquery");
  trackEvents();
  function trackEvents() {
    events.forEach((event) => {
      $(document).on(event.type, event.element, () => {
        if (!event.predicate()) return;
        window.DY.API("event", {
          name: event.name,
        });
      });
    });
  }
};

index.test.js

const jsdom = require("jsdom");
const sinon = require("sinon");
const proxyquire = require("proxyquire");

describe("trackCustomDyEvents", () => {
  before(() => {
    const html = '<!doctype html><html><head><meta charset="utf-8">' + "</head><body></body></html>";
    const url = "http://localhost";
    const document = new jsdom.JSDOM(html, { url });
    const window = document.window;
    global.document = window.document;
    global.window = window;
  });
  afterEach(() => {
    sinon.restore();
  });
  it("should call DY.API", () => {
    const onStub = sinon.stub().yields();
    const jqueryStub = sinon.stub().callsFake(() => ({
      on: onStub,
    }));
    const trackCustomDyEvents = proxyquire("./", {
      jquery: jqueryStub,
    });
    const APISpy = sinon.stub(window.DY, "API");
    const predicateStub = sinon.stub().returns(true);
    const events = [
      { type: "click", element: "button", name: "a", predicate: predicateStub },
      { type: "submit", element: "form", name: "b", predicate: predicateStub },
    ];
    trackCustomDyEvents(events);
    sinon.assert.calledWithExactly(onStub.firstCall, "click", "button", sinon.match.func);
    sinon.assert.calledWithExactly(onStub.secondCall, "submit", "form", sinon.match.func);
    sinon.assert.calledTwice(predicateStub);
    sinon.assert.calledWithExactly(APISpy.firstCall, "event", { name: "a" });
    sinon.assert.calledWithExactly(APISpy.secondCall, "event", { name: "b" });
  });

  it("should not call DY.API", () => {
    const onStub = sinon.stub().yields();
    const jqueryStub = sinon.stub().callsFake(() => ({
      on: onStub,
    }));
    const trackCustomDyEvents = proxyquire("./", {
      jquery: jqueryStub,
    });
    const APISpy = sinon.stub(window.DY, "API");
    const predicateStub = sinon.stub().returns(false);
    const events = [
      { type: "click", element: "button", name: "a", predicate: predicateStub },
      { type: "submit", element: "form", name: "b", predicate: predicateStub },
    ];
    trackCustomDyEvents(events);
    sinon.assert.calledWithExactly(onStub.firstCall, "click", "button", sinon.match.func);
    sinon.assert.calledWithExactly(onStub.secondCall, "submit", "form", sinon.match.func);
    sinon.assert.calledTwice(predicateStub);
    sinon.assert.notCalled(APISpy);
  });
});

单元测试结果覆盖率100%:

  trackCustomDyEvents
    ✓ should call DY.API (76ms)
    ✓ should not call DY.API


  2 passing (146ms)

---------------|----------|----------|----------|----------|-------------------|
File           |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files      |      100 |      100 |    91.67 |      100 |                   |
 index.js      |      100 |      100 |       80 |      100 |                   |
 index.test.js |      100 |      100 |      100 |      100 |                   |
---------------|----------|----------|----------|----------|-------------------|

源代码:https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/59569112

© www.soinside.com 2019 - 2024. All rights reserved.