Skip to content

Commit

Permalink
Improve tests (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarkelov authored Mar 6, 2020
1 parent 49897b3 commit 0acc09b
Showing 1 changed file with 67 additions and 3 deletions.
70 changes: 67 additions & 3 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import fs from 'fs'
import path from 'path'
import {
import * as Utils from './utils'
import { DEFAULT_CONFIG, CHROMIUM } from './constants'

const {
readConfig,
getBrowserType,
getDeviceType,
checkBrowserEnv,
checkDeviceEnv,
readPackage,
} from './utils'
import { DEFAULT_CONFIG, CHROMIUM } from './constants'
getPlaywrightInstance,
} = Utils

jest.spyOn(fs, 'exists')

Expand Down Expand Up @@ -221,3 +224,64 @@ describe('readPackage', () => {
expect(playwright).toEqual('core')
})
})

describe('getPlaywrightInstance', () => {
const spy = jest.spyOn(Utils, 'readPackage')

it('should return specified instance from playwright package', async () => {
spy.mockResolvedValue('playwright')

jest.doMock('playwright', () => ({
firefox: 'firefox',
}))

const instance = await getPlaywrightInstance('firefox')
expect(instance).toEqual('firefox')
})

it('should register passed selectors for playwright package', async () => {
spy.mockResolvedValue('playwright')

const register = jest.fn().mockResolvedValue('registered')

jest.doMock('playwright', () => ({
firefox: 'firefox',
selectors: { register },
}))

const selectors = [{ name: 'test', script: 'test' }]

const instance = await getPlaywrightInstance('firefox', selectors)
expect(register).toHaveBeenLastCalledWith('test', { name: 'test' })
expect(instance).toEqual('firefox')
})

it('should return specified instance from specified playwright package', async () => {
spy.mockResolvedValue('chromium')

jest.doMock('playwright-chromium', () => ({
chromium: 'chromium',
}))

const instance = await getPlaywrightInstance('chromium')
expect(instance).toEqual('chromium')
})

it('should return specified instance from playwright-core package and download if it is needed', async () => {
spy.mockResolvedValue('core')

const downloadBrowserIfNeeded = jest.fn().mockResolvedValue('downloaded')

const webkit = {
downloadBrowserIfNeeded,
}

jest.doMock('playwright-core', () => ({
webkit,
}))

const instance = await getPlaywrightInstance('webkit')
expect(downloadBrowserIfNeeded).toHaveBeenCalled()
expect(instance).toEqual(webkit)
})
})

0 comments on commit 0acc09b

Please sign in to comment.