acvetkov / sinon-chrome

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

How to test chrome.browserAction functions? #38

Closed herodrigues closed 7 years ago

herodrigues commented 7 years ago

I've read the examples and everything else but I'm still confused on how to use sinon-chrome. The extension I'm building has a lot of communication between content and background scripts.

For example, when a user access some webpage (defined in my match rules), the extension icon must become active. Moreover, if the icon is clicked, a sidebar should open.

Currently, I'm trying to do something like this:

global.chrome = require('sinon-chrome');

describe('chrome.*', function () {
  global.chrome = {
    browserAction: {
      setIcon: {
        get: jasmine.createSpy('get'),
        set: jasmine.createSpy('set')
      }
    }
  };

  beforeEach(function () {
    spyOn(chrome.browserAction, 'setIcon');
    chrome.tabs.create({ url: 'http://www.customdomain.com' });
  });

  it('tracks chrome.tabs.onUpdated events', function () {
    expect(chrome.browserAction.setIcon).toHaveBeenCalled();
    console.log(chrome.browserAction.setIcon.callCount);
  })
});

Am I misunderstanding something?

acvetkov commented 7 years ago

Hi.

  1. You should not wrap chrome.browserAction methods in spies. They are already spies.
  2. sinon-chrome is not emulation of real chrome api. It's only sinon-stubs and event emitters
herodrigues commented 7 years ago

Thanks @acvetkov