react / wstein / node_modules / react / node_modules / envify / node_modules / jstransform / visitors / __tests__ / es6-object-concise-method-visitors-test.js
80551 views/**1* Copyright 2013 Facebook, Inc.2*3* Licensed under the Apache License, Version 2.0 (the "License");4* you may not use this file except in compliance with the License.5* You may obtain a copy of the License at6*7* http://www.apache.org/licenses/LICENSE-2.08*9* Unless required by applicable law or agreed to in writing, software10* distributed under the License is distributed on an "AS IS" BASIS,11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12* See the License for the specific language governing permissions and13* limitations under the License.14*/1516/**17* @emails [email protected]18*/1920/*jshint evil:true*/2122require('mock-modules').autoMockOff();2324describe('es6-object-concise-method-visitors', function() {25var transformFn;26var conciseMethodVisitors;27var restParamVisitors;28var visitors;2930beforeEach(function() {31require('mock-modules').dumpCache();32conciseMethodVisitors = require('../es6-object-concise-method-visitors').visitorList;33restParamVisitors = require('../es6-rest-param-visitors').visitorList;34transformFn = require('../../src/jstransform').transform;35visitors = conciseMethodVisitors.concat(restParamVisitors);36});3738function transform(code) {39return transformFn(visitors, code).code;40}4142function expectTransform(code, result) {43expect(transform(code)).toEqual(result);44}4546// Functional tests.4748it('should transform concise method and return 42', function() {49/*global foo*/50var code = transform([51'var foo = {',52' bar(x) {',53' return x;',54' }',55'};'56].join('\n'));5758eval(code);59expect(foo.bar(42)).toEqual(42);60});6162it('should transform concise method with literal property', function() {63var code = transform([64'var foo = {',65' "bar 1"(x) {',66' return x;',67' }',68'};'69].join('\n'));7071eval(code);72expect(foo['bar 1'](42)).toEqual(42);73});747576it('should work with rest params', function() {77var code = transform([78'({',79' init(x, ...rest) {',80' return rest.concat(x);',81' }',82'}).init(1, 2, 3);'83].join('\n'));8485expect(eval(code)).toEqual([2, 3, 1]);86});8788// Source code tests.89it('should transform concise methods', function() {9091// Should transform simple concise method.92expectTransform(93'var foo = {bar() {}};',94'var foo = {bar:function() {}};'95);9697// Should transform inner objects.98expectTransform(99'({bar(x) { return {baz(y) {}}; }});',100'({bar:function(x) { return {baz:function(y) {}}; }});'101);102});103104it('should preserve generators', function() {105// Identifier properties106expectTransform(107'var foo = {*bar(x) {yield x;}};',108'var foo = {bar:function*(x) {yield x;}};'109);110111// Literal properties112expectTransform(113'var foo = {*"abc"(x) {yield x;}, *42(x) {yield x;}};',114'var foo = {"abc":function*(x) {yield x;}, 42:function*(x) {yield x;}};'115);116117// Dynamic properties118expectTransform(119'var foo = {*[a+b](x) {yield x;}}',120'var foo = {[a+b]:function*(x) {yield x;}}'121);122});123124it('should handle reserved words', function() {125expectTransform(126'({delete(x) {}})',127'({"delete":function(x) {}})'128);129});130});131132133134135