Sync out another jsx transform test.
We added this test internally and it never got added here. We haven't broken it with any transforms *yet* but it's still good to actually run all of our tests here too.
Paul O’Shannessy committed
Mar 3, 2015 at 10:40 UTC
e114988a2c7e9d60690d1332f1ee49775cce4eee
1 file changed
+141
vendor/fbtransform/transforms/__tests__/jsx-test.js
new
+141
@@ -0,0 +1,141 @@
1
+/**
2
+ * Copyright 2013-2015, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree. An additional grant
7
+ * of patent rights can be found in the PATENTS file in the same directory.
8
+ *
9
+ * @emails react-core
10
+ */
11
+
12
+require('mock-modules').autoMockOff();
13
+
14
+describe('jsx', function() {
15
+ var jsx = require('../jsx');
16
+ var utils = require('jstransform/src/utils');
17
+ var esprima = require('esprima-fb');
18
+
19
+ function runWithLiteral(text, isLast) {
20
+ var source = '<div>' + text + '</div>';
21
+ var ast = esprima.parse(source, { range: true });
22
+ var state = utils.createState(source);
23
+ var literal = ast.body[0].expression.children[0];
24
+ state.g.position = literal.range[0];
25
+ jsx.renderJSXLiteral(literal, isLast, state);
26
+ return state;
27
+ }
28
+
29
+ function runWithAttribute(inlineAttribute, isLast) {
30
+ var source = '<div ' + inlineAttribute + ' />';
31
+ var ast = esprima.parse(source, { range: true });
32
+ var state = utils.createState(source);
33
+ var attribute = ast.body[0].expression.openingElement.attributes[0];
34
+ state.g.position = attribute.value.range[0];
35
+ jsx.renderJSXExpressionContainer(
36
+ function() {},
37
+ attribute.value,
38
+ isLast,
39
+ [],
40
+ state
41
+ );
42
+ return state;
43
+ }
44
+
45
+ it('should render simple literal', function() {
46
+ var state = runWithLiteral('a', true);
47
+ expect(state.g.buffer).toEqual('"a"');
48
+ });
49
+
50
+
51
+ it('should render simple literal with single space before', function() {
52
+ var state = runWithLiteral(' a', true);
53
+ expect(state.g.buffer).toEqual('" a"');
54
+ });
55
+
56
+ it('should render simple literal with single space before' +
57
+ ' from multiple spaces', function() {
58
+ var state = runWithLiteral(' \t a', true);
59
+ expect(state.g.buffer).toEqual('" a"');
60
+ });
61
+
62
+ it('should render simple literal with single space after', function() {
63
+ var state = runWithLiteral('a ', true);
64
+ expect(state.g.buffer).toEqual('"a "');
65
+ });
66
+
67
+ it('should render simple literal with single space after' +
68
+ ' from multiple spaces', function() {
69
+ var state = runWithLiteral('a \t \t ', true);
70
+ expect(state.g.buffer).toEqual('"a "');
71
+ });
72
+
73
+ it('should render multiline literal as last', function() {
74
+ var state = runWithLiteral(
75
+ ' sdfsdfsdf\n' +
76
+ ' sdlkfjsdfljs\n' +
77
+ ' ',
78
+ true);
79
+
80
+ expect(state.g.buffer).toEqual(
81
+ '" sdfsdfsdf" + \' \' +\n' +
82
+ ' "sdlkfjsdfljs"\n' +
83
+ ' ');
84
+ });
85
+
86
+ it('should render multiline literal as not last', function() {
87
+ var state = runWithLiteral(
88
+ ' sdfsdfsdf\n' +
89
+ ' sdlkfjsdfljs\n' +
90
+ ' ',
91
+ false);
92
+
93
+ expect(state.g.buffer).toEqual(
94
+ '" sdfsdfsdf" + \' \' +\n' +
95
+ ' "sdlkfjsdfljs", \n' +
96
+ ' ');
97
+ });
98
+
99
+ it('should render attribute expressions', function() {
100
+ var state = runWithAttribute('attr={"foo"}', true);
101
+ expect(state.g.buffer).toEqual('"foo"');
102
+ });
103
+
104
+ it('should render attribute expressions as not last', function() {
105
+ var state = runWithAttribute('attr={"foo"}', false);
106
+ expect(state.g.buffer).toEqual('"foo", ');
107
+ });
108
+
109
+ it('should render attribute expressions with spaces', function() {
110
+ var state = runWithAttribute('attr={ "foo"\n }', true);
111
+ expect(state.g.buffer).toEqual(' "foo"\n ');
112
+ });
113
+
114
+ it('should render attribute expressions with commas before trailing ' +
115
+ 'whitespace', function() {
116
+ var state = runWithAttribute('attr={\n"foo"\n }', false);
117
+ expect(state.g.buffer).toEqual('\n"foo", \n ');
118
+ });
119
+
120
+ it('should render empty child expressions with comments', function() {
121
+ var source = '<div>{/*comment*/}</div>';
122
+ var ast = esprima.parse(source, {range: true});
123
+ var child = ast.body[0].expression.children[0];
124
+ var state = utils.createState(source, child);
125
+ state.g.position = child.range[0];
126
+ jsx.renderJSXExpressionContainer(function() {}, child, true, [], state);
127
+ expect(state.g.buffer).toBe('/*comment*/');
128
+ });
129
+
130
+ it('should not render commas after empty child expressions even if they\'re' +
131
+ 'not last', function() {
132
+ var source = '<div>{/*comment*/}</div>';
133
+ var ast = esprima.parse(source, {range: true});
134
+ var child = ast.body[0].expression.children[0];
135
+ var state = utils.createState(source, child);
136
+ state.g.position = child.range[0];
137
+ jsx.renderJSXExpressionContainer(function() {}, child, false, [], state);
138
+ expect(state.g.buffer).toBe('/*comment*/');
139
+ });
140
+});
141
+