Created
July 12, 2016 15:33
-
-
Save jamesjtong/df1cc66aafd8ee13502785e251f0479e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//util | |
let cachedBrowser = ''; | |
export function userAgent() { | |
return navigator.userAgent.toLowerCase(); | |
} | |
export function browser() { | |
if (cachedBrowser.length > 0) { | |
return cachedBrowser; | |
} else { | |
cachedBrowser = userAgent(); | |
return cachedBrowser; | |
} | |
} | |
export function isMobile() { | |
return /android|ipad|iphone|iemobile/i.test(browser()); | |
} | |
export function isNotMobile() { | |
return !this.isMobile(); | |
} | |
export function isSafari5() { | |
return /version\/5.+\ssafari\//i.test(browser()) && !browser().match(' Chrom'); | |
} | |
export function resetCachedBrowser() { | |
cachedBrowser = ''; | |
} | |
export function setCachedBrowser(string) { | |
cachedBrowser = string; | |
} | |
// Test | |
/* global sinon */ | |
import { | |
browser, | |
isMobile, | |
isNotMobile, | |
isSafari5, | |
resetCachedBrowser, | |
setCachedBrowser, | |
userAgent | |
} from 'player-code/util/user-agent-checker'; | |
import { module, test } from 'qunit'; | |
const browserGuy = { browser } | |
let sandbox; | |
module('Unit | Utility | user agent checker', { | |
beforeEach() { | |
sandbox = sinon.sandbox.create(); | |
}, | |
afterEach() { | |
sandbox.restore(); | |
resetCachedBrowser(); | |
} | |
}); | |
test('.browser()', function(assert) { | |
let result = browser(); | |
assert.equal(typeof result, 'string', 'returns a string identifying the browser useragent'); | |
}); | |
// TEST THAT ISNT WORKING | |
test('navigator user agent call is cached', function(assert) { | |
let spy = sandbox.spy('what is this?', 'browser'); | |
isSafari5(); | |
isMobile(); | |
isMobile(); | |
isMobile(); | |
isMobile(); | |
assert.equal(spy.callCount, 1, 'navigator.userAgent is only called once'); | |
}); | |
test('.isMobile', function(assert) { | |
setCachedBrowser('Mozilla/5.0 (Linux; U; Android 2.3; en-us) AppleWebKit/999+ (KHTML, like Gecko) Safari/999.9'); | |
assert.equal(isMobile(), true, 'returns true when using a mobile device'); | |
setCachedBrowser('Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko'); | |
assert.equal(isMobile(), false, 'returns false when using a desktop browser useragent'); | |
}); | |
test('when it is a safari 5 browser', function(assert) { | |
setCachedBrowser('Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; de-at) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1'); | |
assert.equal(isSafari5(), true, 'returns true when using safari 5'); | |
}); | |
test('when it isnt a safari 5 browser', function(assert) { | |
setCachedBrowser('Mozilla/5.0 (Linux; U; Android 2.3; en-us) AppleWebKit/999+ (KHTML, like Gecko) Safari/999.9'); | |
assert.equal(isSafari5(), false, 'returns false when not using safari 5'); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment