Skip to content
Permalink
Browse files
feat(core): toString method of Screenplay classes prints the name of …
…the function
  • Loading branch information
jan-molak committed Feb 24, 2019
1 parent 5d37aa3 commit f3d738e0d8eb53d2fa3a0ecba236988345375c39
Showing with 21 additions and 3 deletions.
  1. +6 −3 packages/core/spec/io/formatted.spec.ts
  2. +15 −0 packages/core/src/io/formatted.ts
@@ -14,19 +14,22 @@ describe ('`formatted` tag function', () => {
i = value => ({ inspect: () => value }),
ts = value => ({ toString: () => value });

class SomeClass {}

given(
{ description: 'no parameters', actual: formatted `Hello World!`, expected: 'Hello World!' },
{ description: 'an undefined parameter', actual: formatted `param: ${ undefined }`, expected: 'param: undefined' },
{ description: 'a number parameter', actual: formatted `Answer: ${ 42 }`, expected: 'Answer: 42' },
{ description: 'a string parameters', actual: formatted `Hello ${ 'World' }!`, expected: "Hello 'World'!" },
{ description: 'an object parameters', actual: formatted `${ { twitter: '@JanMolak'} }`, expected: "{ twitter: '@JanMolak' }" },
{ description: 'an array parameters', actual: formatted `${ [1, 2, '3'] }`, expected: "[ 1, 2, '3' ]" },
{ description: 'a string parameter', actual: formatted `Hello ${ 'World' }!`, expected: "Hello 'World'!" },
{ description: 'an object parameter', actual: formatted `${ { twitter: '@JanMolak'} }`, expected: "{ twitter: '@JanMolak' }" },
{ description: 'an array parameter', actual: formatted `${ [1, 2, '3'] }`, expected: "[ 1, 2, '3' ]" },
{ description: 'an object array parameter', actual: formatted `${ [{ name: 'Jan'}] }`, expected: "[ { name: 'Jan' } ]" },
{ description: 'a Date parameter', actual: formatted `${ new Date('Jan 27, 2019') }`, expected: '2019-01-27T00:00:00.000Z' },
{ description: 'a promised parameter', actual: formatted `${ p('something') }`, expected: 'a promised value' },
{ description: 'a question', actual: formatted `${ q('value') }`, expected: 'the meaning of life' },
{ description: 'an inspectable object', actual: formatted `${ i('result') }`, expected: 'result' },
{ description: 'an "toStringable" object', actual: formatted `${ ts('result') }`, expected: 'result' },
{ description: 'a function parameter', actual: formatted `${ SomeClass }`, expected: 'SomeClass' },
).
it('produces a human-readable description when given a template with', ({ actual, expected }) => {
expect(actual).to.equal(expected);
@@ -47,6 +47,10 @@ function descriptionOf(value: KnowableUnknown<any>): string {
return value.inspect();
}

if (isANamedFunction(value)) {
return value.name;
}

if (hasItsOwnToString(value)) {
return value.toString();
}
@@ -123,6 +127,17 @@ function isAPromise<T>(v: KnowableUnknown<T>): v is Promise<T> {
return !! (v as any).then;
}

/**
* @desc
* Checks if the value is a named {@link Function}
*
* @private
* @param {KnowableUnknown<any>} v
*/
function isANamedFunction<T>(v: any): v is { name: string } {
return {}.toString.call(v) === '[object Function]' && (v as any).name !== '';
}

/**
* https://davidwalsh.name/detect-native-function
* @param {any} v

0 comments on commit f3d738e

Please sign in to comment.