react / wstein / node_modules / react / node_modules / envify / node_modules / jstransform / visitors / __tests__ / es6-destructuring-visitors-test.js
80552 views/**1* @emails [email protected]2*/34/*jshint evil:true*/5/*jshint -W117*/67require('mock-modules').autoMockOff();89describe('es6-destructuring-visitors', function() {10var transformFn;1112var destructuringVisitors;13var conciseMethodVisitors;14var shortObjectsVisitors;15var reservedWordsVisitors;16var restParamVisitors;17var classVisitorsVisitors;18var arrowFunctionVisitors;1920var visitors;2122beforeEach(function() {23require('mock-modules').dumpCache();24transformFn = require('../../src/jstransform').transform;2526destructuringVisitors = require('../es6-destructuring-visitors').visitorList;27conciseMethodVisitors = require('../es6-object-concise-method-visitors').visitorList;28shortObjectsVisitors = require('../es6-object-short-notation-visitors').visitorList;29reservedWordsVisitors = require('../reserved-words-visitors').visitorList;30restParamVisitors = require('../es6-rest-param-visitors').visitorList;31classVisitorsVisitors = require('../es6-class-visitors').visitorList;32arrowFunctionVisitors = require('../es6-arrow-function-visitors').visitorList;3334visitors = destructuringVisitors.concat(35conciseMethodVisitors,36shortObjectsVisitors,37restParamVisitors,38classVisitorsVisitors,39arrowFunctionVisitors,40reservedWordsVisitors41);42});4344function transform(code) {45return transformFn(visitors, code).code;46}4748function expectTransform(code, result) {49expect(transform(code)).toEqual(result);50}5152it('should handle simple object pattern', function() {53var code = transform([54'var {x, y} = {x: 10, y: 20};',55'(x + y);'56].join('\n'));5758expect(eval(code)).toEqual(30);59});6061it('should handle literal property names', function() {62var code = transform([63'var {x, "y y": yy, 0: z} = {x: 10, "y y": 20, 0: 30};',64'([x, yy, z]);'65].join('\n'));6667expect(eval(code)).toEqual([10, 20, 30]);68});6970it('should handle array pattern assignment expression', function() {71var code = transform([72'var x = 10, y = 20;',73'[x, y] = [y, x];',74'([x, y]);'75].join('\n'));7677expect(eval(code)).toEqual([20, 10]);78});7980it('should should not redeclare vars with assignment expression', function() {81var code = transform([82'var x = 10, y = 20;',83'(function() {',84' [x, y] = [y, x];',85'})();',86'([x, y]);'87].join('\n'));8889expect(eval(code)).toEqual([20, 10]);90});9192it('should handle object pattern assignment expression', function() {93var code = transform([94'var x = 10, y = 20;',95'({x, y} = {y, x});',96'({x, y});'97].join('\n'));9899expect(eval(code)).toEqual({x: 10, y: 20});100});101102it('should destructure result of a function', function() {103var code = transform([104'var [x, y] = (function({x, y}) { return [x, y]; })({x: 1, y: 2});',105'([x, y]);'106].join('\n'));107108expect(eval(code)).toEqual([1, 2]);109});110111it('should handle skipped array elements', function() {112var code = transform([113'var [x, , y] = [1, 2, 3];',114'([x, y]);'115].join('\n'));116117expect(eval(code)).toEqual([1, 3]);118});119120it('should handle rest elements of an array', function() {121var code = transform([122'var [x, ...xs] = [1, 2, 3];'123].join('\n'));124125eval(code);126127expect(x).toEqual(1);128expect(xs).toEqual([2, 3]);129});130131it('should swap two variables w/o third using pattern', function() {132var code = transform([133'var x = 10, y = 20;',134'var [x, y] = [y, x];',135'([x, y]);'136].join('\n'));137138expect(eval(code)).toEqual([20, 10]);139});140141it('should transform complex pattern argument', function() {142var code = transform([143'function init(user, {ip, coords: [x, y], port}) {',144' return [user, ip, x, y, port].join(", ");',145'}'146].join('\n'));147148eval(code);149150expect(init(151'John Doe', {152ip: '127.0.0.1',153coords: [1, 2],154port: 8080155}156)).toBe('John Doe, 127.0.0.1, 1, 2, 8080');157});158159it('should work with rest params', function() {160var code = transform([161'function foo({bar, baz}, ...rest) {',162' return {bar, baz, qux: rest[0]};',163'}'164].join('\n'));165166eval(code);167168expect(foo({bar: 10, baz: 20}, 30))169.toEqual({bar: 10, baz: 20, qux: 30});170});171172it('should work with class methods', function() {173var code = transform([174'class Point {',175' constructor({x, y}) {',176' this._x = x;',177' this._y = y;',178' }',179'',180' getData([deltaX, deltaY]) {',181' return this._x + deltaX + this._y + deltaY',182' }',183'}'184].join('\n'));185186eval(code);187188var x = 10, y = 20;189var foo = new Point({x: x, y: y});190var data = foo.getData([30, 40]);191192expect(data).toBe(100);193});194195it('should work with object concise methods', function() {196var code = transform([197'var foo = {',198' bar({x, y}) {',199' return {x, y};',200' }',201'}'202].join('\n'));203204eval(code);205206expect(foo.bar({x: 10, y: 20}))207.toEqual({x: 10, y: 20});208});209210it('should work with arrows', function() {211var code = transform([212'var foo = ({x, y}, z) => x + y + z;'213].join('\n'));214215eval(code);216217expect(foo({x: 10, y: 20}, 30))218.toEqual(60);219});220221// Auto-generated temp vars test.222it('should allocate correct temp index', function() {223var code = transform([224'function foo(x, {y}, {z}) {',225' var {q} = {q: 30};',226' return [$__0, $__1, $__2];',227'}'228].join('\n'));229230eval(code);231232expect(foo(1, {y: 10}, {z: 20}))233.toEqual([{y: 10}, {z: 20}, {q: 30}]);234});235236it('should allocate correct temp nested index', function() {237var code = transform([238'var foo = function(x, {y}, {z}) {',239' var {q, m: {v}} = {q: 30, m: {v: 40}};',240' var {a} = (function({a}) { return $__0; })({a: 50});',241' return [$__0, $__1, $__2, $__3, a];',242'}'243].join('\n'));244245eval(code);246247expect(foo(1, {y: 10}, {z: 20}))248.toEqual([{y: 10}, {z: 20}, {q: 30, m: {v: 40}}, {v: 40}, 50]);249});250251252253// Syntax tests.254255it('should correctly transform structured patterns', function() {256257// Variable declaration.258expectTransform(259'var a, {x, data: [y, , z]} = {x: 10, data: [1, 2, 3]};',260'var a, $__0= {x: 10, data: [1, 2, 3]},x=$__0.x,$__1=$__0.data,y=$__1[0],z=$__1[2];'261);262263// Function parameters.264expectTransform(265'function f(a, {x, data: [y, z]}, b) {}',266'function f(a, $__0 , b) {var x=$__0.x,$__1=$__0.data,y=$__1[0],z=$__1[1];}'267);268});269270it('should handle reserved words', function() {271expectTransform(272'var {delete: x} = {delete: 1};',273'var $__0= {"delete": 1},x=$__0["delete"];'274);275});276});277278279280281