Skip to content

Commit

Permalink
feat: improve types export and name
Browse files Browse the repository at this point in the history
  • Loading branch information
andostronaut committed Dec 17, 2024
1 parent 0b6df3f commit c72fd29
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 66 deletions.
40 changes: 22 additions & 18 deletions src/driver.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { assert, Builder, By, isEmpty, join, Kia } from '../deps.ts'
import type {
TCaseFn,
TConfigJSON,
TData,
TDriverParams,
TDriverServiceCaseParamsBuilder,
TDrowserDriverResponse,
TDrowserServiceCase,
TDrowserThenableWebDriver,
CaseFn,
ConfigJSON,
Data,
DriverParams,
DriverServiceCaseParamsBuilder,
DrowserDriverResponse,
DrowserServiceCase,
DrowserThenableWebDriver,
} from './types.ts'
import { isValidHttpUrl, result as resultData } from './utils.ts'
import {
Expand All @@ -20,13 +20,13 @@ import { exportGeneratedLog, exportJSONReport } from './export.ts'

const driver = async ({
browser,
}: TDriverParams): Promise<TDrowserDriverResponse> => {
const data: TData = { url: '', results: [] }
}: DriverParams): Promise<DrowserDriverResponse> => {
const data: Data = { url: '', results: [] }
const configPath = join(Deno.cwd(), 'drowser.json')

try {
await Deno.stat(configPath)
const { url }: TConfigJSON = JSON.parse(
const { url }: ConfigJSON = JSON.parse(
await Deno.readTextFile(configPath),
)

Expand All @@ -39,7 +39,9 @@ const driver = async ({
data.url = url
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
throw new Error('An error occurred, please create drowser.json file.')
throw new Error(
'An error occurred, please create drowser.json file.',
)
}

if (!(error instanceof Deno.errors.NotFound)) {
Expand All @@ -50,15 +52,17 @@ const driver = async ({
}

if (isEmpty(browser) || !driverBrowserList.includes(browser)) {
throw new Error('An error occurred, please provide a valid browser driver')
throw new Error(
'An error occurred, please provide a valid browser driver',
)
}

return new Promise<TDrowserDriverResponse>((resolve, reject) => {
return new Promise<DrowserDriverResponse>((resolve, reject) => {
if (isEmpty(data.url) || !isValidHttpUrl({ url: data.url })) reject()

const builder = new Builder()
.forBrowser(driverBrowsers[browser])
.build() as TDrowserThenableWebDriver
.build() as DrowserThenableWebDriver

const service = { cases: [] }

Expand All @@ -75,16 +79,16 @@ const driver = async ({
.finally(() => {
const methodPromises: Promise<void>[] = []

service.cases.forEach((c: TDrowserServiceCase) => {
service.cases.forEach((c: DrowserServiceCase) => {
if (typeof c === 'object') {
const omitedBuilder =
builder as unknown as TDriverServiceCaseParamsBuilder
builder as unknown as DriverServiceCaseParamsBuilder
const megaBuilder = {
builder: omitedBuilder,
assert,
by: By,
}
const method = c.fn as TCaseFn
const method = c.fn as CaseFn
const methodPromise = method(megaBuilder)

const start = performance.now()
Expand Down
97 changes: 49 additions & 48 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,84 @@
import { assert } from '../deps.ts'
import type { By, ThenableWebDriver } from '../deps.ts'

export type TDriverParams = {
browser: TDriverBrowser
export type DriverParams = {
browser: DriverBrowser
}

export type TDriverBrowser = 'chrome' | 'firefox' | 'safari' | 'edge'
export type DriverBrowser = 'chrome' | 'firefox' | 'safari' | 'edge'

export type TConfigJSON = {
export type ConfigJSON = {
url: string
exportPdf: boolean
}

export type TDataResult = {
export type DataResult = {
id?: string
name: string
status: string
timestamp?: Date
duration: number
month_of_test?: string
browser: TDriverBrowser
browser: DriverBrowser
}

export type TData = {
export type Data = {
url: string
results: Array<TDataResult>
results: Array<DataResult>
}

export type TDrowserThenableWebDriver = ThenableWebDriver
export type DrowserThenableWebDriver = ThenableWebDriver

export type TDrowserBuilder = Omit<
export type DrowserBuilder = Omit<
ThenableWebDriver,
'get'
>

export type TDriverServiceCaseParamsBuilder = Omit<
export type DriverServiceCaseParamsBuilder = Omit<
ThenableWebDriver,
'get' | 'quit' | 'then' | 'catch' | 'close' | 'finally'
>

export type TDriverServiceCaseParamsAssert = typeof assert
export type DriverServiceCaseParamsAssert = typeof assert

export type TDriverServiceCaseParamsBy = typeof By
export type DriverServiceCaseParamsBy = typeof By

export type TDriverBrowserCaseParams = {
builder: TDriverServiceCaseParamsBuilder
assert: TDriverServiceCaseParamsAssert
by: TDriverServiceCaseParamsBy
export type DriverBrowserCaseParams = {
builder: DriverServiceCaseParamsBuilder
assert: DriverServiceCaseParamsAssert
by: DriverServiceCaseParamsBy
}

export type TDrowserServiceCase = {
export type DrowserServiceCase = {
name: string
fn: (
params: TDriverBrowserCaseParams,
params: DriverBrowserCaseParams,
) => void
}

export type TDrowserService = {
cases: Array<TDrowserServiceCase>
export type DrowserService = {
cases: Array<DrowserServiceCase>
}

export type TCaseFn = (
params: TDriverBrowserCaseParams,
export type CaseFn = (
params: DriverBrowserCaseParams,
) => Promise<void>

export type TDrowserDriverResponse = {
service: TDrowserService
export type DrowserDriverResponse = {
service: DrowserService
}

export type TAssertFunction = (
export type AssertFunction = (
actual: unknown,
expected: unknown,
msg?: string,
) => void

export type TAssertError = {
export type AssertError = {
name: string
}

export type TIsValidHttpUrlParams = {
export type IsValidHttpUrlParams = {
url: string
}

Expand All @@ -102,7 +102,7 @@ export type MonthValue = {
value: number
}

export type TJSON = {
export type ReportSchema = {
drowser: {
metadata: {
current_month: string
Expand Down Expand Up @@ -131,35 +131,36 @@ export type TJSON = {
coverage: number
flaky: number
month_of_test: string
browser: TDriverBrowser
cases: Array<TDataResult>
browser: DriverBrowser
cases: Array<DataResult>
},
]
}
}

const types = {
TDriverParams: {} as TDriverParams,
TDriverBrowser: {} as TDriverBrowser,
TConfigJSON: {} as TConfigJSON,
TData: {} as TData,
TDrowserThenableWebDriver: {} as TDrowserThenableWebDriver,
TDrowserBuilder: {} as TDrowserBuilder,
TDriverServiceCaseParamsBuilder: {} as TDriverServiceCaseParamsBuilder,
TDriverServiceCaseParamsAssert: {} as TDriverServiceCaseParamsAssert,
TDriverServiceCaseParamsBy: {} as TDriverServiceCaseParamsBy,
TDriverBrowserCaseParams: {} as TDriverBrowserCaseParams,
TDrowserServiceCase: {} as TDrowserServiceCase,
TDrowserService: {} as TDrowserService,
TCaseFn: {} as TCaseFn,
TDrowserDriverResponse: {} as TDrowserDriverResponse,
TAssertFunction: {} as TAssertFunction,
TAssertError: {} as TAssertError,
TIsValidHttpUrlParams: {} as TIsValidHttpUrlParams,
TDriverParams: {} as DriverParams,
DriverBrowser: {} as DriverBrowser,
ConfigJSON: {} as ConfigJSON,
Data: {} as Data,
DrowserThenableWebDriver: {} as DrowserThenableWebDriver,
DrowserBuilder: {} as DrowserBuilder,
DriverServiceCaseParamsBuilder: {} as DriverServiceCaseParamsBuilder,
DriverServiceCaseParamsAssert: {} as DriverServiceCaseParamsAssert,
DriverServiceCaseParamsBy: {} as DriverServiceCaseParamsBy,
DriverBrowserCaseParams: {} as DriverBrowserCaseParams,
DrowserServiceCase: {} as DrowserServiceCase,
DrowserService: {} as DrowserService,
CaseFn: {} as CaseFn,
DrowserDriverResponse: {} as DrowserDriverResponse,
AssertFunction: {} as AssertFunction,
AssertError: {} as AssertError,
IsValidHttpUrlParams: {} as IsValidHttpUrlParams,
DataPoint: {} as DataPoint,
DataSet: {} as DataSet,
MonthCount: {} as MonthCount,
MonthValue: {} as MonthValue,
ReportSchema: {} as ReportSchema,
}

export default types

0 comments on commit c72fd29

Please sign in to comment.