Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/modules / ___dist / exotic / __tests__ / test.coersion.test.js
Size: Mime:
"use strict";

// const {curry, overStaticMethods} = require('chain')
const _require = require(".."),
      toShape = _require.toShape; // // const { curry, overStaticMethods } = require('../../../../../modules/deps/index')
//
// const toRamdom = x => Math.random(0, 1000)
//
// const coerceOverArgs = curry(2, coerceArgs)
//
// test('coerce Class methods', () => {
//   const toCoercer = coercer => coerceArgs(coercer)
//   const coerceMethods = (Klass, coercer) =>
//     overStaticMethods(Klass, toCoercer(coercer))
//
//   class Classy {
//     static eh(arg1, arg2) {
//       console.log('eh', {arg1, arg2})
//     }
//   }
//
//   coerceMethods(Classy, toRamdom)
// })
//
// test('coerceOverArgs', () => {
//   function myFn(obj, ar, str, num) {
//     return {obj, ar, str, num}
//   }
//
//   const overMyArgs = coerceOverArgs(Object, Array, String('string'), Number(1))
//   const mySafeFn = overMyArgs(myFn)
//
//   // const results = mySafeFn('null', 'null', 'null', 'null')
//   const results = mySafeFn(null, {obj: []}, 1, '10')
//   console.log(results)
// })
//
// test('myFnWithTypeHintsForBabel', () => {
//   function myFnWithTypeHintsForBabel(
//     obj = Object,
//     ar = Array,
//     str = String('string'),
//     num = Number(1)
//   ) {
//     return {obj, ar, str, num}
//   }
// })


test('inline function shape - coerces arguments', () => {
  function myFn(obj, ar, str, num) {
    return {
      obj,
      ar,
      str,
      num
    };
  }

  const overMyArgs = toShape(Object, Array, String('str'), Number(1));
  const mySafeFn = overMyArgs(myFn);
  const results = mySafeFn(null, {
    obj: []
  }, 1, '10');
  expect(results).toEqual({
    obj: {},
    ar: [{
      obj: []
    }],
    str: '1',
    num: 10
  });
});
test('inline function shape - coerces arguments that would BREAK', () => {
  function myFn(obj, ar, str, num) {
    return {
      obj,
      ar,
      str,
      num
    };
  }

  const overMyArgs = toShape(Object, Array, String('str'), Number(1));
  const mySafeFn = overMyArgs(myFn);
  const usuallyBreaking = mySafeFn(undefined, null, null, null);
  expect(usuallyBreaking).toEqual({
    obj: {},
    ar: [],
    str: 'str',
    num: 1
  });
});