Skip to content

Commit 90f3629

Browse files
committed
add a method to override browser for tests
1 parent c669868 commit 90f3629

File tree

4 files changed

+34
-27
lines changed

4 files changed

+34
-27
lines changed

src/js/utils/browser.js

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ const USER_AGENT = window.navigator && window.navigator.userAgent || '';
99
const webkitVersionMap = (/AppleWebKit\/([\d.]+)/i).exec(USER_AGENT);
1010
const appleWebkitVersion = webkitVersionMap ? parseFloat(webkitVersionMap.pop()) : null;
1111

12+
const browser = {};
13+
1214
/**
1315
* Whether or not this device is an iPad.
1416
*
1517
* @static
1618
* @const
1719
* @type {Boolean}
1820
*/
19-
export const IS_IPAD = (/iPad/i).test(USER_AGENT);
21+
export const IS_IPAD = browser.IS_IPAD = (/iPad/i).test(USER_AGENT);
2022

2123
/**
2224
* Whether or not this device is an iPhone.
@@ -28,7 +30,7 @@ export const IS_IPAD = (/iPad/i).test(USER_AGENT);
2830
// The Facebook app's UIWebView identifies as both an iPhone and iPad, so
2931
// to identify iPhones, we need to exclude iPads.
3032
// http://artsy.github.io/blog/2012/10/18/the-perils-of-ios-user-agent-sniffing/
31-
export const IS_IPHONE = (/iPhone/i).test(USER_AGENT) && !IS_IPAD;
33+
export const IS_IPHONE = browser.IS_IPHONE = (/iPhone/i).test(USER_AGENT) && !IS_IPAD;
3234

3335
/**
3436
* Whether or not this device is an iPod.
@@ -37,7 +39,7 @@ export const IS_IPHONE = (/iPhone/i).test(USER_AGENT) && !IS_IPAD;
3739
* @const
3840
* @type {Boolean}
3941
*/
40-
export const IS_IPOD = (/iPod/i).test(USER_AGENT);
42+
export const IS_IPOD = browser.IS_IPOD = (/iPod/i).test(USER_AGENT);
4143

4244
/**
4345
* Whether or not this is an iOS device.
@@ -46,7 +48,7 @@ export const IS_IPOD = (/iPod/i).test(USER_AGENT);
4648
* @const
4749
* @type {Boolean}
4850
*/
49-
export const IS_IOS = IS_IPHONE || IS_IPAD || IS_IPOD;
51+
export const IS_IOS = browser.IS_IOS = IS_IPHONE || IS_IPAD || IS_IPOD;
5052

