Home Manual Reference Source Test Repository

spec-js/symbol/iterator-spec.js

"use strict";
var chai_1 = require('chai');
var iterator_1 = require('../../dist/cjs/symbol/iterator');
describe('iterator symbol', function () {
    it('should exist', function () {
        chai_1.expect(iterator_1.$$iterator).to.exist;
    });
});
describe('symbolIteratorPonyfill', function () {
    describe('when root.Symbol is a function', function () {
        describe('and Symbol.iterator exists', function () {
            it('should return Symbol.iterator', function () {
                var FakeSymbol = function () { };
                FakeSymbol.iterator = {};
                var result = iterator_1.symbolIteratorPonyfill({ Symbol: FakeSymbol });
                chai_1.expect(result).to.equal(FakeSymbol.iterator);
            });
        });
        describe('and Symbol.iterator does not exist', function () {
            it('should use Symbol to create an return a symbol and polyfill Symbol.iterator', function () {
                var SYMBOL_RETURN = {};
                var passedDescription;
                var root = {
                    Symbol: function (description) {
                        passedDescription = description;
                        return SYMBOL_RETURN;
                    }
                };
                var result = iterator_1.symbolIteratorPonyfill(root);
                chai_1.expect(result).to.equal(SYMBOL_RETURN);
                chai_1.expect(root.Symbol.iterator).to.equal(SYMBOL_RETURN);
            });
        });
    });
    describe('when root.Symbol is NOT a function', function () {
        describe('and root.Set exists with an @@iterator property that is a function (Mozilla bug)', function () {
            it('should return "$$iterator"', function () {
                var result = iterator_1.symbolIteratorPonyfill({
                    Set: function FakeSet() {
                        this['@@iterator'] = function () { };
                    }
                });
                chai_1.expect(result).to.equal('@@iterator');
            });
        });
        describe('root.Set does not exit or does not have an @@iterator property', function () {
            describe('but Map exists and a random key on Map.prototype that matches Map.prototype.entries (for es6-shim)', function () {
                it('should return the key that matches the "entries" key on Map.prototype, but not return "size"', function () {
                    function FakeMap() { }
                    function fakeMethod() { }
                    FakeMap.prototype['-omg-lol-i-can-use-whatever-I-want-YOLO-'] = fakeMethod;
                    FakeMap.prototype.entries = fakeMethod;
                    FakeMap.prototype.size = fakeMethod;
                    var root = {
                        Map: FakeMap
                    };
                    var result = iterator_1.symbolIteratorPonyfill(root);
                    chai_1.expect(result).to.equal('-omg-lol-i-can-use-whatever-I-want-YOLO-');
                });
            });
            describe('but Map exists and no other key except "size" on Map.prototype that matches Map.prototype.entries (for es6-shim)', function () {
                it('should return "@@iterator"', function () {
                    function FakeMap() { }
                    function fakeMethod() { }
                    FakeMap.prototype.entries = fakeMethod;
                    FakeMap.prototype.size = fakeMethod;
                    var root = {
                        Map: FakeMap
                    };
                    var result = iterator_1.symbolIteratorPonyfill(root);
                    chai_1.expect(result).to.equal('@@iterator');
                });
            });
            describe('if Set exists but has no iterator, and Map does not exist (bad polyfill maybe?)', function () {
                it('should return "@@iterator"', function () {
                    var result = iterator_1.symbolIteratorPonyfill({
                        Set: function () { }
                    });
                    chai_1.expect(result).to.equal('@@iterator');
                });
            });
            describe('and root.Set and root.Map do NOT exist', function () {
                it('should return "@@iterator"', function () {
                    var result = iterator_1.symbolIteratorPonyfill({});
                    chai_1.expect(result).to.equal('@@iterator');
                });
            });
        });
    });
});
//# sourceMappingURL=iterator-spec.js.map