acvetkov / sinon-chrome

Testing chrome extensions with Node.js
ISC License
434 stars 46 forks source link

question(s): how to trigger client connection #79

Open bboydflo opened 6 years ago

bboydflo commented 6 years ago

I am not sure exactly how to actually trigger a connection using sinon-chrome. Here is my test:

const browser = require('sinon-chrome/webextensions');
const sinon = require('sinon');

it("multiple content scripts can connect to the same background script", () => {
    let onConnect = sinon.spy();

    browser.runtime.onConnect.addListener(onConnect);

    browser.runtime.connect(null, { name: 'one' });
    browser.runtime.connect(null, { name: 'two' });

    browser.runtime.onConnect.trigger();
    browser.runtime.onConnect.trigger();

    assert(onConnect.callCount === 2, 'on connect has been called 2 times');
});

This seems to work but I am not sure why. I was expecting that I don't need to call browser.runtime.onConnect.trigger();

What is the best way to improve documentation? I have a hard time using sinon-chrome as a new user.