| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var callBind = require('../');
|
|---|
| 4 | var hasStrictMode = require('has-strict-mode')();
|
|---|
| 5 | var forEach = require('for-each');
|
|---|
| 6 | var inspect = require('object-inspect');
|
|---|
| 7 | var v = require('es-value-fixtures');
|
|---|
| 8 |
|
|---|
| 9 | var test = require('tape');
|
|---|
| 10 |
|
|---|
| 11 | test('callBindBasic', function (t) {
|
|---|
| 12 | forEach(v.nonFunctions, function (nonFunction) {
|
|---|
| 13 | t['throws'](
|
|---|
| 14 | // @ts-expect-error
|
|---|
| 15 | function () { callBind([nonFunction]); },
|
|---|
| 16 | TypeError,
|
|---|
| 17 | inspect(nonFunction) + ' is not a function'
|
|---|
| 18 | );
|
|---|
| 19 | });
|
|---|
| 20 |
|
|---|
| 21 | var sentinel = { sentinel: true };
|
|---|
| 22 | /** @type {<T, A extends number, B extends number>(this: T, a: A, b: B) => [T | undefined, A, B]} */
|
|---|
| 23 | var func = function (a, b) {
|
|---|
| 24 | // eslint-disable-next-line no-invalid-this
|
|---|
| 25 | return [!hasStrictMode && this === global ? undefined : this, a, b];
|
|---|
| 26 | };
|
|---|
| 27 | t.equal(func.length, 2, 'original function length is 2');
|
|---|
| 28 |
|
|---|
| 29 | /** type {(thisArg: unknown, a: number, b: number) => [unknown, number, number]} */
|
|---|
| 30 | var bound = callBind([func]);
|
|---|
| 31 | /** type {((a: number, b: number) => [typeof sentinel, typeof a, typeof b])} */
|
|---|
| 32 | var boundR = callBind([func, sentinel]);
|
|---|
| 33 | /** type {((b: number) => [typeof sentinel, number, typeof b])} */
|
|---|
| 34 | var boundArg = callBind([func, sentinel, /** @type {const} */ (1)]);
|
|---|
| 35 |
|
|---|
| 36 | // @ts-expect-error
|
|---|
| 37 | t.deepEqual(bound(), [undefined, undefined, undefined], 'bound func with no args');
|
|---|
| 38 |
|
|---|
| 39 | // @ts-expect-error
|
|---|
| 40 | t.deepEqual(func(), [undefined, undefined, undefined], 'unbound func with too few args');
|
|---|
| 41 | // @ts-expect-error
|
|---|
| 42 | t.deepEqual(bound(1, 2), [hasStrictMode ? 1 : Object(1), 2, undefined], 'bound func too few args');
|
|---|
| 43 | // @ts-expect-error
|
|---|
| 44 | t.deepEqual(boundR(), [sentinel, undefined, undefined], 'bound func with receiver, with too few args');
|
|---|
| 45 | // @ts-expect-error
|
|---|
| 46 | t.deepEqual(boundArg(), [sentinel, 1, undefined], 'bound func with receiver and arg, with too few args');
|
|---|
| 47 |
|
|---|
| 48 | t.deepEqual(func(1, 2), [undefined, 1, 2], 'unbound func with right args');
|
|---|
| 49 | t.deepEqual(bound(1, 2, 3), [hasStrictMode ? 1 : Object(1), 2, 3], 'bound func with right args');
|
|---|
| 50 | t.deepEqual(boundR(1, 2), [sentinel, 1, 2], 'bound func with receiver, with right args');
|
|---|
| 51 | t.deepEqual(boundArg(2), [sentinel, 1, 2], 'bound func with receiver and arg, with right arg');
|
|---|
| 52 |
|
|---|
| 53 | // @ts-expect-error
|
|---|
| 54 | t.deepEqual(func(1, 2, 3), [undefined, 1, 2], 'unbound func with too many args');
|
|---|
| 55 | // @ts-expect-error
|
|---|
| 56 | t.deepEqual(bound(1, 2, 3, 4), [hasStrictMode ? 1 : Object(1), 2, 3], 'bound func with too many args');
|
|---|
| 57 | // @ts-expect-error
|
|---|
| 58 | t.deepEqual(boundR(1, 2, 3), [sentinel, 1, 2], 'bound func with receiver, with too many args');
|
|---|
| 59 | // @ts-expect-error
|
|---|
| 60 | t.deepEqual(boundArg(2, 3), [sentinel, 1, 2], 'bound func with receiver and arg, with too many args');
|
|---|
| 61 |
|
|---|
| 62 | t.end();
|
|---|
| 63 | });
|
|---|