Skip to main content
Module

x/rambda/source/type.spec.js

Faster and smaller alternative to Ramda
Go to Latest
File
import {type} from './type'import {type as typeRamda} from 'ramda'
test('with symbol', () => { expect(type(Symbol())).toBe('Symbol')})
test('with simple promise', () => { expect(type(Promise.resolve(1))).toBe('Promise')})
test('with new Boolean', () => { expect(type(new Boolean(true))).toBe('Boolean')})
test('with new String', () => { expect(type(new String('I am a String object'))).toEqual('String')})
test('with new Number', () => { expect(type(new Number(1))).toBe('Number')})
test('with error', () => { expect(type(Error(`foo`))).toBe('Error') expect(typeRamda(Error(`foo`))).toBe('Error')})
test('with error - wrong @types/ramda test', () => { // @types/ramda expect the result to be 'Error' but it is not class ExtendedError extends Error {} expect(type(ExtendedError)).toBe('Function') expect(typeRamda(ExtendedError)).toBe('Function')})
test('with new promise', () => { const delay = ms => new Promise(resolve => { setTimeout(() => { resolve(ms + 110) }, ms) })
expect(type(delay(10))).toEqual('Promise')})
test('async function', () => { expect(type(async () => {})).toEqual('Async')})
test('async arrow', () => { const asyncArrow = async () => {} expect(type(asyncArrow)).toBe('Async')})
test('function', () => { const fn1 = () => {} const fn2 = function () {}
function fn3() {}
;[() => {}, fn1, fn2, fn3].map(val => { expect(type(val)).toEqual('Function') })})
test('object', () => { expect(type({})).toEqual('Object')})
test('number', () => { expect(type(1)).toEqual('Number')})
test('boolean', () => { expect(type(false)).toEqual('Boolean')})
test('string', () => { expect(type('foo')).toEqual('String')})
test('null', () => { expect(type(null)).toEqual('Null')})
test('array', () => { expect(type([])).toEqual('Array') expect(type([1, 2, 3])).toEqual('Array')})
test('regex', () => { expect(type(/\s/g)).toEqual('RegExp')})
test('undefined', () => { expect(type(undefined)).toEqual('Undefined')})
test('not a number', () => { expect(type(Number('s'))).toBe('NaN')})
test('set', () => { const exampleSet = new Set([1, 2, 3]) expect(type(exampleSet)).toBe('Set') expect(typeRamda(exampleSet)).toBe('Set')})
test('function inside object 1', () => { const obj = { f() { return 4 }, }
expect(type(obj.f)).toBe('Function') expect(typeRamda(obj.f)).toBe('Function')})
test('function inside object 2', () => { const name = 'f' const obj = { [name]() { return 4 }, } expect(type(obj.f)).toBe('Function') expect(typeRamda(obj.f)).toBe('Function')})