Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #554 from crdlc/bug-1129392
Browse files Browse the repository at this point in the history
Bug 1129392 - Add tests for compatibility checker module
  • Loading branch information
Cristian Rodriguez committed Feb 5, 2015
2 parents 95fec0f + a912406 commit ddde82b
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 2 deletions.
106 changes: 106 additions & 0 deletions test/unit/test_compatibility_checker.js
@@ -0,0 +1,106 @@
'use strict';

require('unit/utils/mock_mozL10n.js');
require('libs/lazy_loader.js');
require('unit/utils/mock_mozL10n.js');

suite('Tests CompatibilityChecker', function() {

var cookie;

function getConfiguration(devices, minimumMajorVersion) {
return {
device: {
names: devices
},
os: {
minimumMajorVersion: minimumMajorVersion
}
};
}

function restore(objects) {
objects = Array.isArray(objects) ? objects : [objects];
objects.forEach(object => object.restore());
}

suiteSetup(function(done) {
Object.defineProperty(navigator, 'userAgent', {
configurable: true,
get: () => 'Mozilla/5.0 (Mobile; rv:32.0) Gecko/32.0 Firefox/32.0'
});

Object.defineProperty(document, 'cookie', {
configurable: true,
get: () => cookie,
set: (value) => cookie = value
});

require('js/compatibility_checker.js', () => done());
});

setup(function() {
document.cookie = '';
});

test('CompatibilityChecker should exist', function() {
chai.assert.isObject(CompatibilityChecker);
});

test('Compatibility already confirmed', function(done) {
document.cookie = 'compatibility=confirmed';
CompatibilityChecker.check().then(done);
});

test('Compatible device and OS', function(done) {
sinon.stub(LazyLoader,
'getJSON', () => Promise.resolve(getConfiguration(['Mozilla'], 32)));

CompatibilityChecker.check().then(() => {
chai.assert.equal(document.cookie, 'compatibility=confirmed');
restore(LazyLoader.getJSON);
done();
});
});

test('Error getting JSON file', function(done) {
sinon.stub(LazyLoader, 'getJSON', () => Promise.reject());

CompatibilityChecker.check().then(() => {
restore(LazyLoader.getJSON);
done();
});
});

test('Device not compatible', function(done) {
sinon.stub(LazyLoader,
'getJSON', () => Promise.resolve(getConfiguration(['Rocio Jurado'], 32)));

sinon.stub(window, 'alert', message => {
chai.assert.equal(message, 'notCompatibleDevice');
});

sinon.stub(window, 'close', () => {
restore([LazyLoader.getJSON, window.alert, window.close]);
done();
});

CompatibilityChecker.check();
});

test('Old OS version', function(done) {
sinon.stub(LazyLoader,
'getJSON', () => Promise.resolve(getConfiguration(['Mozilla'], 34)));

sinon.stub(window, 'alert', message => {
chai.assert.equal(message, 'oldOSVersion');
});

sinon.stub(window, 'close', () => {
restore([LazyLoader.getJSON, window.alert, window.close]);
done();
});

CompatibilityChecker.check();
});
});
6 changes: 4 additions & 2 deletions test/unit/utils/mock_mozL10n.js
Expand Up @@ -4,8 +4,10 @@

var MockMozL10n = {
get: function(key, values) {
return key + JSON.stringify(values);
}
return key + (values ? JSON.stringify(values) : '');
},

readyState: 'complete'
}

global.navigator.mozL10n = MockMozL10n;
Expand Down

0 comments on commit ddde82b

Please sign in to comment.