react / wstein / node_modules / react / node_modules / envify / node_modules / jstransform / visitors / __tests__ / es6-call-spread-visitors-test.js
80552 views/**1* @emails [email protected]2*/34/*jshint evil:true*/56require('mock-modules').autoMockOff();78describe('es6-call-spread-visitors', function() {9var transformFn;10var visitors;1112beforeEach(function() {13require('mock-modules').dumpCache();14transformFn = require('../../src/jstransform').transform;1516visitors = require('../es6-call-spread-visitors').visitorList;17});1819function transform(code, options) {20return transformFn(visitors, code, options).code;21}2223it('should spread given data with context', function() {24expect(transform('Math.max(1,\t[2], 3, ...[4, 5, 6])'))25.toEqual('var $__0;($__0 = Math).max.apply($__0, [1,\t[2], 3].concat([4, 5, 6]))');26});2728it('should avoid unnecessary concat call', function() {29expect(transform('window.Math.max(...list)'))30.toEqual('var $__0;($__0 = window.Math).max.apply($__0, list)');31});3233it('should default to null context', function() {34expect(transform('max(1, 2, ...list)'))35.toEqual('max.apply(null, [1, 2].concat(list))');36});3738it('should handle computed method names', function() {39expect(transform('Math["m" + (0 ? "in" : "ax")](1, 2, ...list)'))40.toEqual('var $__0;($__0 = Math)["m" + (0 ? "in" : "ax")].apply($__0, [1, 2].concat(list))');41});4243it('should handle immediately invoked function expressions', function() {44expect(transform('(function(a, b, c) { return a+b+c; })(1, 2, ...more)'))45.toEqual('(function(a, b, c) { return a+b+c; }).apply(null, [1, 2].concat(more))');46});4748it('should spread while creating new instances', function() {49expect(transform('new Set(1, 2, ...list)'))50.toEqual('new (Function.prototype.bind.apply(Set, [null, 1, 2].concat(list)))');51});5253it('should create temporary variables when necessary in program scope', function() {54expect(transform('foo().bar(arg1, arg2, ...more)'))55.toEqual('var $__0;($__0 = foo()).bar.apply($__0, [arg1, arg2].concat(more))');56});5758it('should create temporary variables when necessary in function scope', function() {59expect(transform('function fn(){ return foo().bar(arg1, arg2, ...more); }'))60.toEqual('function fn(){var $__0; return ($__0 = foo()).bar.apply($__0, [arg1, arg2].concat(more)); }');61});6263it('should not evaluate context more than once', function() {64var code = transform([65'var obj = {',66' calls: 0,',67' get context() {',68' this.calls++;',69' return {',70' add: function(a, b) { return a + b; }',71' };',72' }',73'};',74'var nums = [1, 2];',75'obj.context.add(...nums);',76'obj.calls === 1;',77].join('\n'));78expect(eval(code)).toEqual(true);79});8081it('should transform nested spread expressions', function() {82var code = transform([83'function getBase() {',84' return {',85' getParams: function(a, b) {',86' return [a, b];',87' }',88' };',89'}',90'[].concat(...getBase().getParams(...[1, 2, 3])).join(" ");',91].join('\n'));92expect(eval(code)).toEqual("1 2");93});9495});969798