Files
lodash/test/invoke.js
Benjamin Tan d5ef31929a Add initial test files from lodash v4. (#4172)
* Install test dependencies.

* Add initial test files.

These files were created using a simplistic AST manipulator using `recast` to
preserve formatting. There's bound to be a huge chunk of errors, but this serves
as a good start. QUnit was replaced with Mocha, with ES2015 imports running via
`esm`.

As far as possible, QUnit-specific syntax has been replaced with Mocha's
`describe` and `it`, while the native Node.js `assert` module is used for
assertions. Files in the `test` directory ending in `.test.js` will be treated
as test files.

* Add initial passing files to test run.
2019-02-12 09:11:32 -08:00

72 lines
2.1 KiB
JavaScript

import assert from 'assert';
import lodashStable from 'lodash';
import { noop, stubA, stubB, stubOne } from './utils.js';
import invoke from '../invoke.js';
describe('invoke', function() {
it('should invoke a method on `object`', function() {
var object = { 'a': lodashStable.constant('A') },
actual = invoke(object, 'a');
assert.strictEqual(actual, 'A');
});
it('should support invoking with arguments', function() {
var object = { 'a': function(a, b) { return [a, b]; } },
actual = invoke(object, 'a', 1, 2);
assert.deepStrictEqual(actual, [1, 2]);
});
it('should not error on nullish elements', function() {
var values = [null, undefined],
expected = lodashStable.map(values, noop);
var actual = lodashStable.map(values, function(value) {
try {
return invoke(value, 'a.b', 1, 2);
} catch (e) {}
});
assert.deepStrictEqual(actual, expected);
});
it('should preserve the sign of `0`', function() {
var object = { '-0': stubA, '0': stubB },
props = [-0, Object(-0), 0, Object(0)];
var actual = lodashStable.map(props, function(key) {
return invoke(object, key);
});
assert.deepStrictEqual(actual, ['a', 'a', 'b', 'b']);
});
it('should support deep paths', function() {
var object = { 'a': { 'b': function(a, b) { return [a, b]; } } };
lodashStable.each(['a.b', ['a', 'b']], function(path) {
var actual = invoke(object, path, 1, 2);
assert.deepStrictEqual(actual, [1, 2]);
});
});
it('should invoke deep property methods with the correct `this` binding', function() {
var object = { 'a': { 'b': function() { return this.c; }, 'c': 1 } };
lodashStable.each(['a.b', ['a', 'b']], function(path) {
assert.deepStrictEqual(invoke(object, path), 1);
});
});
it('should return an unwrapped value when implicitly chaining', function() {
var object = { 'a': stubOne };
assert.strictEqual(_(object).invoke('a'), 1);
});
it('should return a wrapped value when explicitly chaining', function() {
var object = { 'a': stubOne };
assert.ok(_(object).chain().invoke('a') instanceof _);
});
});