Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80552 views
1
/**
2
* @emails [email protected]
3
*/
4
5
/*jshint evil:true*/
6
7
require('mock-modules').autoMockOff();
8
9
describe('es6-call-spread-visitors', function() {
10
var transformFn;
11
var visitors;
12
13
beforeEach(function() {
14
require('mock-modules').dumpCache();
15
transformFn = require('../../src/jstransform').transform;
16
17
visitors = require('../es6-call-spread-visitors').visitorList;
18
});
19
20
function transform(code, options) {
21
return transformFn(visitors, code, options).code;
22
}
23
24
it('should spread given data with context', function() {
25
expect(transform('Math.max(1,\t[2], 3, ...[4, 5, 6])'))
26
.toEqual('var $__0;($__0 = Math).max.apply($__0, [1,\t[2], 3].concat([4, 5, 6]))');
27
});
28
29
it('should avoid unnecessary concat call', function() {
30
expect(transform('window.Math.max(...list)'))
31
.toEqual('var $__0;($__0 = window.Math).max.apply($__0, list)');
32
});
33
34
it('should default to null context', function() {
35
expect(transform('max(1, 2, ...list)'))
36
.toEqual('max.apply(null, [1, 2].concat(list))');
37
});
38
39
it('should handle computed method names', function() {
40
expect(transform('Math["m" + (0 ? "in" : "ax")](1, 2, ...list)'))
41
.toEqual('var $__0;($__0 = Math)["m" + (0 ? "in" : "ax")].apply($__0, [1, 2].concat(list))');
42
});
43
44
it('should handle immediately invoked function expressions', function() {
45
expect(transform('(function(a, b, c) { return a+b+c; })(1, 2, ...more)'))
46
.toEqual('(function(a, b, c) { return a+b+c; }).apply(null, [1, 2].concat(more))');
47
});
48
49
it('should spread while creating new instances', function() {
50
expect(transform('new Set(1, 2, ...list)'))
51
.toEqual('new (Function.prototype.bind.apply(Set, [null, 1, 2].concat(list)))');
52
});
53
54
it('should create temporary variables when necessary in program scope', function() {
55
expect(transform('foo().bar(arg1, arg2, ...more)'))
56
.toEqual('var $__0;($__0 = foo()).bar.apply($__0, [arg1, arg2].concat(more))');
57
});
58
59
it('should create temporary variables when necessary in function scope', function() {
60
expect(transform('function fn(){ return foo().bar(arg1, arg2, ...more); }'))
61
.toEqual('function fn(){var $__0; return ($__0 = foo()).bar.apply($__0, [arg1, arg2].concat(more)); }');
62
});
63
64
it('should not evaluate context more than once', function() {
65
var code = transform([
66
'var obj = {',
67
' calls: 0,',
68
' get context() {',
69
' this.calls++;',
70
' return {',
71
' add: function(a, b) { return a + b; }',
72
' };',
73
' }',
74
'};',
75
'var nums = [1, 2];',
76
'obj.context.add(...nums);',
77
'obj.calls === 1;',
78
].join('\n'));
79
expect(eval(code)).toEqual(true);
80
});
81
82
it('should transform nested spread expressions', function() {
83
var code = transform([
84
'function getBase() {',
85
' return {',
86
' getParams: function(a, b) {',
87
' return [a, b];',
88
' }',
89
' };',
90
'}',
91
'[].concat(...getBase().getParams(...[1, 2, 3])).join(" ");',
92
].join('\n'));
93
expect(eval(code)).toEqual("1 2");
94
});
95
96
});
97
98