react / wstein / node_modules / react / node_modules / envify / node_modules / jstransform / visitors / __tests__ / es7-rest-property-helpers-test.js
80552 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}2425// Semantic tests.2627it('picks off remaining properties from an object', function() {28var code = transform(29'({ x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 });',30'([ x, y, z ]);'31);32expect(eval(code)).toEqual([1, 2, { a: 3, b: 4 }]);33});3435it('picks off remaining properties from a nested object', function() {36var code = transform(37'var complex = {',38' x: { a: 1, b: 2, c: 3 },',39' y: [4, 5, 6]',40'};',41'var {',42' x: { a: xa, ...xbc },',43' y: [y0, ...y12]',44'} = complex;',45'([ xa, xbc, y0, y12 ]);'46);47expect(eval(code)).toEqual([ 1, { b: 2, c: 3 }, 4, [5, 6] ]);48});4950it('only extracts own properties', function() {51var code = transform(52'var obj = Object.create({ x: 1 });',53'obj.y = 2;',54'({ ...y } = obj);',55'(y);'56);57expect(eval(code)).toEqual({ y: 2 });58});5960it('only extracts own properties, except when they are explicit', function() {61var code = transform(62'var obj = Object.create({ x: 1, y: 2 });',63'obj.z = 3;',64'({ y, ...z } = obj);',65'([ y, z ]);'66);67expect(eval(code)).toEqual([ 2, { z: 3 } ]);68});6970it('avoids passing extra properties when they are picked off', function() {71var code = transform(72'function base({ a, b, x }) { return [ a, b, x ]; }',73'function wrapper({ x, y, ...restConfig }) {',74' return base(restConfig);',75'}',76'wrapper({ x: 1, y: 2, a: 3, b: 4 });'77);78expect(eval(code)).toEqual([ 3, 4, undefined ]);79});8081// Syntax tests.8283it('throws on leading rest properties', function () {84expect(() => transform('({ ...x, y, z } = obj)')).toThrow();85});8687it('throws on multiple rest properties', function () {88expect(() => transform('({ x, ...y, ...z } = obj)')).toThrow();89});9091// TODO: Ideally identifier reuse should fail to transform92// it('throws on identifier reuse', function () {93// expect(() => transform('({ x: { ...z }, y: { ...z } } = obj)')).toThrow();94// });9596});979899