react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / jstransform / visitors / __tests__ / es7-rest-property-helpers-test.js
81153 views/**1* @emails [email protected]2*/34/*jshint evil:true*/56require('mock-modules').autoMockOff();78describe('es7-rest-property-visitors', function() {9var transformFn;1011var visitors;1213beforeEach(function() {14require('mock-modules').dumpCache();15transformFn = require('../../src/jstransform').transform;1617visitors = require('../es6-destructuring-visitors').visitorList;18});1920function transform(code) {21var lines = Array.prototype.join.call(arguments, '\n');22return transformFn(visitors, lines).code;23}2425function expectTransform(code, result) {26expect(transform(code)).toEqual(result);27}2829// Semantic tests.3031it('picks off remaining properties from an object', function() {32var code = transform(33'({ x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 });',34'([ x, y, z ]);'35);36expect(eval(code)).toEqual([1, 2, { a: 3, b: 4 }]);37});3839it('picks off remaining properties from a nested object', function() {40var code = transform(41'var complex = {',42' x: { a: 1, b: 2, c: 3 },',43' y: [4, 5, 6]',44'};',45'var {',46' x: { a: xa, ...xbc },',47' y: [y0, ...y12]',48'} = complex;',49'([ xa, xbc, y0, y12 ]);'50);51expect(eval(code)).toEqual([ 1, { b: 2, c: 3 }, 4, [5, 6] ]);52});5354it('only extracts own properties', function() {55var code = transform(56'var obj = Object.create({ x: 1 });',57'obj.y = 2;',58'({ ...y } = obj);',59'(y);'60);61expect(eval(code)).toEqual({ y: 2 });62});6364it('only extracts own properties, except when they are explicit', function() {65var code = transform(66'var obj = Object.create({ x: 1, y: 2 });',67'obj.z = 3;',68'({ y, ...z } = obj);',69'([ y, z ]);'70);71expect(eval(code)).toEqual([ 2, { z: 3 } ]);72});7374it('avoids passing extra properties when they are picked off', function() {75var code = transform(76'function base({ a, b, x }) { return [ a, b, x ]; }',77'function wrapper({ x, y, ...restConfig }) {',78' return base(restConfig);',79'}',80'wrapper({ x: 1, y: 2, a: 3, b: 4 });'81);82expect(eval(code)).toEqual([ 3, 4, undefined ]);83});8485// Syntax tests.8687it('throws on leading rest properties', function () {88expect(() => transform('({ ...x, y, z } = obj)')).toThrow();89});9091it('throws on multiple rest properties', function () {92expect(() => transform('({ x, ...y, ...z } = obj)')).toThrow();93});9495// TODO: Ideally identifier reuse should fail to transform96// it('throws on identifier reuse', function () {97// expect(() => transform('({ x: { ...z }, y: { ...z } } = obj)')).toThrow();98// });99100});101102103