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 | var sortSymbols = function (a, b) {
|
---|
12 | return inspect(a).localeCompare(inspect(b));
|
---|
13 | };
|
---|
14 |
|
---|
15 | test('shimUnscopables', function (t) {
|
---|
16 | t.equal(typeof shimUnscopables, 'function', 'is a function');
|
---|
17 |
|
---|
18 | forEach(v.nonStrings, function (notNonEmptyString) {
|
---|
19 | t['throws'](
|
---|
20 | function () { shimUnscopables(notNonEmptyString); },
|
---|
21 | TypeError,
|
---|
22 | inspect(notNonEmptyString) + ' is not a non-empty String'
|
---|
23 | );
|
---|
24 | });
|
---|
25 |
|
---|
26 | t['throws'](
|
---|
27 | function () { shimUnscopables('x'); },
|
---|
28 | TypeError,
|
---|
29 | inspect('x') + ' is not on Array.prototype'
|
---|
30 | );
|
---|
31 |
|
---|
32 | t.test('no symbols', { skip: typeof Symbol === 'function' }, function (st) {
|
---|
33 | st.doesNotThrow(function () { shimUnscopables('forEach'); });
|
---|
34 |
|
---|
35 | st.end();
|
---|
36 | });
|
---|
37 |
|
---|
38 | t.test('symbols, no unscopables', { skip: typeof Symbol !== 'function' || Symbol.unscopables }, function (st) {
|
---|
39 | st.deepEqual(Object.getOwnPropertySymbols(Array.prototype), [Symbol.iterator]);
|
---|
40 |
|
---|
41 | shimUnscopables('forEach');
|
---|
42 |
|
---|
43 | st.deepEqual(Object.getOwnPropertySymbols(Array.prototype), [Symbol.iterator]);
|
---|
44 |
|
---|
45 | st.end();
|
---|
46 | });
|
---|
47 |
|
---|
48 | t.test('Symbol.unscopables', { skip: typeof Symbol !== 'function' || !Symbol.unscopables }, function (st) {
|
---|
49 | st.deepEqual(
|
---|
50 | Object.getOwnPropertySymbols(Array.prototype).sort(sortSymbols),
|
---|
51 | [Symbol.iterator, Symbol.unscopables]
|
---|
52 | );
|
---|
53 | st.notOk(hasOwn(Array.prototype[Symbol.unscopables], 'forEach'), 'unscopables map lacks forEach');
|
---|
54 |
|
---|
55 | shimUnscopables('forEach');
|
---|
56 |
|
---|
57 | st.deepEqual(
|
---|
58 | Object.getOwnPropertySymbols(Array.prototype).sort(sortSymbols),
|
---|
59 | [Symbol.iterator, Symbol.unscopables]
|
---|
60 | );
|
---|
61 | st.equal(Array.prototype[Symbol.unscopables].forEach, true, 'unscopables map has forEach');
|
---|
62 |
|
---|
63 | st.end();
|
---|
64 | });
|
---|
65 |
|
---|
66 | t.end();
|
---|
67 | });
|
---|