updeep-remeda/test/map-spec.js

60 lines
1.3 KiB
JavaScript
Raw Normal View History

2015-08-05 07:25:34 +00:00
import { expect } from 'chai';
import u from '../lib';
describe('u.map', () => {
it('applies updates to each item in an array', () => {
2015-08-05 07:27:56 +00:00
const object = [0, 1, 2];
2015-08-05 07:25:34 +00:00
const inc = x => x + 1;
2015-08-05 07:27:56 +00:00
const result = u.map(inc, object);
2015-08-05 07:25:34 +00:00
expect(result).to.eql([1, 2, 3]);
});
it('applies updates to each value in an object', () => {
2015-08-05 07:27:56 +00:00
const object = { a: 0, b: 1, c: 2 };
2015-08-05 07:25:34 +00:00
const inc = x => x + 1;
2015-08-05 07:27:56 +00:00
const result = u.map(inc, object);
2015-08-05 07:25:34 +00:00
expect(result).to.eql({ a: 1, b: 2, c: 3 });
});
it('can update with a regular updates object', () => {
2015-08-05 07:27:56 +00:00
const object = [{ a: 0 }, { a: 0 }];
const result = u.map({ a: 1 }, object);
2015-08-05 07:25:34 +00:00
expect(result).to.eql([{ a: 1 }, { a: 1 }]);
});
it('passes the key or index as the second parameter to the iteratee', () => {
2015-08-05 07:27:56 +00:00
const object = {
2015-08-05 07:25:34 +00:00
a: { x: 0 },
b: [3, 3],
};
const setToKey = (_, key) => key;
2015-08-05 07:27:56 +00:00
const result = u.map(u.map(setToKey), object);
2015-08-05 07:25:34 +00:00
expect(result).to.eql( {
a: { x: 'x' },
b: [0, 1],
});
});
it('can be partially applied', () => {
2015-08-05 07:27:56 +00:00
const object = {
2015-08-05 07:25:34 +00:00
b: [3, 3],
};
const setToKey = (_, key) => key;
const result = u({
b: u.map(setToKey),
2015-08-05 07:27:56 +00:00
}, object);
2015-08-05 07:25:34 +00:00
expect(result).to.eql( {
b: [0, 1],
});
});
it('freezes the result', () => {
expect(Object.isFrozen(u.map({}, {}))).to.be.true;
});
2015-08-05 07:25:34 +00:00
});