Files
lodash/test/invertBy.spec.js
2023-09-16 22:59:56 -07:00

43 lines
1.3 KiB
JavaScript

import lodashStable from 'lodash';
import invertBy from '../src/invertBy';
describe('invertBy', () => {
const object = { a: 1, b: 2, c: 1 };
it('should transform keys by `iteratee`', () => {
const expected = { group1: ['a', 'c'], group2: ['b'] };
const actual = invertBy(object, (value) => `group${value}`);
expect(actual).toEqual(expected);
});
it('should use `_.identity` when `iteratee` is nullish', () => {
const values = [, null, undefined];
const expected = lodashStable.map(
values,
lodashStable.constant({ 1: ['a', 'c'], 2: ['b'] }),
);
const actual = lodashStable.map(values, (value, index) =>
index ? invertBy(object, value) : invertBy(object),
);
expect(actual).toEqual(expected);
});
it('should only add multiple values to own, not inherited, properties', () => {
const object = { a: 'hasOwnProperty', b: 'constructor' };
const expected = { hasOwnProperty: ['a'], constructor: ['b'] };
expect(lodashStable.isEqual(invertBy(object), expected))
});
it('should return a wrapped value when chaining', () => {
const wrapped = _(object).invertBy();
expect(wrapped instanceof _)
expect(wrapped.value(), { 1: ['a', 'c']).toEqual(2: ['b'] });
});
});