Skip to content

Commit

Permalink
Add unit tests for isAndroid
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesBrill committed Aug 26, 2020
1 parent 005cf7a commit 7ad1d2a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/isAndroid.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
const userAgent = typeof navigator !== 'undefined' ? navigator.userAgent : ''

export default () => /(android)/i.test(userAgent)
export default () => /(android)/i.test(typeof navigator !== 'undefined' ? navigator.userAgent : '')
32 changes: 32 additions & 0 deletions src/isAndroid.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import isAndroid from './isAndroid'

const mockUserAgent = (userAgent) => {
Object.defineProperty(navigator, 'userAgent', { value: userAgent, writable: true })
}

const mockUndefinedNavigator = () => {
Object.defineProperty(global, 'navigator', { value: undefined, writable: true })
}

describe('isAndroid', () => {
test('returns false when navigator.userAgent does not contain android string', () => {
mockUserAgent('safari browser')
const result = isAndroid()

expect(result).toEqual(false)
})

test('returns true when navigator.userAgent contains android string', () => {
mockUserAgent('android browser')
const result = isAndroid()

expect(result).toEqual(true)
})

test('returns false when navigator is undefined', () => {
mockUndefinedNavigator()
const result = isAndroid()

expect(result).toEqual(false)
})
})

0 comments on commit 7ad1d2a

Please sign in to comment.