Files
core-js/tests/unit-pure/es.reflect.construct.js

29 lines
1.3 KiB
JavaScript

import construct from 'core-js-pure/es/reflect/construct';
import getPrototypeOf from 'core-js-pure/es/object/get-prototype-of';
QUnit.test('Reflect.construct', assert => {
assert.isFunction(construct);
assert.arity(construct, 2);
if ('name' in construct) {
assert.name(construct, 'construct');
}
function A(a, b, c) {
this.qux = a + b + c;
}
assert.same(construct(A, ['foo', 'bar', 'baz']).qux, 'foobarbaz', 'basic');
A.apply = 42;
assert.same(construct(A, ['foo', 'bar', 'baz']).qux, 'foobarbaz', 'works with redefined apply');
const instance = construct(function () {
this.x = 42;
}, [], Array);
assert.same(instance.x, 42, 'constructor with newTarget');
assert.true(instance instanceof Array, 'prototype with newTarget');
assert.throws(() => construct(42, []), TypeError, 'throws on primitive');
function B() { /* empty */ }
B.prototype = 42;
assert.notThrows(() => getPrototypeOf(construct(B, [])) === Object.prototype);
assert.notThrows(() => typeof construct(Date, []).getTime() == 'number', 'works with native constructors with 2 arguments');
// eslint-disable-next-line prefer-arrow-callback -- testing
assert.throws(() => construct(function () { /* empty */ }), TypeError, 'throws when the second argument is not an object');
});