Skip to content

Commit

Permalink
Some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarkelov committed Mar 24, 2020
1 parent 87500ad commit 4847b5d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
33 changes: 15 additions & 18 deletions src/PlaywrightEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ import {
getPlaywrightInstance,
readConfig,
} from './utils'
import { Config, CHROMIUM, BrowserType } from './constants'
import {
Config,
CHROMIUM,
BrowserType,
Initializer,
InitializerProps,
Args,
RootProxy,
} from './constants'

const handleError = (error: Error): void => {
process.emit('uncaughtException', error)
Expand Down Expand Up @@ -102,19 +110,7 @@ class PlaywrightEnvironment extends NodeEnvironment {
browsers.map(browser => getPlaywrightInstance(browser, selectors)),
)

// Utils
type InitializerProps = {
browser: BrowserType
device?: string
}

type RootProxy = {
[key: string]: any
}

type Initializer = (args: InitializerProps) => Promise<any>
type Args = (string | Function)[]

// Helpers
const getResult = <T>(
data: T[],
instances: BrowserType[] | Array<keyof typeof DeviceDescriptors>,
Expand Down Expand Up @@ -179,13 +175,13 @@ class PlaywrightEnvironment extends NodeEnvironment {
browsers.map(browser => initialize<Page>(browser, pageInitializer)),
).then(data => getResult(data, browsers))

const checker = ({
const checker = <T>({
instance,
key,
args,
}: {
instance: any
key: any
key: keyof T
args: Args
}) => {
if (typeof instance[key] === 'function') {
Expand Down Expand Up @@ -213,11 +209,11 @@ class PlaywrightEnvironment extends NodeEnvironment {
return await Promise.all(
devices.map(device => {
const instance = browserInstance[device]
return checker({ instance, key, args })
return checker<T>({ instance, key, args })
}),
).then(data => getResult(data, devices))
} else {
return checker({ instance: browserInstance, key, args })
return checker<T>({ instance: browserInstance, key, args })
}
}),
).then(data => getResult(data, browsers))
Expand Down Expand Up @@ -258,6 +254,7 @@ class PlaywrightEnvironment extends NodeEnvironment {
this.global.context = proxyWrapper<BrowserContext>(contexts)
this.global.page = proxyWrapper<Page>(pages)
// TODO Types
// TODO Add expectWebkit, expectFirefox?
this.global.expectAllBrowsers = (input: any) =>
new Proxy(
{},
Expand Down
13 changes: 13 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,16 @@ export const DEFAULT_CONFIG: Config = {
browser: CHROMIUM,
exitOnPageError: true,
}

// Utils
export type InitializerProps = {
browser: BrowserType
device?: string
}

export type RootProxy = {
[key: string]: any
}

export type Initializer = (args: InitializerProps) => Promise<any>
export type Args = (string | Function)[]

0 comments on commit 4847b5d

Please sign in to comment.