|
| 1 | +import { DESCRIPTORS, GLOBAL, TYPED_ARRAYS } from '../helpers/constants'; |
| 2 | + |
| 3 | +const { getPrototypeOf } = Object; |
| 4 | + |
| 5 | +if (DESCRIPTORS) QUnit.test('%TypedArrayPrototype%.groupBy', assert => { |
| 6 | + // we can't implement %TypedArrayPrototype% in all engines, so run all tests for each typed array constructor |
| 7 | + for (const name in TYPED_ARRAYS) { |
| 8 | + const TypedArray = GLOBAL[name]; |
| 9 | + const { groupBy } = TypedArray.prototype; |
| 10 | + assert.isFunction(groupBy, `${ name }::groupBy is function`); |
| 11 | + assert.arity(groupBy, 1, `${ name }::groupBy arity is 1`); |
| 12 | + assert.name(groupBy, 'groupBy', `${ name }::groupBy name is 'groupBy'`); |
| 13 | + assert.looksNative(groupBy, `${ name }::groupBy looks native`); |
| 14 | + const array = new TypedArray([1]); |
| 15 | + const context = {}; |
| 16 | + array.groupBy(function (value, key, that) { |
| 17 | + assert.same(arguments.length, 3, 'correct number of callback arguments'); |
| 18 | + assert.same(value, 1, 'correct value in callback'); |
| 19 | + assert.same(key, 0, 'correct index in callback'); |
| 20 | + assert.same(that, array, 'correct link to array in callback'); |
| 21 | + assert.same(this, context, 'correct callback context'); |
| 22 | + }, context); |
| 23 | + |
| 24 | + assert.same(getPrototypeOf(new TypedArray([1]).groupBy(it => it)), null, 'null proto'); |
| 25 | + assert.ok(new TypedArray([1]).groupBy(it => it)[1] instanceof TypedArray, 'instance'); |
| 26 | + assert.deepEqual( |
| 27 | + new TypedArray([1, 2, 3]).groupBy(it => it % 2), |
| 28 | + { 1: new TypedArray([1, 3]), 0: new TypedArray([2]) }, |
| 29 | + '#1', |
| 30 | + ); |
| 31 | + assert.deepEqual(new TypedArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]).groupBy(it => `i${ it % 5 }`), { |
| 32 | + i1: new TypedArray([1, 6, 11]), |
| 33 | + i2: new TypedArray([2, 7, 12]), |
| 34 | + i3: new TypedArray([3, 8]), |
| 35 | + i4: new TypedArray([4, 9]), |
| 36 | + i0: new TypedArray([5, 10]), |
| 37 | + }, '#2'); |
| 38 | + |
| 39 | + assert.throws(() => groupBy.call([0], () => { /* empty */ }), "isn't generic"); |
| 40 | + } |
| 41 | +}); |
| 42 | + |
0 commit comments