react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / jstransform / visitors / __tests__ / es6-arrow-function-visitors-test.js
81153 views/**1* Copyright 2013 Facebook, Inc.2*3* Licensed under the Apache License, Version 2.0 (the "License");4* you may not use this file except in compliance with the License.5* You may obtain a copy of the License at6*7* http://www.apache.org/licenses/LICENSE-2.08*9* Unless required by applicable law or agreed to in writing, software10* distributed under the License is distributed on an "AS IS" BASIS,11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12* See the License for the specific language governing permissions and13* limitations under the License.14*/1516/**17* @emails [email protected]18*/1920/*jshint evil:true*/2122require('mock-modules').autoMockOff();2324describe('es6ArrowFunctionsTransform', function() {25var transformFn;26var visitors;2728beforeEach(function() {29require('mock-modules').dumpCache();30visitors = require('../es6-arrow-function-visitors').visitorList;31transformFn = require('../../src/jstransform').transform;32});3334function transform(code) {35return transformFn(visitors, code).code;36}3738function expectTransform(code, result) {39expect(transform(code)).toEqual(result);40}4142it('should capture correct this value at different levels', function() {43var code = transform([44'var foo = {',45' createFooGetter: function() {',46' return (x) => [x, this];', // captures foo47' },',48' getParentThis: () => this', // captures parent this49'};'50].join('\n'));5152eval(code);5354expect(typeof foo.createFooGetter).toBe('function');55expect(typeof foo.createFooGetter()).toBe('function');56expect(typeof foo.getParentThis).toBe('function');5758expect(foo.getParentThis()).toEqual(this);59expect(foo.createFooGetter()(10)).toEqual([10, foo]);60});6162it('should map an array using arrow capturing this value', function() {63this.factor = 10;6465var code = transform(66'[1, 2, 3].map(x => x * x * this.factor);'67);6869expect(eval(code)).toEqual([10, 40, 90]);70});7172it('binds if any `super` keyword is referenced', function() {73var code = transform(74'var fn=x=>super;'75);7677// We have to do a source text comparison here because `super` is a reserved78// keyword (so we can't eval it).79expect(code).toEqual('var fn=function(x){return super;}.bind(this);');80});8182it('should filter an array using arrow with two params', function() {83this.factor = 0;8485var code = transform([86'[1, 2, 3].filter((v, idx) => {',87' if (idx > 1 && this.factor > 0) {',88' return true;',89' }',90' this.factor++;',91' return false;',92'});'93].join('\n'));9495expect(eval(code)).toEqual([3]);96});9798it('should fetch this value data from nested arrow', function() {99var code = transform([100'({',101' bird: 22,',102' run: function() {',103' return () => () => this.bird;',104' }',105'}).run()()();'106].join('\n'));107108expect(eval(code)).toEqual(22);109});110111// Syntax tests.112113it('should correctly transform arrows', function() {114// 0 params, expression.115expectTransform(116'() => this.value;',117'(function() {return this.value;}.bind(this));'118);119120// 0 params, expression wrapped in parens121expectTransform(122'() => (this.value);',123'(function() {return this.value;}.bind(this));'124);125126// 1 param, no-parens, expression, no this.127expectTransform(128'x => x * x;',129'(function(x) {return x * x;});'130);131132// 1 param, parens, expression, as argument, no this.133expectTransform(134'map((x) => x * x);',135'map(function(x) {return x * x;});'136);137138// 2 params, block, as argument, nested.139expectTransform(140'makeRequest((response, error) => {'.concat(141' return this.update(data => this.onData(data), response);',142'});'),143'makeRequest(function(response, error) {'.concat(144' return this.update(function(data) {return this.onData(data);}.bind(this), response);',145'}.bind(this));')146);147148// Assignment to a var, simple, 1 param.149expectTransform(150'var action = (value) => this.performAction(value);',151'var action = function(value) {return this.performAction(value);}.bind(this);'152);153154// Preserve lines transforming ugly code.155expectTransform([156'(',157'',158'',159' x,',160' y',161'',162')',163'',164' =>',165'',166' {',167' return x + y;',168'};'169].join('\n'), [170'(function(',171'',172'',173' x,',174' y)',175'',176'',177'',178' ',179'',180' {',181' return x + y;',182'});'183].join('\n'));184185// Preserve line numbers with single parens-free param ugly code.186expectTransform([187'x',188'',189' =>',190' x;'191].join('\n'), [192'(function(x)',193'',194' ',195' {return x;});'196].join('\n'));197198// Preserve line numbers with single parens param ugly code.199expectTransform([200'(',201'',202' x',203'',204')',205'',206' =>',207' x;'208].join('\n'), [209'(function(',210'',211' x)',212'',213'',214'',215' ',216' {return x;});'217].join('\n'));218219// Preserve line numbers with parens around expression.220expectTransform([221'(x) => (',222' x',223');'224].join('\n'), [225'(function(x) ',226' {return x;}',227');'228].join('\n'));229230// Preserve typechecker annotation.231expectTransform(232'(/*string*/foo, /*bool*/bar) => foo;',233'(function(/*string*/foo, /*bool*/bar) {return foo;});'234);235});236});237238239240