Skip to content

Commit

Permalink
create test for createDeferredListener
Browse files Browse the repository at this point in the history
  • Loading branch information
SidneyNemzer committed Jun 15, 2024
1 parent 5a28336 commit 933a774
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions test/listener.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import sinon from "sinon";
import { createDeferredListener } from "../src/listener";

describe("createDeferredListener", () => {
it("queues calls to the listener", async () => {
const { setListener, listener } = createDeferredListener();
const spy = sinon.spy();

// Trigger a couple of events
listener("message", "sender", "sendResponse");
listener("message2", "sender2", "sendResponse2");

// Listener should receive previous messages
setListener(spy);

// Trigger more events
listener("message3", "sender3", "sendResponse3");
listener("message4", "sender4", "sendResponse4");

await Promise.resolve();

spy.callCount.should.equal(4);
spy.getCall(0).args.should.eql(["message", "sender", "sendResponse"]);
spy.getCall(1).args.should.eql(["message2", "sender2", "sendResponse2"]);
spy.getCall(2).args.should.eql(["message3", "sender3", "sendResponse3"]);
spy.getCall(3).args.should.eql(["message4", "sender4", "sendResponse4"]);
});
});

0 comments on commit 933a774

Please sign in to comment.