Skip to content

Commit

Permalink
chore: make as Object fluent
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbi08 committed Jan 22, 2024
1 parent 4ffc76b commit a60c76f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 104 deletions.
2 changes: 1 addition & 1 deletion examples/cap-bookshop-wdi5
4 changes: 1 addition & 3 deletions examples/ui5-ts-app/test/e2e/Input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ describe("Input", async () => {
const control = await browser.asControl(inputSelector)
const bindingInfo = await control.getBindingInfo("value")
// @ts-expect-error "parts" is not part of the type definition
const parts = await bindingInfo.parts
const partzero = await parts[0]
const path = await partzero.path
const path = await browser.asObject(bindingInfo.getUUID()).parts[0].path
expect(path).toEqual("/Customers('TRAIH')/ContactName")
})
})
153 changes: 53 additions & 100 deletions src/lib/wdi5-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,119 +425,72 @@ export async function _addWdi5Commands(browserInstance: WebdriverIO.Browser) {
// the a Proxy and a recursive `handler` function
if (!browserInstance.asControl) {
browserInstance.asControl = function (ui5ControlSelector) {
const asyncMethods = ["then", "catch", "finally"]
const functionQueue = []
// we need to do the same operation as in the 'init' of 'wdi5-control.ts'
const logging = ui5ControlSelector?.logging ?? true
function makeFluent(target) {
const promise = Promise.resolve(target)
const handler = {
get(_, prop) {
functionQueue.push(prop)
return asyncMethods.includes(prop)
? (...boundArgs) => makeFluent(promise[prop](...boundArgs))
: makeFluent(
promise.then((object) => {
// when object is undefined the previous function call failed
try {
return object[prop]
} catch (error) {
// different node versions return a different `error.message` so we use our own message
if (logging) {
Logger.error(`Cannot read property '${prop}' in the execution queue!`)
}
}
})
)
},
apply(_, thisArg, boundArgs) {
return makeFluent(
// When "targetFunction" is empty we can assume that there are errors in the execution queue
promise.then((targetFunction) => {
if (targetFunction) {
return Reflect.apply(targetFunction, thisArg, boundArgs)
} else {
// a functionQueue without a 'then' can be ignored
// as the original error was already logged
if (functionQueue.includes("then") && logging) {
functionQueue.splice(functionQueue.indexOf("then"))
Logger.error(
`One of the calls in the queue "${functionQueue.join(
"()."
)}()" previously failed!`
)
}
}
})
)
}
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
return new Proxy(function () {}, handler)
}
const makeFluent = createMakeFluent(ui5ControlSelector?.logging)
// @ts-ignore

Check warning on line 429 in src/lib/wdi5-bridge.ts

View workflow job for this annotation

GitHub Actions / Run linters

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
return makeFluent(browserInstance._asControl(ui5ControlSelector))
}
}

if (!browserInstance.asObject) {
browserInstance.asObject = function (uuid) {
const asyncMethods = ["then", "catch", "finally"]
const functionQueue = []
// we need to do the same operation as in the 'init' of 'wdi5-control.ts'
const logging = true
function makeFluent(target) {
const promise = Promise.resolve(target)
const handler = {
get(_, prop) {
functionQueue.push(prop)
return asyncMethods.includes(prop)
? (...boundArgs) => makeFluent(promise[prop](...boundArgs))
: makeFluent(
promise.then((object) => {
// when object is undefined the previous function call failed
try {
return object[prop]
} catch (error) {
// different node versions return a different `error.message` so we use our own message
if (logging) {
Logger.error(`Cannot read property '${prop}' in the execution queue!`)
}
}
})
)
},
apply(_, thisArg, boundArgs) {
return makeFluent(
// When "targetFunction" is empty we can assume that there are errors in the execution queue
promise.then((targetFunction) => {
if (targetFunction) {
return Reflect.apply(targetFunction, thisArg, boundArgs)
} else {
// a functionQueue without a 'then' can be ignored
// as the original error was already logged
if (functionQueue.includes("then") && logging) {
functionQueue.splice(functionQueue.indexOf("then"))
Logger.error(
`One of the calls in the queue "${functionQueue.join(
"()."
)}()" previously failed!`
)
}
}
})
)
}
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
return new Proxy(function () {}, handler)
}
const makeFluent = createMakeFluent()
// @ts-ignore

Check warning on line 437 in src/lib/wdi5-bridge.ts

View workflow job for this annotation

GitHub Actions / Run linters

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
return makeFluent(browserInstance._asObject(uuid))
}
}
}

export function createMakeFluent(loggin = true) {
const asyncMethods = ["then", "catch", "finally"]
const functionQueue = []
// we need to do the same operation as in the 'init' of 'wdi5-control.ts'
const logging = loggin
return function makeFluent(target) {
const promise = Promise.resolve(target)
const handler = {
get(_, prop) {
functionQueue.push(prop)
return asyncMethods.includes(prop)
? (...boundArgs) => makeFluent(promise[prop](...boundArgs))
: makeFluent(
promise.then((object) => {
// when object is undefined the previous function call failed
try {
return object[prop]
} catch (error) {
// different node versions return a different `error.message` so we use our own message
if (logging) {
Logger.error(`Cannot read property '${prop}' in the execution queue!`)
}
}
})
)
},
apply(_, thisArg, boundArgs) {
return makeFluent(
// When "targetFunction" is empty we can assume that there are errors in the execution queue
promise.then((targetFunction) => {
if (targetFunction) {
return Reflect.apply(targetFunction, thisArg, boundArgs)
} else {
// a functionQueue without a 'then' can be ignored
// as the original error was already logged
if (functionQueue.includes("then") && logging) {
functionQueue.splice(functionQueue.indexOf("then"))
Logger.error(
`One of the calls in the queue "${functionQueue.join("().")}()" previously failed!`
)
}
}
})
)
}
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
return new Proxy(function () {}, handler)
}
}

/**
* retrieve a DOM element via UI5 locator
* @param {sap.ui.test.RecordReplay.ControlSelector} controlSelector
Expand Down

0 comments on commit a60c76f

Please sign in to comment.