Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80551 views
1
/**
2
* @emails [email protected]
3
*/
4
5
/*jshint evil:true*/
6
7
require('mock-modules').autoMockOff();
8
9
describe('es7-spread-property-visitors', function() {
10
var transformFn;
11
var originalAssign = Object.assign;
12
var visitors;
13
14
// These are placeholder variables in scope that we can use to assert that a
15
// specific variable reference was passed, rather than an object clone of it.
16
var x = 123456;
17
var y = 789012;
18
var z = 345678;
19
20
beforeEach(function() {
21
require('mock-modules').dumpCache();
22
transformFn = require('../../src/jstransform').transform;
23
24
visitors = require('../es7-spread-property-visitors').visitorList;
25
});
26
27
function transform(code) {
28
return transformFn(visitors, code).code;
29
}
30
31
function expectTransform(code, result) {
32
expect(transform(code)).toEqual(result);
33
}
34
35
function expectObjectAssign(code) {
36
var objectAssignMock = jest.genMockFunction();
37
Object.assign = objectAssignMock;
38
eval(transform(code));
39
return expect(objectAssignMock);
40
}
41
42
afterEach(function() {
43
Object.assign = originalAssign;
44
});
45
46
// Polyfill sanity checks
47
48
it('has access to a working Object.assign implementation', function() {
49
expect(typeof Object.assign).toBe('function');
50
expect(Object.assign({ b: 2 }, null, { a: 1 })).toEqual({ a: 1, b: 2 });
51
});
52
53
// Semantic tests.
54
55
it('uses Object.assign with an empty new target object', function() {
56
expectObjectAssign(
57
'var xy = { ...x, y: 2 }'
58
).toBeCalledWith({}, x, { y: 2 });
59
});
60
61
it('coalesces consecutive properties into a single object', function() {
62
expectObjectAssign(
63
'var xyz = { ...x, y: 2, z: z }'
64
).toBeCalledWith({}, x, { y: 2, z: z });
65
66
});
67
68
it('avoids an unnecessary empty object when spread is not first', function() {
69
expectObjectAssign(
70
'var xy = { x: 1, ...y }'
71
).toBeCalledWith({ x: 1}, y);
72
});
73
74
it('passes the same value multiple times to Object.assign', function() {
75
expectObjectAssign(
76
'var xyz = { x: 1, y: 2, ...z, ...z }'
77
).toBeCalledWith({ x: 1, y: 2}, z, z);
78
});
79
80
it('keeps object literals as separate arguments to assign', function() {
81
expectObjectAssign(
82
'var xyz = { x: 1, ...({ y: 2 }), z: 3 }'
83
).toBeCalledWith({ x: 1}, { y: 2 }, {z: 3 });
84
});
85
86
it('does not call assign when there are no spread properties', function() {
87
expectObjectAssign(
88
'var xy = { x: 1, y: 2 }'
89
).not.toBeCalled();
90
});
91
92
// Syntax tests.
93
94
it('should preserve extra whitespace', function() {
95
expectTransform(
96
'let xyz = { x: 1, y : \n 2, ... \nz, ... z }',
97
'let xyz = Object.assign({ x: 1, y : \n 2}, \nz, z )'
98
);
99
});
100
101
it('should preserve parenthesis', function() {
102
expectTransform(
103
'let xyz = { x: 1, ...({ y: 2 }), z: 3 }',
104
'let xyz = Object.assign({ x: 1}, ({ y: 2 }), {z: 3 })'
105
);
106
});
107
108
it('should remove trailing commas after properties', function() {
109
expectTransform(
110
'let xyz = { ...x, y: 1, }',
111
'let xyz = Object.assign({}, x, {y: 1 })'
112
);
113
});
114
115
it('should remove trailing commas after spread', function() {
116
expectTransform(
117
'let xyz = { x: 1, ...y, }',
118
'let xyz = Object.assign({ x: 1}, y )'
119
);
120
});
121
122
// Don't transform
123
124
it('should not transform destructuring assignment', function() {
125
expectTransform(
126
'let { x, ...y } = z',
127
'let { x, ...y } = z'
128
);
129
});
130
131
// Accessors are unsupported. We leave it unprocessed so that other,
132
// chained transforms, can take over.
133
134
it('should not transform when there are getters', function() {
135
expectTransform(
136
'let xy = { ...x, get x() { } }',
137
'let xy = { ...x, get x() { } }'
138
);
139
});
140
141
it('should not transform when there are setters', function() {
142
expectTransform(
143
'let xy = { set x(v) { }, ...y }',
144
'let xy = { set x(v) { }, ...y }'
145
);
146
});
147
148
it('should silently ignore falsy values', function() {
149
/*globals obj*/
150
var code = transform([
151
'var x = null;',
152
'var y = { y: "y" };',
153
'var obj = { ...x, ...y, ...{ ...false, z: "z", ...y } };'
154
].join('\n'));
155
156
eval(code);
157
158
expect(obj).toEqual({ y: 'y', z: 'z' });
159
});
160
});
161
162