wix-storybook-utils
Version:
Utilities for automated component documentation within Storybook
61 lines • 2.62 kB
JavaScript
/* global describe it expect */
import functionToString from './function-to-string';
describe('functionToString', function () {
describe('given function as argument', function () {
it('should convert to arrow function and return it', function () {
/* tslint:disable */
function prop(arg) {
return 'value' + arg;
}
/* tslint:enable */
expect(functionToString(prop)).toEqual("arg => 'value' + arg");
});
it('should handle multiple arguments', function () {
/* tslint:disable */
function prop(arg1, arg2, arg3) {
var anything = 'hello';
return arg1 + arg2 + arg3 + anything;
}
/* tslint:enable */
expect(functionToString(prop)).toEqual("(arg1, arg2, arg3) => {\n var anything = 'hello';\n return arg1 + arg2 + arg3 + anything;\n}");
});
it('should not do anything to arrow function', function () {
/* tslint:disable */
var prop = function (arg) { return arg + 1; };
/* tslint:enable */
expect(functionToString(prop)).toEqual("arg => arg + 1");
});
it('should convert properties to shorthand if possible', function () {
/* tslint:disable */
function prop(value) {
// intentional ignore
// @ts-ignore
return setState({
value: value,
});
}
/* tslint:enable */
expect(functionToString(prop)).toEqual("value => setState({\n value\n})");
});
it('should work with FunctionExpression', function () {
var obj = {
functionExpression: function (argument1, argument2) {
var rest = [];
for (var _i = 2; _i < arguments.length; _i++) {
rest[_i - 2] = arguments[_i];
}
return 'this is a method';
},
};
expect(functionToString(obj.functionExpression)).toEqual(obj.functionExpression.toString());
});
});
describe('given non function argument', function () {
[0, 10, false, String('hello'), [], null, NaN, Infinity].map(function (assert) {
return it("which is ".concat(assert, " should return ").concat(assert), function () {
return expect(functionToString(assert)).toEqual(assert);
});
});
});
});
//# sourceMappingURL=function-to-string.test.js.map