Skip to content

Commit

Permalink
Fix the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Finesse committed Oct 18, 2024
1 parent ef7a769 commit 428a70d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 27 deletions.
4 changes: 3 additions & 1 deletion src/sources/app_version.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
getBrowserEngineVersion,
getBrowserVersion,
getOsMajorVersion,
isChromium,
Expand All @@ -13,15 +14,16 @@ describe('Sources', () => {
describe('appVersion', () => {
it('returns an expected value', () => {
const value = getAppVersion()
const version = getBrowserVersion() ?? { major: 0, minor: 0 }

if (isWebKit()) {
const version = getBrowserVersion() ?? { major: 0, minor: 0 }
expect(navigator.userAgent).toContain(value)
expect(value).toContain(`Version/${version.major}.${version.minor}`)
return
}

if (isChromium()) {
const version = getBrowserEngineVersion() ?? { major: 0, minor: 0 }
expect(navigator.userAgent).toContain(value)
expect(value).toContain(`Chrome/${version.major}.${version.minor}`)
return
Expand Down
9 changes: 0 additions & 9 deletions src/sources/document_element_keys.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import { getBrowserMajorVersion, isChromium } from '../../tests/utils'
import { BotdError } from '../types'
import getDocumentElementKeys from './document_element_keys'

describe('Sources', () => {
describe('documentElementKeys', () => {
it('returns expected values or throws', () => {
if (isChromium() && (getBrowserMajorVersion() ?? 0) < 61) {
expect(() => getDocumentElementKeys()).toThrow(
new BotdError(-2, 'document.documentElement.getAttributeNames is not a function'),
)
return
}

const result = getDocumentElementKeys()
// There are no document keys when running the tests using Karma
expect(result).toEqual([])
Expand Down
4 changes: 2 additions & 2 deletions src/sources/error_trace.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getBrowserMajorVersion, isChromium } from '../../tests/utils'
import { getBrowserEngineMajorVersion, isChromium } from '../../tests/utils'
import getErrorTrace from './error_trace'

describe('Sources', () => {
Expand All @@ -8,7 +8,7 @@ describe('Sources', () => {

if (isChromium()) {
expect(result).toContain(
(getBrowserMajorVersion() ?? 0) < 93
(getBrowserEngineMajorVersion() ?? 0) < 93
? "TypeError: Cannot read property '0' of null"
: "TypeError: Cannot read properties of null (reading '0')",
)
Expand Down
4 changes: 2 additions & 2 deletions src/sources/rtt.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { getBrowserMajorVersion, isChromium, isGecko, isWebKit } from '../../tests/utils'
import { isGecko, isWebKit } from '../../tests/utils'
import { BotdError } from '../types'
import getRTT from './rtt'

describe('Sources', () => {
describe('rtt', () => {
it('returns an expected value or throws', () => {
if (isGecko() || isWebKit() || (isChromium() && (getBrowserMajorVersion() ?? 0) < 61)) {
if (isGecko() || isWebKit()) {
expect(() => getRTT()).toThrow(new BotdError(-1, 'navigator.connection is undefined'))
return
}
Expand Down
9 changes: 3 additions & 6 deletions src/sources/user_agent.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { getBrowserVersion } from '../../tests/utils'
import { getBrowserEngineVersion } from '../../tests/utils'
import getUserAgent from './user_agent'

describe('Sources', () => {
describe('userAgent', () => {
it('returns an expected value', () => {
const value = getUserAgent()
const version = getBrowserVersion() ?? { major: 0, minor: 0 }

// We do not want to call ua-parser-js in these tests, as those likely
// use navigator.userAgent internally.
expect(value).toMatch(new RegExp(`(Version|Chrome|Firefox)/${version.major}.${version.minor}`))
const version = getBrowserEngineVersion() ?? { major: 0, minor: 0 }
expect(value).toMatch(new RegExp(`(Safari|Chrome|Firefox)/${version.major}.${version.minor}`))
})
})
})
8 changes: 1 addition & 7 deletions src/sources/webdriver.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { getBrowserMajorVersion, isChromium, isHeadlessChrome } from '../../tests/utils'
import { isHeadlessChrome } from '../../tests/utils'
import getWebDriver from './webdriver'
import { BotdError } from '../types'

describe('Sources', () => {
describe('webdriver', () => {
it('returns an expected value or throws', () => {
if (isChromium() && (getBrowserMajorVersion() ?? 0) < 63) {
expect(() => getWebDriver()).toThrow(new BotdError(-1, 'navigator.webdriver is undefined'))
return
}

const result = getWebDriver()

if (isHeadlessChrome()) {
Expand Down
20 changes: 20 additions & 0 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ export function getBrowserMajorVersion(): number | undefined {
return parseInt(version.split('.')[0])
}

export function getBrowserEngineMajorVersion(): number | undefined {
const version = new UAParser().getEngine().version
if (version === undefined) {
return undefined
}
return parseInt(version.split('.')[0])
}

export function getBrowserVersion(): { major: number; minor: number } | undefined {
const version = new UAParser().getBrowser().version
if (version === undefined) {
Expand All @@ -61,3 +69,15 @@ export function getBrowserVersion(): { major: number; minor: number } | undefine
minor: parseInt(version.split('.')[1]),
}
}

export function getBrowserEngineVersion(): { major: number; minor: number } | undefined {
const version = new UAParser().getEngine().version
if (version === undefined) {
return undefined
}

return {
major: parseInt(version.split('.')[0]),
minor: parseInt(version.split('.')[1]),
}
}

0 comments on commit 428a70d

Please sign in to comment.