| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var test = require('tape');
|
|---|
| 4 | var inspect = require('object-inspect');
|
|---|
| 5 | var v = require('es-value-fixtures');
|
|---|
| 6 | var forEach = require('for-each');
|
|---|
| 7 | var hasOwn = require('hasown');
|
|---|
| 8 |
|
|---|
| 9 | var shimUnscopables = require('../');
|
|---|
| 10 |
|
|---|
| 11 | /** @type {(a: symbol, b: symbol) => number} */
|
|---|
| 12 | var sortSymbols = function (a, b) {
|
|---|
| 13 | return inspect(a).localeCompare(inspect(b));
|
|---|
| 14 | };
|
|---|
| 15 |
|
|---|
| 16 | test('shimUnscopables', function (t) {
|
|---|
| 17 | t.equal(typeof shimUnscopables, 'function', 'is a function');
|
|---|
| 18 |
|
|---|
| 19 | forEach(v.nonStrings, function (notNonEmptyString) {
|
|---|
| 20 | t['throws'](
|
|---|
| 21 | // @ts-expect-error
|
|---|
| 22 | function () { shimUnscopables(notNonEmptyString); },
|
|---|
| 23 | TypeError,
|
|---|
| 24 | inspect(notNonEmptyString) + ' is not a non-empty String'
|
|---|
| 25 | );
|
|---|
| 26 | });
|
|---|
| 27 |
|
|---|
| 28 | t['throws'](
|
|---|
| 29 | // @ts-expect-error
|
|---|
| 30 | function () { shimUnscopables('x'); },
|
|---|
| 31 | TypeError,
|
|---|
| 32 | inspect('x') + ' is not on Array.prototype'
|
|---|
| 33 | );
|
|---|
| 34 |
|
|---|
| 35 | t.test('no symbols', { skip: typeof Symbol === 'function' }, function (st) {
|
|---|
| 36 | st.doesNotThrow(function () { shimUnscopables('forEach'); });
|
|---|
| 37 |
|
|---|
| 38 | st.end();
|
|---|
| 39 | });
|
|---|
| 40 |
|
|---|
| 41 | t.test('symbols, no unscopables', { skip: typeof Symbol !== 'function' || !!Symbol.unscopables }, function (st) {
|
|---|
| 42 | st.deepEqual(Object.getOwnPropertySymbols(Array.prototype), [Symbol.iterator]);
|
|---|
| 43 |
|
|---|
| 44 | shimUnscopables('forEach');
|
|---|
| 45 |
|
|---|
| 46 | st.deepEqual(Object.getOwnPropertySymbols(Array.prototype), [Symbol.iterator]);
|
|---|
| 47 |
|
|---|
| 48 | st.end();
|
|---|
| 49 | });
|
|---|
| 50 |
|
|---|
| 51 | t.test('Symbol.unscopables', { skip: typeof Symbol !== 'function' || !Symbol.unscopables }, function (st) {
|
|---|
| 52 | st.deepEqual(
|
|---|
| 53 | Object.getOwnPropertySymbols(Array.prototype).sort(sortSymbols),
|
|---|
| 54 | [Symbol.iterator, Symbol.unscopables]
|
|---|
| 55 | );
|
|---|
| 56 | st.notOk(hasOwn(Array.prototype[Symbol.unscopables], 'forEach'), 'unscopables map lacks forEach');
|
|---|
| 57 |
|
|---|
| 58 | shimUnscopables('forEach');
|
|---|
| 59 |
|
|---|
| 60 | st.deepEqual(
|
|---|
| 61 | Object.getOwnPropertySymbols(Array.prototype).sort(sortSymbols),
|
|---|
| 62 | [Symbol.iterator, Symbol.unscopables]
|
|---|
| 63 | );
|
|---|
| 64 | st.equal(Array.prototype[Symbol.unscopables].forEach, true, 'unscopables map has forEach');
|
|---|
| 65 |
|
|---|
| 66 | st.end();
|
|---|
| 67 | });
|
|---|
| 68 |
|
|---|
| 69 | t.end();
|
|---|
| 70 | });
|
|---|