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

Commit a912406

Browse files
author
crdlc
committed
Bug 1129392 - Add tests for compatibility checker module
1 parent 7137a97 commit a912406

File tree

2 files changed

+110
-2
lines changed

2 files changed

+110
-2
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
'use strict';
2+
3+
require('unit/utils/mock_mozL10n.js');
4+
require('libs/lazy_loader.js');
5+
require('unit/utils/mock_mozL10n.js');
6+
7+
suite('Tests CompatibilityChecker', function() {
8+
9+
var cookie;
10+
11+
function getConfiguration(devices, minimumMajorVersion) {
12+
return {
13+
device: {
14+
names: devices
15+
},
16+
os: {
17+
minimumMajorVersion: minimumMajorVersion
18+
}
19+
};
20+
}
21+
22+
function restore(objects) {
23+
objects = Array.isArray(objects) ? objects : [objects];
24+
objects.forEach(object => object.restore());
25+
}
26+
27+
suiteSetup(function(done) {
28+
Object.defineProperty(navigator, 'userAgent', {
29+
configurable: true,
30+
get: () => 'Mozilla/5.0 (Mobile; rv:32.0) Gecko/32.0 Firefox/32.0'
31+
});
32+
33+
Object.defineProperty(document, 'cookie', {
34+
configurable: true,
35+
get: () => cookie,
36+
set: (value) => cookie = value
37+
});
38+
39+
require('js/compatibility_checker.js', () => done());
40+
});
41+
42+
setup(function() {
43+
document.cookie = '';
44+
});
45+
46+
test('CompatibilityChecker should exist', function() {
47+
chai.assert.isObject(CompatibilityChecker);
48+
});
49+
50+
test('Compatibility already confirmed', function(done) {
51+
document.cookie = 'compatibility=confirmed';
52+
CompatibilityChecker.check().then(done);
53+
});
54+
55+
test('Compatible device and OS', function(done) {
56+
sinon.stub(LazyLoader,
57+
'getJSON', () => Promise.resolve(getConfiguration(['Mozilla'], 32)));
58+
59+
CompatibilityChecker.check().then(() => {
60+
chai.assert.equal(document.cookie, 'compatibility=confirmed');
61+
restore(LazyLoader.getJSON);
62+
done();
63+
});
64+
});
65+
66+
test('Error getting JSON file', function(done) {
67+
sinon.stub(LazyLoader, 'getJSON', () => Promise.reject());
68+
69+
CompatibilityChecker.check().then(() => {
70+
restore(LazyLoader.getJSON);
71+
done();
72+
});
73+
});
74+
75+
test('Device not compatible', function(done) {
76+
sinon.stub(LazyLoader,
77+
'getJSON', () => Promise.resolve(getConfiguration(['Rocio Jurado'], 32)));
78+
79+
sinon.stub(window, 'alert', message => {
80+
chai.assert.equal(message, 'notCompatibleDevice');
81+
});
82+
83+
sinon.stub(window, 'close', () => {
84+
restore([LazyLoader.getJSON, window.alert, window.close]);
85+
done();
86+
});
87+
88+
CompatibilityChecker.check();
89+
});
90+
91+
test('Old OS version', function(done) {
92+
sinon.stub(LazyLoader,
93+
'getJSON', () => Promise.resolve(getConfiguration(['Mozilla'], 34)));
94+
95+
sinon.stub(window, 'alert', message => {
96+
chai.assert.equal(message, 'oldOSVersion');
97+
});
98+
99+
sinon.stub(window, 'close', () => {
100+
restore([LazyLoader.getJSON, window.alert, window.close]);
101+
done();
102+
});
103+
104+
CompatibilityChecker.check();
105+
});
106+
});

test/unit/utils/mock_mozL10n.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
var MockMozL10n = {
66
get: function(key, values) {
7-
return key + JSON.stringify(values);
8-
}
7+
return key + (values ? JSON.stringify(values) : '');
8+
},
9+
10+
readyState: 'complete'
911
}
1012

1113
global.navigator.mozL10n = MockMozL10n;

0 commit comments

Comments
 (0)