react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / jstransform / visitors / __tests__ / es6-object-concise-method-visitors-test.js
81153 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() {49var code = transform([50'var foo = {',51' bar(x) {',52' return x;',53' }',54'};'55].join('\n'));5657eval(code);58expect(foo.bar(42)).toEqual(42);59});6061it('should transform concise method with literal property', function() {62var code = transform([63'var foo = {',64' "bar 1"(x) {',65' return x;',66' }',67'};'68].join('\n'));6970eval(code);71expect(foo['bar 1'](42)).toEqual(42);72});737475it('should work with rest params', function() {76var code = transform([77'({',78' init(x, ...rest) {',79' return rest.concat(x);',80' }',81'}).init(1, 2, 3);'82].join('\n'));8384expect(eval(code)).toEqual([2, 3, 1]);85});8687// Source code tests.88it('should transform concise methods', function() {8990// Should transform simple concise method.91expectTransform(92'var foo = {bar() {}};',93'var foo = {bar:function() {}};'94);9596// Should transform inner objects.97expectTransform(98'({bar(x) { return {baz(y) {}}; }});',99'({bar:function(x) { return {baz:function(y) {}}; }});'100);101});102103it('should preserve generators', function() {104// Identifier properties105expectTransform(106'var foo = {*bar(x) {yield x;}};',107'var foo = {bar:function*(x) {yield x;}};'108);109110// Literal properties111expectTransform(112'var foo = {*"abc"(x) {yield x;}, *42(x) {yield x;}};',113'var foo = {"abc":function*(x) {yield x;}, 42:function*(x) {yield x;}};'114);115116// Dynamic properties117expectTransform(118'var foo = {*[a+b](x) {yield x;}}',119'var foo = {[a+b]:function*(x) {yield x;}}'120);121});122123it('should handle reserved words', function() {124expectTransform(125'({delete(x) {}})',126'({"delete":function(x) {}})'127);128});129});130131132133134