1
- import { transform } from 'babel-core' ;
1
+ import * as babel6 from 'babel-core' ;
2
+ import * as babel7 from '@babel/core' ;
2
3
import { expect } from 'chai' ;
3
4
import { minify } from 'uglify-js' ;
4
5
@@ -7,128 +8,170 @@ const uglify = code => minify(code, {
7
8
mangle : false
8
9
} ) . code ;
9
10
10
- const config = {
11
- plugins : [
12
- './src' ,
13
- [ 'transform-react-jsx' , { pragma : 'j' } ] ,
14
- [ 'transform-es2015-arrow-functions' , { } ]
15
- ]
16
- } ;
11
+ const runTests = ( label , transform ) => {
12
+ describe ( label , ( ) => {
13
+ describe ( 'jsx-remove-data-test-id' , ( ) => {
14
+ it ( 'does not replace data-something-else' , ( ) => {
15
+ const code = '<p data-something-else="cake-day">hi, finally it is cake time</p>' ;
16
+ const actual = transform ( code , { usePlugin : true } ) ;
17
+ const expected = transform ( code ) ;
18
+ expect ( uglify ( actual ) ) . to . equal ( uglify ( expected ) ) ;
19
+ } ) ;
17
20
18
- const configWithoutPlugin = {
19
- plugins : [
20
- [ ' transform-react-jsx' , { pragma : 'j' } ] ,
21
- [ 'transform-es2015-arrow-functions' , { } ]
22
- ]
23
- } ;
21
+ it ( 'does not remove attributes that contain "data-test-id" in part only' , ( ) => {
22
+ const code = '<p data-test-id-not="not-test-id">hi, finally it is cake time</p>' ;
23
+ const actual = transform ( code , { usePlugin : true } ) ;
24
+ const expected = transform ( code ) ;
25
+ expect ( uglify ( actual ) ) . to . equal ( uglify ( expected ) ) ;
26
+ } ) ;
24
27
25
- describe ( 'jsx-remove- data-test-id', ( ) => {
26
- it ( 'does not replace data-something-else' , ( ) => {
27
- const code = '<p data-something-else="cake-day">hi, finally it is cake time </p>' ;
28
- const actual = transform ( code , config ) . code ;
29
- const expected = transform ( code , configWithoutPlugin ) . code ;
30
- expect ( uglify ( actual ) ) . to . equal ( uglify ( expected ) ) ;
31
- } ) ;
28
+ it ( 'removes data-test-id', ( ) => {
29
+ const code = '<p data-test-id="test-id"></p>' ;
30
+ const expectedCode = '<p> </p>' ;
31
+ const actual = transform ( code , { usePlugin : true } ) ;
32
+ const expected = transform ( expectedCode ) ;
33
+ expect ( uglify ( actual ) ) . to . equal ( uglify ( expected ) ) ;
34
+ } ) ;
32
35
33
- it ( 'does not remove attributes that contain "data-test-id" in part only' , ( ) => {
34
- const code = '<p data-test-id-not="not-test-id">hi, finally it is cake time</p>' ;
35
- const actual = transform ( code , config ) . code ;
36
- const expected = transform ( code , configWithoutPlugin ) . code ;
37
- expect ( uglify ( actual ) ) . to . equal ( uglify ( expected ) ) ;
38
- } ) ;
36
+ it ( 'removes data-test-id funcs' , ( ) => {
37
+ const code = '<p data-test-id={() => {}}></p>' ;
38
+ const expectedCode = '<p></p>' ;
39
+ const actual = transform ( code , { usePlugin : true } ) ;
40
+ const expected = transform ( expectedCode ) ;
41
+ expect ( uglify ( actual ) ) . to . equal ( uglify ( expected ) ) ;
42
+ } ) ;
39
43
40
- it ( 'removes data-test-id' , ( ) => {
41
- const code = '<p data-test-id="test-id" ></p>' ;
42
- const expectedCode = '<p></p>' ;
43
- const actual = transform ( code , config ) . code ;
44
- const expected = transform ( expectedCode , configWithoutPlugin ) . code ;
45
- expect ( uglify ( actual ) ) . to . equal ( uglify ( expected ) ) ;
46
- } ) ;
44
+ it ( 'removes data-test-id bools ' , ( ) => {
45
+ const code = '<p data-test-id={false} ></p>' ;
46
+ const expectedCode = '<p></p>' ;
47
+ const actual = transform ( code , { usePlugin : true } ) ;
48
+ const expected = transform ( expectedCode ) ;
49
+ expect ( uglify ( actual ) ) . to . equal ( uglify ( expected ) ) ;
50
+ } ) ;
47
51
48
- it ( 'removes data-test-id funcs' , ( ) => {
49
- const code = '<p data-test-id={() => {}}></p>' ;
50
- const expectedCode = '<p></p>' ;
51
- const actual = transform ( code , config ) . code ;
52
- const expected = transform ( expectedCode , configWithoutPlugin ) . code ;
53
- expect ( uglify ( actual ) ) . to . equal ( uglify ( expected ) ) ;
54
- } ) ;
52
+ describe ( 'with invalid options.attributes' , ( ) => {
53
+ it ( 'throws error when attributes is empty string' , ( ) => {
54
+ const code = '<p selenium-id={false}></p>' ;
55
+ const expectedCode = '<p></p>' ;
56
+ const action = ( ) => transform ( code , {
57
+ useErroneousAttributes : true ,
58
+ attributes : ''
59
+ } ) ;
60
+ expect ( action ) . to . throw ( ) ;
61
+ } ) ;
55
62
56
- it ( 'removes data-test-id bools' , ( ) => {
57
- const code = '<p data-test-id={false}></p>' ;
58
- const expectedCode = '<p></p>' ;
59
- const actual = transform ( code , config ) . code ;
60
- const expected = transform ( expectedCode , configWithoutPlugin ) . code ;
61
- expect ( uglify ( actual ) ) . to . equal ( uglify ( expected ) ) ;
62
- } ) ;
63
+ it ( 'throws error when attributes is empty array' , ( ) => {
64
+ const code = '<p selenium-id={false}></p>' ;
65
+ const expectedCode = '<p></p>' ;
66
+ const action = ( ) => transform ( code , {
67
+ useErroneousAttributes : true ,
68
+ attributes : [ ]
69
+ } ) ;
70
+ expect ( action ) . to . throw ( ) ;
71
+ } ) ;
72
+ } )
63
73
64
- describe ( 'with invalid options.attributes' , ( ) => {
65
- it ( 'throws error when attributes is empty string' , ( ) => {
66
- const configWithErroneousAttributesOption = {
67
- plugins : [
68
- [ './src' , { attributes : '' } ] ,
69
- [ 'transform-react-jsx' , { pragma : 'j' } ] ,
70
- [ 'transform-es2015-arrow-functions' , { } ]
71
- ]
72
- } ;
73
- const code = '<p selenium-id={false}></p>' ;
74
- const expectedCode = '<p></p>' ;
75
- const action = ( ) => transform ( code , configWithErroneousAttributesOption ) ;
76
- expect ( action ) . to . throw ( ) ;
77
- } ) ;
74
+ describe ( 'with valid options.attributes' , ( ) => {
75
+ it ( 'does not remove attributes that match options.attributes in part only' , ( ) => {
76
+ const code = '<p selenium-id-not="not-test-id" no-useless-attr="useless">hi, finally it is cake time</p>' ;
77
+ const actual = transform ( code , { useValidAttributes : true } ) ;
78
+ const expected = transform ( code ) ;
79
+ expect ( uglify ( actual ) ) . to . equal ( uglify ( expected ) ) ;
80
+ } ) ;
78
81
79
- it ( 'throws error when attributes is empty array' , ( ) => {
80
- const configWithErroneousAttributesOption = {
81
- plugins : [
82
- [ './src' , { attributes : [ ] } ] ,
83
- [ 'transform-react-jsx' , { pragma : 'j' } ] ,
84
- [ 'transform-es2015-arrow-functions' , { } ]
85
- ]
86
- } ;
87
- const code = '<p selenium-id={false}></p>' ;
88
- const expectedCode = '<p></p>' ;
89
- const action = ( ) => transform ( code , configWithErroneousAttributesOption ) ;
90
- expect ( action ) . to . throw ( ) ;
91
- } ) ;
92
- } )
82
+ it ( 'removes options.attributes' , ( ) => {
83
+ const code = '<p selenium-id="test-id" useless-attr="useless"></p>' ;
84
+ const expectedCode = '<p></p>' ;
85
+ const actual = transform ( code , { useValidAttributes : true } ) ;
86
+ const expected = transform ( expectedCode ) ;
87
+ expect ( uglify ( actual ) ) . to . equal ( uglify ( expected ) ) ;
88
+ } ) ;
93
89
94
- describe ( 'with valid options.attributes' , ( ) => {
95
- const configWithValidAttributesOption = {
96
- plugins : [
97
- [ './src' , { attributes : [ 'selenium-id' , 'useless-attr' ] } ] ,
98
- [ 'transform-react-jsx' , { pragma : 'j' } ] ,
99
- [ 'transform-es2015-arrow-functions' , { } ]
100
- ]
101
- } ;
90
+ it ( 'removes options.attributes funcs' , ( ) => {
91
+ const code = '<p selenium-id={() => {}} useless-attr={() => {}}></p>' ;
92
+ const expectedCode = '<p></p>' ;
93
+ const actual = transform ( code , { useValidAttributes : true } ) ;
94
+ const expected = transform ( expectedCode ) ;
95
+ expect ( uglify ( actual ) ) . to . equal ( uglify ( expected ) ) ;
96
+ } ) ;
102
97
103
- it ( 'does not remove attributes that match options.attributes in part only' , ( ) => {
104
- const code = '<p selenium-id-not="not-test-id" no-useless-attr="useless">hi, finally it is cake time</p>' ;
105
- const actual = transform ( code , configWithValidAttributesOption ) . code ;
106
- const expected = transform ( code , configWithoutPlugin ) . code ;
107
- expect ( uglify ( actual ) ) . to . equal ( uglify ( expected ) ) ;
108
- } ) ;
109
-
110
- it ( 'removes options.attributes' , ( ) => {
111
- const code = '<p selenium-id="test-id" useless-attr="useless"></p>' ;
112
- const expectedCode = '<p></p>' ;
113
- const actual = transform ( code , configWithValidAttributesOption ) . code ;
114
- const expected = transform ( expectedCode , configWithoutPlugin ) . code ;
115
- expect ( uglify ( actual ) ) . to . equal ( uglify ( expected ) ) ;
116
- } ) ;
117
-
118
- it ( 'removes options.attributes funcs' , ( ) => {
119
- const code = '<p selenium-id={() => {}} useless-attr={() => {}}></p>' ;
120
- const expectedCode = '<p></p>' ;
121
- const actual = transform ( code , configWithValidAttributesOption ) . code ;
122
- const expected = transform ( expectedCode , configWithoutPlugin ) . code ;
123
- expect ( uglify ( actual ) ) . to . equal ( uglify ( expected ) ) ;
124
- } ) ;
125
-
126
- it ( 'removes options.attributes bools' , ( ) => {
127
- const code = '<p selenium-id={false} useless-attr={true}></p>' ;
128
- const expectedCode = '<p></p>' ;
129
- const actual = transform ( code , configWithValidAttributesOption ) . code ;
130
- const expected = transform ( expectedCode , configWithoutPlugin ) . code ;
131
- expect ( uglify ( actual ) ) . to . equal ( uglify ( expected ) ) ;
98
+ it ( 'removes options.attributes bools' , ( ) => {
99
+ const code = '<p selenium-id={false} useless-attr={true}></p>' ;
100
+ const expectedCode = '<p></p>' ;
101
+ const actual = transform ( code , { useValidAttributes : true } ) ;
102
+ const expected = transform ( expectedCode ) ;
103
+ expect ( uglify ( actual ) ) . to . equal ( uglify ( expected ) ) ;
104
+ } ) ;
105
+ } )
132
106
} ) ;
133
107
} )
134
- } ) ;
108
+ }
109
+
110
+ runTests (
111
+ "babel6" ,
112
+ (
113
+ code ,
114
+ {
115
+ useErroneousAttributes = false ,
116
+ useValidAttributes = false ,
117
+ usePlugin = false ,
118
+ attributes
119
+ } = { }
120
+ ) => {
121
+ let plugins ;
122
+ if ( useErroneousAttributes ) {
123
+ plugins = [
124
+ [ "./src" , { attributes } ] ,
125
+ [ "transform-react-jsx" , { pragma : "j" } ] ,
126
+ [ "transform-es2015-arrow-functions" , { } ]
127
+ ] ;
128
+ } else if ( useValidAttributes ) {
129
+ plugins = [
130
+ [ "./src" , { attributes : [ "selenium-id" , "useless-attr" ] } ] ,
131
+ [ "transform-react-jsx" , { pragma : "j" } ] ,
132
+ [ "transform-es2015-arrow-functions" , { } ]
133
+ ] ;
134
+ } else {
135
+ plugins = [
136
+ usePlugin && "./src" ,
137
+ [ "transform-react-jsx" , { pragma : "j" } ] ,
138
+ [ "transform-es2015-arrow-functions" , { } ]
139
+ ] . filter ( Boolean ) ;
140
+ }
141
+ return babel6 . transform ( code , { plugins } ) . code ;
142
+ }
143
+ ) ;
144
+ runTests (
145
+ "babel7" ,
146
+ (
147
+ code ,
148
+ {
149
+ useErroneousAttributes = false ,
150
+ useValidAttributes = false ,
151
+ usePlugin = false ,
152
+ attributes
153
+ } = { }
154
+ ) => {
155
+ let plugins
156
+ if ( useErroneousAttributes ) {
157
+ plugins = [
158
+ [ './src' , { attributes } ] ,
159
+ [ "@babel/transform-react-jsx" , { pragma : "j" } ] ,
160
+ [ "@babel/transform-arrow-functions" , { } ]
161
+ ]
162
+ } else if ( useValidAttributes ) {
163
+ plugins = [
164
+ [ './src' , { attributes : [ 'selenium-id' , 'useless-attr' ] } ] ,
165
+ [ "@babel/transform-react-jsx" , { pragma : "j" } ] ,
166
+ [ "@babel/transform-arrow-functions" , { } ]
167
+ ]
168
+ } else {
169
+ plugins = [
170
+ usePlugin && "./src" ,
171
+ [ "@babel/transform-react-jsx" , { pragma : "j" } ] ,
172
+ [ "@babel/transform-arrow-functions" , { } ]
173
+ ] . filter ( Boolean )
174
+ }
175
+ return babel7 . transformSync ( code , { plugins } ) . code ;
176
+ }
177
+ ) ;
0 commit comments