Skip to main content
Module

x/rambda/forEach.spec.js

Faster and smaller alternative to Ramda
Go to Latest
File
import { forEach } from './forEach'import { type } from './type'
test('happy', () => { const sideEffect = {} forEach(x => sideEffect[ `foo${ x }` ] = x + 10)([ 1, 2 ])
expect(sideEffect).toEqual({ foo1 : 11, foo2 : 12, })})
test('iterate over object', () => { const obj = { a : 1, b : [ 1, 2 ], c : { d : 7 }, f : 'foo', } const result = {} const returned = forEach(( val, prop, inputObj ) => { expect(type(inputObj)).toBe('Object') result[ prop ] = `${ prop }-${ type(val) }` })(obj)
const expected = { a : 'a-Number', b : 'b-Array', c : 'c-Object', f : 'f-String', }
expect(result).toEqual(expected) expect(returned).toEqual(obj)})
test('with empty list', () => { const list = [] const result = forEach(x => x * x)(list)
expect(result).toEqual(list)})
test('with wrong input', () => { const list = undefined const result = forEach(x => x * x)(list)
expect(result).toBeUndefined()})
test('returns the input', () => { const list = [ 1, 2, 3 ] const result = forEach(x => x * x)(list)
expect(result).toEqual(list)})