diff --git a/spec/util/SymbolShim-spec.js b/spec/util/SymbolShim-spec.js index 05dafca76b..ac251d4e5f 100644 --- a/spec/util/SymbolShim-spec.js +++ b/spec/util/SymbolShim-spec.js @@ -25,6 +25,18 @@ describe('SymbolShim', function () { expect(test).toBe('@@test'); }); + it('should add a for method even if Symbol already exists but does not have for', function () { + var root = { + Symbol: {} + }; + var result = polyfillSymbol(root); + + expect(typeof result.for).toBe('function'); + + var test = result.for('test'); + expect(test).toBe('@@test'); + }); + describe('when symbols exists on root', function () { it('should use symbols from root', function () { var root = { @@ -59,17 +71,15 @@ describe('SymbolShim', function () { expect(result.observable).toBe('@@observable'); }); }); -}); -describe('ensureIterator', function () { - it('should patch root using for symbol if exist', function () { + it('should patch root using Symbol.for if exist', function () { var root = { Symbol: { for: function (x) { return x; } } }; - ensureIterator(root.Symbol, root); - expect(root.Symbol.iterator).toBe(root.Symbol.for('iterator')); + var result = polyfillSymbol(root); + expect(result.iterator).toBe(root.Symbol.for('iterator')); }); it('should patch using Set for mozilla bug', function () { @@ -82,8 +92,8 @@ describe('ensureIterator', function () { Symbol: {} }; - ensureIterator(root.Symbol, root); - expect(root.Symbol.iterator).toBe('@@iterator'); + var result = polyfillSymbol(root); + expect(result.iterator).toBe('@@iterator'); }); it('should patch using map for es6-shim', function () { @@ -95,7 +105,7 @@ describe('ensureIterator', function () { root.Map.prototype.key = 'iteratorValue'; root.Map.prototype.entries = 'iteratorValue'; - ensureIterator(root.Symbol, root); - expect(root.Symbol.iterator).toBe('key'); + var result = polyfillSymbol(root); + expect(result.iterator).toBe('key'); }); }); diff --git a/src/util/SymbolShim.ts b/src/util/SymbolShim.ts index 8d152d9939..98e4163403 100644 --- a/src/util/SymbolShim.ts +++ b/src/util/SymbolShim.ts @@ -4,14 +4,19 @@ export function polyfillSymbol(root) { const Symbol = ensureSymbol(root); ensureIterator(Symbol, root); ensureObservable(Symbol); + ensureFor(Symbol); return Symbol; } +export function ensureFor(Symbol) { + if (!Symbol.for) { + Symbol.for = symbolForPolyfill; + } +} + export function ensureSymbol(root) { if (!root.Symbol) { - root.Symbol = { - for: symbolForPolyfill - }; + root.Symbol = {}; } return root.Symbol; }