How to create a question that would Round number and convert to string. #2259
-
I am attempting to create a question that would intake a note with a number in it (i.e. came from API response). This number would be validated against the UI text value. The number needs to be rounded and convert to string for the integer. Thanks for your help... const RoundNumberToString = (dataPoint: Answerable<number>) =>
Question.about(`the number is rounded and converted to text`, actor => {
return Math.round(dataPoint).toFixed(0);
});
await actor.attemptsTo(
notes().set('num', 23.65),
Ensure.that(RoundNum(notes<MyNotes>().get('num')), equals('24')),
) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
There are three ways to do it:
Inline transformation
Assuming that your implementation of interface MyNotes {
num: number;
} this notes<MyNotes>().get('num').toFixed(0) // returns QuestionAdapter<string> resolving to '24' Another useful API is notes<MyNotes>().get('num').as(dataPoint => Math.round(dataPoint)).toFixed(0) Since notes<MyNotes>().get('num').as(Math.round).toFixed(0) The complete example would then looks like this: await actor.attemptsTo(
notes().set('num', 23.65),
Ensure.that(
notes<MyNotes>().get('num').as(Math.round).toFixed(0),
equals('24')
),
) Using a custom mapping functionIf the transformation gets more involved, or you want to be able to reuse the same transformation in different parts of your codebase, you could introduce a custom mapping function you'd pass to Such mapping function should accept the original value and return the transformed value: function rounded(dataPoint: number): string {
return Math.round(dataPoint).toFixed(0)
} If the transformation is asynchronous, the mapping function can also return a async function rounded(dataPoint: number): Promise<string> {
// some async operations that you need to `await` for
return Math.round(dataPoint).toFixed(0)
} The mapping function could also be produced by a higher-order function allowing for the transformation to be parameterised: function amount(symbol: string) {
return function(dataPoint: number): string {
const rounded = Math.round(dataPoint).toFixed(0)
return `${ symbol }${ rounded }`
}
} In this case, the complete example looks like this: await actor.attemptsTo(
notes().set('num', 23.65),
Ensure.that(
notes<MyNotes>().get('num').as(rounded),
equals('24')
),
Ensure.that(
notes<MyNotes>().get('num').as(amount('£')),
equals('£24')
),
) Using a custom questionIf you need to access the const prependName = (dataPoint: Answerable<number>) =>
Question.about('name', async actor => {
const param = await actor.answer(dataPoint);
const value = Math.round(param).toFixed(0);
return `${ actor.name }: ${ value }`
}); In this case, the complete example using the custom question would look like this: await actor.attemptsTo(
notes().set('num', 23.65),
Ensure.that(
prependName(notes<MyNotes>().get('num')),
equals('Alice: 24')
),
) |
Beta Was this translation helpful? Give feedback.
There are three ways to do it:
actor
instance to perform the transformation, and inline operations are insufficientInline transformation
Question.about
, used by all the built-in Serenity/JS APIs, produces aQuestionAdapter
, which proxies the methods and fields of the wrapped object recursively, allowing them to be used as either a Question (or an Interaction).Assuming that your implementation of
MyNotes
…