5153
/**
5254
* The detected iOS version - or `null`.
@@ -55,7 +57,7 @@ export const IS_IOS = IS_IPHONE || IS_IPAD || IS_IPOD;
5557
* @const
5658
* @type {string|null}
5759
*/
58-
export const IOS_VERSION = (function() {
60+
export const IOS_VERSION = browser.IOS_VERSION = (function() {
5961
const match = USER_AGENT.match(/OS (\d+)_/i);
6062

6163
if (match && match[1]) {
@@ -71,7 +73,7 @@ export const IOS_VERSION = (function() {
7173
* @const
7274
* @type {Boolean}
7375
*/
74-
export const IS_ANDROID = (/Android/i).test(USER_AGENT);
76+
export const IS_ANDROID = browser.IS_ANDROID = (/Android/i).test(USER_AGENT);
7577

7678
/**
7779
* The detected Android version - or `null`.
@@ -80,7 +82,7 @@ export const IS_ANDROID = (/Android/i).test(USER_AGENT);
8082
* @const
8183
* @type {number|string|null}
8284
*/
83-
export const ANDROID_VERSION = (function() {
85+
export const ANDROID_VERSION = browser.ANDROID_VERSION = (function() {
8486
// This matches Android Major.Minor.Patch versions
8587
// ANDROID_VERSION is Major.Minor as a Number, if Minor isn't available, then only Major is returned
8688
const match = USER_AGENT.match(/Android (\d+)(?:\.(\d+))?(?:\.(\d+))*/i);
@@ -107,7 +109,7 @@ export const ANDROID_VERSION = (function() {
107109
* @const
108110
* @type {Boolean}
109111
*/
110-
export const IS_NATIVE_ANDROID = IS_ANDROID && ANDROID_VERSION < 5 && appleWebkitVersion < 537;
112+
export const IS_NATIVE_ANDROID = browser.IS_NATIVE_ANDROID = IS_ANDROID && ANDROID_VERSION < 5 && appleWebkitVersion < 537;
111113

112114
/**
113115
* Whether or not this is Mozilla Firefox.
@@ -116,7 +118,7 @@ export const IS_NATIVE_ANDROID = IS_ANDROID && ANDROID_VERSION < 5 && appleWebki
116118
* @const
117119
* @type {Boolean}
118120
*/
119-
export const IS_FIREFOX = (/Firefox/i).test(USER_AGENT);
121+
export const IS_FIREFOX = browser.IS_FIREFOX = (/Firefox/i).test(USER_AGENT);
120122

121123
/**
122124
* Whether or not this is Microsoft Edge.
@@ -125,7 +127,7 @@ export const IS_FIREFOX = (/Firefox/i).test(USER_AGENT);
125127
* @const
126128
* @type {Boolean}
127129
*/
128-
export const IS_EDGE = (/Edge/i).test(USER_AGENT);
130+
export const IS_EDGE = browser.IS_EDGE = (/Edge/i).test(USER_AGENT);
129131

130132
/**
131133
* Whether or not this is Google Chrome.
@@ -137,7 +139,7 @@ export const IS_EDGE = (/Edge/i).test(USER_AGENT);
137139
* @const
138140
* @type {Boolean}
139141
*/
140-
export const IS_CHROME = !IS_EDGE && ((/Chrome/i).test(USER_AGENT) || (/CriOS/i).test(USER_AGENT));
142+
export const IS_CHROME = browser.IS_CHROME = !IS_EDGE && ((/Chrome/i).test(USER_AGENT) || (/CriOS/i).test(USER_AGENT));
141143

142144
/**
143145
* The detected Google Chrome version - or `null`.
@@ -146,7 +148,7 @@ export const IS_CHROME = !IS_EDGE && ((/Chrome/i).test(USER_AGENT) || (/CriOS/i)
146148
* @const
147149
* @type {number|null}
148150
*/
149-
export const CHROME_VERSION = (function() {
151+
export const CHROME_VERSION = browser.CHROME_VERSION = (function() {
150152
const match = USER_AGENT.match(/(Chrome|CriOS)\/(\d+)/);
151153

152154
if (match && match[2]) {
@@ -162,7 +164,7 @@ export const CHROME_VERSION = (function() {
162164
* @const
163165
* @type {number|null}
164166
*/
165-
export const IE_VERSION = (function() {
167+
export const IE_VERSION = browser.IE_VERSION = (function() {
166168
const result = (/MSIE\s(\d+)\.\d/).exec(USER_AGENT);
167169
let version = result && parseFloat(result[1]);
168170

@@ -181,7 +183,7 @@ export const IE_VERSION = (function() {
181183
* @const
182184
* @type {Boolean}
183185
*/
184-
export const IS_SAFARI = (/Safari/i).test(USER_AGENT) && !IS_CHROME && !IS_ANDROID && !IS_EDGE;
186+
export const IS_SAFARI = browser.IS_SAFARI = (/Safari/i).test(USER_AGENT) && !IS_CHROME && !IS_ANDROID && !IS_EDGE;
185187

186188
/**
187189
* Whether or not this is any flavor of Safari - including iOS.
@@ -190,7 +192,7 @@ export const IS_SAFARI = (/Safari/i).test(USER_AGENT) && !IS_CHROME && !IS_ANDRO
190192
* @const
191193
* @type {Boolean}
192194
*/
193-
export const IS_ANY_SAFARI = (IS_SAFARI || IS_IOS) && !IS_CHROME;
195+
export const IS_ANY_SAFARI = browser.IS_ANY_SAFARI = (IS_SAFARI || IS_IOS) && !IS_CHROME;
194196

195197
/**
196198
* Whether or not this device is touch-enabled.
@@ -199,7 +201,18 @@ export const IS_ANY_SAFARI = (IS_SAFARI || IS_IOS) && !IS_CHROME;
199201
* @const
200202
* @type {Boolean}
201203
*/
202-
export const TOUCH_ENABLED = Dom.isReal() && (
204+
export const TOUCH_ENABLED = browser.TOUCH_ENABLE = Dom.isReal() && (
203205
'ontouchstart' in window ||
204206
window.navigator.maxTouchPoints ||
205207
window.DocumentTouch && window.document instanceof window.DocumentTouch);
208+
209+
/**
210+
* A function that can be used to override values in this module
211+
*
212+
* @param {Object} obj
213+
* The object that contains keys/values that should override
214+
* keys/values in this module
215+
*/
216+
export const override = function(obj) {
217+
Object.assign(browser, obj);
218+
};

test/unit/component.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ QUnit.test('should emit a tap event', function(assert) {
774774

775775
assert.expect(3);
776776
// Fake touch support. Real touch support isn't needed for this test.
777-
browser.TOUCH_ENABLED = true;
777+
browser.override({TOUCH_ENABLED: true});
778778

779779
comp.emitTapEvents();
780780
comp.on('tap', function() {
@@ -822,7 +822,7 @@ QUnit.test('should emit a tap event', function(assert) {
822822
comp.trigger('touchend');
823823

824824
// Reset to orignial value
825-
browser.TOUCH_ENABLED = origTouch;
825+
browser.override({TOUCH_ENABLED: origTouch});
826826
comp.dispose();
827827
});
828828

test/unit/player.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ QUnit.test('should add a touch-enabled classname when touch is supported', funct
629629
// Fake touch support. Real touch support isn't needed for this test.
630630
const origTouch = browser.TOUCH_ENABLED;
631631

632-
browser.TOUCH_ENABLED = true;
632+
browser.override({TOUCH_ENABLED: true});
633633

634634
const player = TestHelpers.makePlayer({});
635635

@@ -651,7 +651,7 @@ QUnit.test('should not add a touch-enabled classname when touch is not supported
651651

652652
assert.equal(player.el().className.indexOf('vjs-touch-enabled'), -1, 'touch-enabled classname not added');
653653

654-
browser.TOUCH_ENABLED = origTouch;
654+
browser.override({TOUCH_ENABLED: origTouch});
655655
player.dispose();
656656
});
657657

test/unit/poster.test.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
/* eslint-env qunit */
22
import PosterImage from '../../src/js/poster-image.js';
3-
import * as browser from '../../src/js/utils/browser.js';
43
import TestHelpers from './test-helpers.js';
54
import document from 'global/document';
65

76
QUnit.module('PosterImage', {
87
beforeEach() {
98
// Store the original background support so we can test different vals
10-
this.origVal = browser.BACKGROUND_SIZE_SUPPORTED;
119
this.poster1 = '#poster1';
1210
this.poster2 = '#poster2';
1311

@@ -27,14 +25,10 @@ QUnit.module('PosterImage', {
2725
}
2826
};
2927
},
30-
afterEach() {
31-
browser.BACKGROUND_SIZE_SUPPORTED = this.origVal;
32-
}
28+
afterEach() {}
3329
});
3430

3531
QUnit.test('should create and update a poster image', function(assert) {
36-
browser.BACKGROUND_SIZE_SUPPORTED = true;
37-
3832
const posterImage = new PosterImage(this.mockPlayer);
3933
let backgroundImage = posterImage.el().style.backgroundImage;
4034

0 commit comments

Comments
 (0)