acvetkov / sinon-chrome

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

sinon-chrome with then-chrome #33

Closed RichardWright closed 8 years ago

RichardWright commented 8 years ago

I can't get the sinon-chrome stubs to return when using promises. Any ideas??

The test itself:


    var mocha = require('mocha');
    var chai = require('chai');
    var sinon = require('sinon');
    var CookieFlip = require('../index');

    describe("Cookieflip", function() {

        beforeEach(function() {
            global.chrome = require('sinon-chrome');
        });

        afterEach(function() {
            global.chrome = {};
        });

        describe('cookieflip flow', function() {

            it('Load the active tab', function(done) {

                var dummyTabs = [];

                dummyTabs.push({url: 'testurl1'});
                dummyTabs.push({url: 'testurl2'});
                dummyTabs.push({url: 'testurl3'});

                chrome.tabs.query.withArgs({active: true, currentWindow:true}).yields(dummyTabs);

                var cookieFlip = new CookieFlip();

                cookieFlip.getCurrentTab()
                    .then(function(data) {
                        console.log('verify the new promise');
                        expect(data.url).to.equal('testurl');

                        done();
                });

                console.log('wait for the promise..');
            });
        });
    });

Code to test:


var thenChrome = require('then-chrome');

function CookieFlip() {};

CookieFlip.prototype.getCurrentTab = function() {

    return thenChrome.tabs.query({active: true, currentWindow: true})
        .then(function(tabs){
            return tabs[0];
    });
};

module.exports = CookieFlip;
RichardWright commented 8 years ago

I've got this code working in the browser, but in node..still bust.

acvetkov commented 8 years ago

@RichardWright, thanks for issue. Can you try to handle promise catch?

it('Load the active tab', function() {

   var dummyTabs = [];

   dummyTabs.push({url: 'testurl1'});
   dummyTabs.push({url: 'testurl2'});
   dummyTabs.push({url: 'testurl3'});

   chrome.tabs.query.withArgs({active: true, currentWindow:true}).yields(dummyTabs);

   var cookieFlip = new CookieFlip();

   return cookieFlip.getCurrentTab()
      .then(function(data) {
          console.log('verify the new promise');
          expect(data.url).to.equal('testurl');
      })
      .catch(err => console.log(err));
});