Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easier testing with browser.runtime.connect/browser.runtime.connectNative #50

Open
freaktechnik opened this issue Mar 2, 2017 · 1 comment

Comments

@freaktechnik
Copy link
Contributor

The stubs currently don't return any value by default, which is fine in general, but connect/connectNative return a Port instance. It'd be nice if there was an easy way to get a prepared Port object, since this package already has much of the infrastructure for the events.

@ddehghan
Copy link

ddehghan commented Dec 24, 2018

After a lot of waisting time i managed to get this work. Put that in your test.ts in an angular app. Or some other setup environment file if you are using a different framework.

 import * as chrome from 'sinon-chrome';
(window as any).global.chrome = chrome;

/*
  Mock-implementation of chrome.runtime messaging API
*/
const Messager = () => {
const _listeners = [];

return {
    onMessage: {
        addListener: cb => _listeners.push(cb)
    },

    onDisconnect: {
        addListener: cb => {}
    },

    postMessage: data => {
        _listeners.forEach(cb => cb.call(this, data));
    }
 };
 };

chrome.runtime.connect.returns(Messager());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants