react / wstein / node_modules / react / node_modules / envify / node_modules / jstransform / visitors / __tests__ / es6-arrow-function-visitors-test.js
80551 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*/21/*jshint -W117*/2223require('mock-modules').autoMockOff();2425describe('es6ArrowFunctionsTransform', function() {26var transformFn;27var visitors;2829beforeEach(function() {30require('mock-modules').dumpCache();31visitors = require('../es6-arrow-function-visitors').visitorList;32transformFn = require('../../src/jstransform').transform;33});3435function transform(code) {36return transformFn(visitors, code).code;37}3839function expectTransform(code, result) {40expect(transform(code)).toEqual(result);41}4243it('should capture correct this value at different levels', function() {44var code = transform([45'var foo = {',46' createFooGetter: function() {',47' return (x) => [x, this];', // captures foo48' },',49' getParentThis: () => this', // captures parent this50'};'51].join('\n'));5253eval(code);5455expect(typeof foo.createFooGetter).toBe('function');56expect(typeof foo.createFooGetter()).toBe('function');57expect(typeof foo.getParentThis).toBe('function');5859expect(foo.getParentThis()).toEqual(this);60expect(foo.createFooGetter()(10)).toEqual([10, foo]);61});6263it('should map an array using arrow capturing this value', function() {64this.factor = 10;6566var code = transform(67'[1, 2, 3].map(x => x * x * this.factor);'68);6970expect(eval(code)).toEqual([10, 40, 90]);71});7273it('binds if any `super` keyword is referenced', function() {74var code = transform(75'var fn=x=>super;'76);7778// We have to do a source text comparison here because `super` is a reserved79// keyword (so we can't eval it).80expect(code).toEqual('var fn=function(x){return super;}.bind(this);');81});8283it('should filter an array using arrow with two params', function() {84this.factor = 0;8586var code = transform([87'[1, 2, 3].filter((v, idx) => {',88' if (idx > 1 && this.factor > 0) {',89' return true;',90' }',91' this.factor++;',92' return false;',93'});'94].join('\n'));9596expect(eval(code)).toEqual([3]);97});9899it('should fetch this value data from nested arrow', function() {100var code = transform([101'({',102' bird: 22,',103' run: function() {',104' return () => () => this.bird;',105' }',106'}).run()()();'107].join('\n'));108109expect(eval(code)).toEqual(22);110});111112// Syntax tests.113114it('should correctly transform arrows', function() {115// 0 params, expression.116expectTransform(117'() => this.value;',118'(function() {return this.value;}.bind(this));'119);120121// 0 params, expression wrapped in parens122expectTransform(123'() => (this.value);',124'(function() {return this.value;}.bind(this));'125);126127// 1 param, no-parens, expression, no this.128expectTransform(129'x => x * x;',130'(function(x) {return x * x;});'131);132133// 1 param, parens, expression, as argument, no this.134expectTransform(135'map((x) => x * x);',136'map(function(x) {return x * x;});'137);138139// 2 params, block, as argument, nested.140expectTransform(141'makeRequest((response, error) => {'.concat(142' return this.update(data => this.onData(data), response);',143'});'),144'makeRequest(function(response, error) {'.concat(145' return this.update(function(data) {return this.onData(data);}.bind(this), response);',146'}.bind(this));')147);148149// Assignment to a var, simple, 1 param.150expectTransform(151'var action = (value) => this.performAction(value);',152'var action = function(value) {return this.performAction(value);}.bind(this);'153);154155// Preserve lines transforming ugly code.156expectTransform([157'(',158'',159'',160' x,',161' y',162'',163')',164'',165' =>',166'',167' {',168' return x + y;',169'};'170].join('\n'), [171'(function(',172'',173'',174' x,',175' y)',176'',177'',178'',179' ',180'',181' {',182' return x + y;',183'});'184].join('\n'));185186// Preserve line numbers with single parens-free param ugly code.187expectTransform([188'x',189'',190' =>',191' x;'192].join('\n'), [193'(function(x)',194'',195' ',196' {return x;});'197].join('\n'));198199// Preserve line numbers with single parens param ugly code.200expectTransform([201'(',202'',203' x',204'',205')',206'',207' =>',208' x;'209].join('\n'), [210'(function(',211'',212' x)',213'',214'',215'',216' ',217' {return x;});'218].join('\n'));219220// Preserve line numbers with parens around expression.221expectTransform([222'(x) => (',223' x',224');'225].join('\n'), [226'(function(x) ',227' {return x;}',228');'229].join('\n'));230231// Preserve typechecker annotation.232expectTransform(233'(/*string*/foo, /*bool*/bar) => foo;',234'(function(/*string*/foo, /*bool*/bar) {return foo;});'235);236});237});238239240241