Mocking Cordova in AngularJS

Submitted by kentr on Thu, 06/23/2016 - 16:22

Cordova attaches to the global window object, so it can be injected and mocked onto Angular's $window service:

/**
 * Test case for AngularJS module that does something
 * when platform = 'ios'
 */
describe('platform = "ios"', function() {

  var $window;
  beforeEach('inject', inject(function(_$window_) {
    $window = _$window_;

    $window.cordova = {
      platformId: 'ios',
    }
  }));

  it('verifies cordova mock platform = "ios"', function() {
    expect($window.cordova.platformId).to.equal('ios');
  });

  it('does something', function() {
    // ...
  });

});