Skip to content

Commit 1ec2629

Browse files
committed
add basic test suite to isMobile browser utility
1 parent 50a4f4b commit 1ec2629

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

src/utils/browser.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
export function isMobile (agent = navigator.userAgent) {
2+
return /Android|iPhone|iPad|iPod/i.test(agent)
3+
}
4+
5+
16
export function isNode (target) {
27
return typeof window.Node === 'object'
38
? target instanceof window.Node
@@ -31,9 +36,3 @@ export function transitionSupported () {
3136
const style = document.documentElement.style
3237
return 'transition' in style || 'WebkitTransition' in style
3338
}
34-
35-
36-
/* istanbul ignore next */
37-
export function isMobile () {
38-
return /Android|iPhone|iPad|iPod/i.test(navigator.userAgent)
39-
}

test/utils/browser.spec.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,40 @@
1-
import { isNode, isNodeList, transformSupported, transitionSupported } from '../../src/utils/browser'
1+
import { isMobile, isNode, isNodeList, transformSupported, transitionSupported } from '../../src/utils/browser'
22

33

44
describe('Browser Utilities', () => {
55

6+
describe('isMobile()', () => {
7+
8+
it('should return true when passed a mobile user agent', () => {
9+
10+
const android = `Mozilla/5.0 (Linux; U; Android 4.2; en-us;
11+
Android SDK built for x86 Build/JOP40C) AppleWebKit/534.30
12+
(KHTML, like Gecko) Version/4.0 Mobile Safari/534.30`
13+
14+
const iPhone = `Mozilla/5.0 (iPhone; CPU iPhone OS 10_10_5 like Mac OS X)
15+
AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12B411 Safari/600.1.4`
16+
17+
expect(isMobile(android)).to.be.true
18+
expect(isMobile(iPhone)).to.be.true
19+
})
20+
21+
it('should return false when passed a desktop user agent', () => {
22+
23+
const chrome = `Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36
24+
(KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36`
25+
26+
const firefox = `Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1;
27+
WOW64; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E)`
28+
29+
expect(isMobile(chrome)).to.be.false
30+
expect(isMobile(firefox)).to.be.false
31+
})
32+
33+
it('should work when not passed an explicit user agent', () => {
34+
expect(isMobile()).to.be.a('boolean')
35+
})
36+
})
37+
638
describe('isNode()', () => {
739

840
it('should return true when passed a DOM node', () => {

0 commit comments

Comments
 (0)