source: imaps-frontend/node_modules/is-symbol/test/index.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 2.8 KB
Line 
1'use strict';
2
3var test = require('tape');
4var forEach = require('for-each');
5var v = require('es-value-fixtures');
6
7var isSymbol = require('../index');
8
9var hasSymbols = require('has-symbols')();
10var hasToStringTag = require('has-tostringtag/shams')();
11var inspect = require('object-inspect');
12
13test('non-symbol values', function (t) {
14 var nonSymbols = v.nonSymbolPrimitives.concat(
15 Object(true),
16 Object(false),
17 // @ts-expect-error TS sucks with concat
18 {},
19 [],
20 /a/g,
21 new Date(),
22 function () {},
23 NaN
24 );
25 t.plan(nonSymbols.length);
26 forEach(nonSymbols, function (nonSymbol) {
27 t.equal(isSymbol(nonSymbol), false, inspect(nonSymbol) + ' is not a symbol');
28 });
29 t.end();
30});
31
32test('faked symbol values', function (t) {
33 t.test('real symbol valueOf', { skip: !hasSymbols }, function (st) {
34 var fakeSymbol = { valueOf: function () { return Symbol('foo'); } };
35 st.equal(isSymbol(fakeSymbol), false, 'object with valueOf returning a symbol is not a symbol');
36 st.end();
37 });
38
39 t.test('faked @@toStringTag', { skip: !hasToStringTag }, function (st) {
40 /** @type {{ valueOf(): unknown; [Symbol.toStringTag]?: unknown }} */
41 var fakeSymbol = { valueOf: function () { return Symbol('foo'); } };
42 fakeSymbol[Symbol.toStringTag] = 'Symbol';
43 st.equal(isSymbol(fakeSymbol), false, 'object with fake Symbol @@toStringTag and valueOf returning a symbol is not a symbol');
44
45 /** @type {{ valueOf(): unknown; [Symbol.toStringTag]?: unknown }} */
46 var notSoFakeSymbol = { valueOf: function () { return 42; } };
47 notSoFakeSymbol[Symbol.toStringTag] = 'Symbol';
48 st.equal(isSymbol(notSoFakeSymbol), false, 'object with fake Symbol @@toStringTag and valueOf not returning a symbol is not a symbol');
49 st.end();
50 });
51
52 var fakeSymbolString = { toString: function () { return 'Symbol(foo)'; } };
53 t.equal(isSymbol(fakeSymbolString), false, 'object with toString returning Symbol(foo) is not a symbol');
54
55 t.end();
56});
57
58test('Symbol support', { skip: !hasSymbols }, function (t) {
59 t.test('well-known Symbols', function (st) {
60 /** @type {(name: string) => name is Exclude<keyof SymbolConstructor, 'for' | 'keyFor'>} */
61 var isWellKnown = function filterer(name) {
62 return name !== 'for' && name !== 'keyFor' && !(name in filterer);
63 };
64 var wellKnownSymbols = Object.getOwnPropertyNames(Symbol).filter(isWellKnown);
65 wellKnownSymbols.forEach(function (name) {
66 // eslint-disable-next-line no-extra-parens
67 var sym = Symbol[/** @type {keyof SymbolConstructor} */ (name)];
68 st.equal(isSymbol(sym), true, inspect(sym) + ' is a symbol');
69 });
70 st.end();
71 });
72
73 t.test('user-created symbols', function (st) {
74 var symbols = v.symbols.concat(
75 Symbol(),
76 Symbol('foo'),
77 Symbol['for']('foo'),
78 Object(Symbol('object'))
79 );
80 symbols.forEach(function (sym) {
81 st.equal(isSymbol(sym), true, inspect(sym) + ' is a symbol');
82 });
83 st.end();
84 });
85
86 t.end();
87});
88
Note: See TracBrowser for help on using the repository browser.