From 482b4d7cbe7e858ba8318e286e386f7d622c512e Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Fri, 9 Sep 2016 14:59:55 -0400 Subject: [PATCH] mock userAgents in a way that works in both Phantom and real browsers --- test/spec/services/mapillary.js | 32 +++++++++++++++++++++++++------- test/spec/ui/cmd.js | 24 ++++++++++++++++++------ 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/test/spec/services/mapillary.js b/test/spec/services/mapillary.js index 4ed11e73f..fb4cec214 100644 --- a/test/spec/services/mapillary.js +++ b/test/spec/services/mapillary.js @@ -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; }); }); diff --git a/test/spec/ui/cmd.js b/test/spec/ui/cmd.js index ee637b540..e1ba3d5d2 100644 --- a/test/spec/ui/cmd.js +++ b/test/spec/ui/cmd.js @@ -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 */ });