8
8
*
9
9
*/
10
10
11
- import { parse , expression } from '../../../tests/utils' ;
11
+ import {
12
+ parse ,
13
+ statement ,
14
+ expression ,
15
+ noopImporter ,
16
+ makeMockImporter ,
17
+ } from '../../../tests/utils' ;
12
18
import getPropertyName from '../getPropertyName' ;
13
19
14
20
describe ( 'getPropertyName' , ( ) => {
@@ -17,53 +23,63 @@ describe('getPropertyName', () => {
17
23
return root . get ( 'body' , root . node . body . length - 1 , 'expression' ) ;
18
24
}
19
25
26
+ const mockImporter = makeMockImporter ( {
27
+ foo : statement ( `
28
+ export default "name";
29
+ ` ) . get ( 'declaration' ) ,
30
+
31
+ bar : statement ( `
32
+ export default { baz: "name" };
33
+ ` ) . get ( 'declaration' ) ,
34
+ } ) ;
35
+
20
36
it ( 'returns the name for a normal property' , ( ) => {
21
37
const def = expression ( '{ foo: 1 }' ) ;
22
38
const param = def . get ( 'properties' , 0 ) ;
23
39
24
- expect ( getPropertyName ( param ) ) . toBe ( 'foo' ) ;
40
+ expect ( getPropertyName ( param , noopImporter ) ) . toBe ( 'foo' ) ;
25
41
} ) ;
26
42
27
43
it ( 'returns the name of a object type spread property' , ( ) => {
28
44
const def = expression ( '(a: { ...foo })' ) ;
29
45
const param = def . get ( 'typeAnnotation' , 'typeAnnotation' , 'properties' , 0 ) ;
30
46
31
- expect ( getPropertyName ( param ) ) . toBe ( 'foo' ) ;
47
+ expect ( getPropertyName ( param , noopImporter ) ) . toBe ( 'foo' ) ;
32
48
} ) ;
33
49
34
50
it ( 'creates name for computed properties' , ( ) => {
35
51
const def = expression ( '{ [foo]: 21 }' ) ;
36
52
const param = def . get ( 'properties' , 0 ) ;
37
53
38
- expect ( getPropertyName ( param ) ) . toBe ( '@computed#foo' ) ;
54
+ expect ( getPropertyName ( param , noopImporter ) ) . toBe ( '@computed#foo' ) ;
39
55
} ) ;
40
56
41
57
it ( 'creates name for computed properties from string' , ( ) => {
42
58
const def = expression ( '{ ["foo"]: 21 }' ) ;
43
59
const param = def . get ( 'properties' , 0 ) ;
44
60
45
- expect ( getPropertyName ( param ) ) . toBe ( 'foo' ) ;
61
+ expect ( getPropertyName ( param , noopImporter ) ) . toBe ( 'foo' ) ;
46
62
} ) ;
47
63
48
64
it ( 'creates name for computed properties from int' , ( ) => {
49
65
const def = expression ( '{ [31]: 21 }' ) ;
50
66
const param = def . get ( 'properties' , 0 ) ;
51
67
52
- expect ( getPropertyName ( param ) ) . toBe ( '31' ) ;
68
+ expect ( getPropertyName ( param , noopImporter ) ) . toBe ( '31' ) ;
53
69
} ) ;
54
70
55
71
it ( 'returns null for computed properties from regex' , ( ) => {
56
72
const def = expression ( '{ [/31/]: 21 }' ) ;
57
73
const param = def . get ( 'properties' , 0 ) ;
58
74
59
- expect ( getPropertyName ( param ) ) . toBe ( null ) ;
75
+ expect ( getPropertyName ( param , noopImporter ) ) . toBe ( null ) ;
60
76
} ) ;
61
77
62
78
it ( 'returns null for to complex computed properties' , ( ) => {
63
79
const def = expression ( '{ [() => {}]: 21 }' ) ;
64
80
const param = def . get ( 'properties' , 0 ) ;
65
81
66
- expect ( getPropertyName ( param ) ) . toBe ( null ) ;
82
+ expect ( getPropertyName ( param , noopImporter ) ) . toBe ( null ) ;
67
83
} ) ;
68
84
69
85
it ( 'resolves simple variables' , ( ) => {
@@ -74,7 +90,18 @@ describe('getPropertyName', () => {
74
90
` ) ;
75
91
const param = def . get ( 'properties' , 0 ) ;
76
92
77
- expect ( getPropertyName ( param ) ) . toBe ( 'name' ) ;
93
+ expect ( getPropertyName ( param , noopImporter ) ) . toBe ( 'name' ) ;
94
+ } ) ;
95
+
96
+ it ( 'resolves imported variables' , ( ) => {
97
+ const def = parsePath ( `
98
+ import foo from 'foo';
99
+
100
+ ({ [foo]: 21 });
101
+ ` ) ;
102
+ const param = def . get ( 'properties' , 0 ) ;
103
+
104
+ expect ( getPropertyName ( param , mockImporter ) ) . toBe ( 'name' ) ;
78
105
} ) ;
79
106
80
107
it ( 'resolves simple member expressions' , ( ) => {
@@ -85,6 +112,17 @@ describe('getPropertyName', () => {
85
112
` ) ;
86
113
const param = def . get ( 'properties' , 0 ) ;
87
114
88
- expect ( getPropertyName ( param ) ) . toBe ( 'name' ) ;
115
+ expect ( getPropertyName ( param , noopImporter ) ) . toBe ( 'name' ) ;
116
+ } ) ;
117
+
118
+ it ( 'resolves imported member expressions' , ( ) => {
119
+ const def = parsePath ( `
120
+ import bar from 'bar';
121
+
122
+ ({ [bar.baz]: 21 });
123
+ ` ) ;
124
+ const param = def . get ( 'properties' , 0 ) ;
125
+
126
+ expect ( getPropertyName ( param , mockImporter ) ) . toBe ( 'name' ) ;
89
127
} ) ;
90
128
} ) ;
0 commit comments