react / wstein / node_modules / react / node_modules / envify / node_modules / jstransform / visitors / __tests__ / es7-spread-property-visitors-test.js
80551 views/**1* @emails [email protected]2*/34/*jshint evil:true*/56require('mock-modules').autoMockOff();78describe('es7-spread-property-visitors', function() {9var transformFn;10var originalAssign = Object.assign;11var visitors;1213// These are placeholder variables in scope that we can use to assert that a14// specific variable reference was passed, rather than an object clone of it.15var x = 123456;16var y = 789012;17var z = 345678;1819beforeEach(function() {20require('mock-modules').dumpCache();21transformFn = require('../../src/jstransform').transform;2223visitors = require('../es7-spread-property-visitors').visitorList;24});2526function transform(code) {27return transformFn(visitors, code).code;28}2930function expectTransform(code, result) {31expect(transform(code)).toEqual(result);32}3334function expectObjectAssign(code) {35var objectAssignMock = jest.genMockFunction();36Object.assign = objectAssignMock;37eval(transform(code));38return expect(objectAssignMock);39}4041afterEach(function() {42Object.assign = originalAssign;43});4445// Polyfill sanity checks4647it('has access to a working Object.assign implementation', function() {48expect(typeof Object.assign).toBe('function');49expect(Object.assign({ b: 2 }, null, { a: 1 })).toEqual({ a: 1, b: 2 });50});5152// Semantic tests.5354it('uses Object.assign with an empty new target object', function() {55expectObjectAssign(56'var xy = { ...x, y: 2 }'57).toBeCalledWith({}, x, { y: 2 });58});5960it('coalesces consecutive properties into a single object', function() {61expectObjectAssign(62'var xyz = { ...x, y: 2, z: z }'63).toBeCalledWith({}, x, { y: 2, z: z });6465});6667it('avoids an unnecessary empty object when spread is not first', function() {68expectObjectAssign(69'var xy = { x: 1, ...y }'70).toBeCalledWith({ x: 1}, y);71});7273it('passes the same value multiple times to Object.assign', function() {74expectObjectAssign(75'var xyz = { x: 1, y: 2, ...z, ...z }'76).toBeCalledWith({ x: 1, y: 2}, z, z);77});7879it('keeps object literals as separate arguments to assign', function() {80expectObjectAssign(81'var xyz = { x: 1, ...({ y: 2 }), z: 3 }'82).toBeCalledWith({ x: 1}, { y: 2 }, {z: 3 });83});8485it('does not call assign when there are no spread properties', function() {86expectObjectAssign(87'var xy = { x: 1, y: 2 }'88).not.toBeCalled();89});9091// Syntax tests.9293it('should preserve extra whitespace', function() {94expectTransform(95'let xyz = { x: 1, y : \n 2, ... \nz, ... z }',96'let xyz = Object.assign({ x: 1, y : \n 2}, \nz, z )'97);98});99100it('should preserve parenthesis', function() {101expectTransform(102'let xyz = { x: 1, ...({ y: 2 }), z: 3 }',103'let xyz = Object.assign({ x: 1}, ({ y: 2 }), {z: 3 })'104);105});106107it('should remove trailing commas after properties', function() {108expectTransform(109'let xyz = { ...x, y: 1, }',110'let xyz = Object.assign({}, x, {y: 1 })'111);112});113114it('should remove trailing commas after spread', function() {115expectTransform(116'let xyz = { x: 1, ...y, }',117'let xyz = Object.assign({ x: 1}, y )'118);119});120121// Don't transform122123it('should not transform destructuring assignment', function() {124expectTransform(125'let { x, ...y } = z',126'let { x, ...y } = z'127);128});129130// Accessors are unsupported. We leave it unprocessed so that other,131// chained transforms, can take over.132133it('should not transform when there are getters', function() {134expectTransform(135'let xy = { ...x, get x() { } }',136'let xy = { ...x, get x() { } }'137);138});139140it('should not transform when there are setters', function() {141expectTransform(142'let xy = { set x(v) { }, ...y }',143'let xy = { set x(v) { }, ...y }'144);145});146147it('should silently ignore falsy values', function() {148/*globals obj*/149var code = transform([150'var x = null;',151'var y = { y: "y" };',152'var obj = { ...x, ...y, ...{ ...false, z: "z", ...y } };'153].join('\n'));154155eval(code);156157expect(obj).toEqual({ y: 'y', z: 'z' });158});159});160161162