mock userAgents in a way that works in both Phantom and real browsers

This commit is contained in:
Bryan Housel
2016-09-09 14:59:55 -04:00
parent 273407da3d
commit 482b4d7cbe
2 changed files with 43 additions and 13 deletions
+25 -7
View File
@@ -1,6 +1,10 @@
describe('iD.services.mapillary', function() {
var dimensions = [64, 64],
context, server, mapillary;
ua = navigator.userAgent,
isPhantom = (navigator.userAgent.match(/PhantomJS/) !== null),
uaMock = function () { return ua; },
context, server, mapillary, orig;
beforeEach(function() {
context = iD.Context(window).assetPath('../dist/');
@@ -10,10 +14,28 @@ describe('iD.services.mapillary', function() {
server = sinon.fakeServer.create();
mapillary = iD.services.mapillary.init();
mapillary.reset();
/* eslint-disable no-native-reassign */
/* mock userAgent */
if (isPhantom) {
orig = navigator;
navigator = Object.create(orig, { userAgent: { get: uaMock }});
} else {
orig = navigator.__lookupGetter__('userAgent');
navigator.__defineGetter__('userAgent', uaMock);
}
});
afterEach(function() {
server.restore();
/* restore userAgent */
if (isPhantom) {
navigator = orig;
} else {
navigator.__defineGetter__('userAgent', orig);
}
/* eslint-enable no-native-reassign */
});
@@ -324,17 +346,13 @@ describe('iD.services.mapillary', function() {
describe('#signsSupported', function() {
it('returns false for Internet Explorer', function() {
var detect = iD.detect;
iD.detect = function() { return { ie: true, browser: 'Internet Explorer' }; };
ua = 'Trident/7.0; rv:11.0';
expect(mapillary.signsSupported()).to.be.false;
iD.detect = detect;
});
it('returns false for Safari', function() {
var detect = iD.detect;
iD.detect = function() { return { ie: false, browser: 'Safari' }; };
ua = 'Version/9.1 Safari/601';
expect(mapillary.signsSupported()).to.be.false;
iD.detect = detect;
});
});
+18 -6
View File
@@ -1,16 +1,28 @@
describe('iD.ui.cmd', function () {
var origNavigator, ua;
var orig,
ua = navigator.userAgent,
isPhantom = (navigator.userAgent.match(/PhantomJS/) !== null),
uaMock = function () { return ua; };
beforeEach(function() {
/* eslint-disable no-native-reassign */
origNavigator = navigator;
navigator = Object.create(origNavigator, {
userAgent: { get: function() { return ua; } }
});
/* mock userAgent */
if (isPhantom) {
orig = navigator;
navigator = Object.create(orig, { userAgent: { get: uaMock }});
} else {
orig = navigator.__lookupGetter__('userAgent');
navigator.__defineGetter__('userAgent', uaMock);
}
});
afterEach(function() {
navigator = origNavigator;
/* restore userAgent */
if (isPhantom) {
navigator = orig;
} else {
navigator.__defineGetter__('userAgent', orig);
}
/* eslint-enable no-native-reassign */
});