react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / jstransform / visitors / __tests__ / es6-destructuring-visitors-test.js
81153 views/**1* @emails [email protected]2*/34/*jshint evil:true*/56require('mock-modules').autoMockOff();78describe('es6-destructuring-visitors', function() {9var transformFn;1011var destructuringVisitors;12var conciseMethodVisitors;13var shortObjectsVisitors;14var reservedWordsVisitors;15var restParamVisitors;16var classVisitorsVisitors;17var arrowFunctionVisitors;1819var visitors;2021beforeEach(function() {22require('mock-modules').dumpCache();23transformFn = require('../../src/jstransform').transform;2425destructuringVisitors = require('../es6-destructuring-visitors').visitorList;26conciseMethodVisitors = require('../es6-object-concise-method-visitors').visitorList;27shortObjectsVisitors = require('../es6-object-short-notation-visitors').visitorList;28reservedWordsVisitors = require('../reserved-words-visitors').visitorList;29restParamVisitors = require('../es6-rest-param-visitors').visitorList;30classVisitorsVisitors = require('../es6-class-visitors').visitorList;31arrowFunctionVisitors = require('../es6-arrow-function-visitors').visitorList;3233visitors = destructuringVisitors.concat(34conciseMethodVisitors,35shortObjectsVisitors,36restParamVisitors,37classVisitorsVisitors,38arrowFunctionVisitors,39reservedWordsVisitors40);41});4243function transform(code) {44return transformFn(visitors, code).code;45}4647function expectTransform(code, result) {48expect(transform(code)).toEqual(result);49}5051it('should handle simple object pattern', function() {52var code = transform([53'var {x, y} = {x: 10, y: 20};',54'(x + y);'55].join('\n'));5657expect(eval(code)).toEqual(30);58});5960it('should handle literal property names', function() {61var code = transform([62'var {x, "y y": yy, 0: z} = {x: 10, "y y": 20, 0: 30};',63'([x, yy, z]);'64].join('\n'));6566expect(eval(code)).toEqual([10, 20, 30]);67});6869it('should handle array pattern assignment expression', function() {70var code = transform([71'var x = 10, y = 20;',72'[x, y] = [y, x];',73'([x, y]);'74].join('\n'));7576expect(eval(code)).toEqual([20, 10]);77});7879it('should should not redeclare vars with assignment expression', function() {80var code = transform([81'var x = 10, y = 20;',82'(function() {',83' [x, y] = [y, x];',84'})();',85'([x, y]);'86].join('\n'));8788expect(eval(code)).toEqual([20, 10]);89});9091it('should handle object pattern assignment expression', function() {92var code = transform([93'var x = 10, y = 20;',94'({x, y} = {y, x});',95'({x, y});'96].join('\n'));9798expect(eval(code)).toEqual({x: 10, y: 20});99});100101it('should destructure result of a function', function() {102var code = transform([103'var [x, y] = (function({x, y}) { return [x, y]; })({x: 1, y: 2});',104'([x, y]);'105].join('\n'));106107expect(eval(code)).toEqual([1, 2]);108});109110it('should handle skipped array elements', function() {111var code = transform([112'var [x, , y] = [1, 2, 3];',113'([x, y]);'114].join('\n'));115116expect(eval(code)).toEqual([1, 3]);117});118119it('should handle rest elements of an array', function() {120var code = transform([121'var [x, ...xs] = [1, 2, 3];'122].join('\n'));123124eval(code);125126expect(x).toEqual(1);127expect(xs).toEqual([2, 3]);128});129130it('should swap two variables w/o third using pattern', function() {131var code = transform([132'var x = 10, y = 20;',133'var [x, y] = [y, x];',134'([x, y]);'135].join('\n'));136137expect(eval(code)).toEqual([20, 10]);138});139140it('should transform complex pattern argument', function() {141var code = transform([142'function init(user, {ip, coords: [x, y], port}) {',143' return [user, ip, x, y, port].join(", ");',144'}'145].join('\n'));146147eval(code);148149expect(init(150'John Doe', {151ip: '127.0.0.1',152coords: [1, 2],153port: 8080154}155)).toBe('John Doe, 127.0.0.1, 1, 2, 8080');156});157158it('should work with rest params', function() {159var code = transform([160'function foo({bar, baz}, ...rest) {',161' return {bar, baz, qux: rest[0]};',162'}'163].join('\n'));164165eval(code);166167expect(foo({bar: 10, baz: 20}, 30))168.toEqual({bar: 10, baz: 20, qux: 30});169});170171it('should work with class methods', function() {172var code = transform([173'class Point {',174' constructor({x, y}) {',175' this._x = x;',176' this._y = y;',177' }',178'',179' getData([deltaX, deltaY]) {',180' return this._x + deltaX + this._y + deltaY',181' }',182'}'183].join('\n'));184185eval(code);186187var x = 10, y = 20;188var foo = new Point({x: x, y: y});189var data = foo.getData([30, 40]);190191expect(data).toBe(100);192});193194it('should work with object concise methods', function() {195var code = transform([196'var foo = {',197' bar({x, y}) {',198' return {x, y};',199' }',200'}'201].join('\n'));202203eval(code);204205expect(foo.bar({x: 10, y: 20}))206.toEqual({x: 10, y: 20});207});208209it('should work with arrows', function() {210var code = transform([211'var foo = ({x, y}, z) => x + y + z;'212].join('\n'));213214eval(code);215216expect(foo({x: 10, y: 20}, 30))217.toEqual(60);218});219220// Auto-generated temp vars test.221it('should allocate correct temp index', function() {222var code = transform([223'function foo(x, {y}, {z}) {',224' var {q} = {q: 30};',225' return [$__0, $__1, $__2];',226'}'227].join('\n'));228229eval(code);230231expect(foo(1, {y: 10}, {z: 20}))232.toEqual([{y: 10}, {z: 20}, {q: 30}]);233});234235it('should allocate correct temp nested index', function() {236var code = transform([237'var foo = function(x, {y}, {z}) {',238' var {q, m: {v}} = {q: 30, m: {v: 40}};',239' var {a} = (function({a}) { return $__0; })({a: 50});',240' return [$__0, $__1, $__2, $__3, a];',241'}'242].join('\n'));243244eval(code);245246expect(foo(1, {y: 10}, {z: 20}))247.toEqual([{y: 10}, {z: 20}, {q: 30, m: {v: 40}}, {v: 40}, 50]);248});249250251252// Syntax tests.253254it('should correctly transform structured patterns', function() {255256// Variable declaration.257expectTransform(258'var a, {x, data: [y, , z]} = {x: 10, data: [1, 2, 3]};',259'var a, $__0= {x: 10, data: [1, 2, 3]},x=$__0.x,$__1=$__0.data,y=$__1[0],z=$__1[2];'260);261262// Function parameters.263expectTransform(264'function f(a, {x, data: [y, z]}, b) {}',265'function f(a, $__0 , b) {var x=$__0.x,$__1=$__0.data,y=$__1[0],z=$__1[1];}'266);267});268269it('should handle reserved words', function() {270expectTransform(271'var {delete: x} = {delete: 1};',272'var $__0= {"delete": 1},x=$__0["delete"];'273);274});275});276277278279280