6
6
*
7
7
*/
8
8
9
- import SuperTestClass , { TestClass } from './__fixtures__/class-mocks-types' ;
9
+ import SuperTestClass , * as testTypes from './__fixtures__/class-mocks-types' ;
10
10
jest . mock ( './__fixtures__/class-mocks-types' ) ;
11
11
12
+ describe ( 'Testing the mocking of exported functions' , ( ) => {
13
+ it ( 'can mock a directly exported function' , ( ) => {
14
+ jest . spyOn ( testTypes , 'testFunction1' ) . mockImplementation ( ( ) => {
15
+ return 'mockTestFunction' ;
16
+ } ) ;
17
+ expect ( testTypes . testFunction1 ( ) ) . toBe ( 'mockTestFunction' ) ;
18
+ } ) ;
19
+
20
+ it ( 'can mock an indirectly exported function' , ( ) => {
21
+ jest . spyOn ( testTypes , 'testFunction2' ) . mockImplementation ( ( ) => {
22
+ return 'mockTestFunction' ;
23
+ } ) ;
24
+ expect ( testTypes . testFunction2 ( ) ) . toBe ( 'mockTestFunction' ) ;
25
+ } ) ;
26
+
27
+ it ( 'can mock an indirectly exported anonymous function' , ( ) => {
28
+ jest . spyOn ( testTypes , 'testFunction3' ) . mockImplementation ( ( ) => {
29
+ return 'mockTestFunction' ;
30
+ } ) ;
31
+ expect ( testTypes . testFunction3 ( ) ) . toBe ( 'mockTestFunction' ) ;
32
+ } ) ;
33
+ } ) ;
34
+
12
35
describe ( 'Testing the mocking of a class hierarchy defined in a single import' , ( ) => {
13
36
it ( 'can call an instance method - Auto-mocked class' , ( ) => {
14
37
const mockTestMethod = jest
@@ -25,11 +48,11 @@ describe('Testing the mocking of a class hierarchy defined in a single import',
25
48
26
49
it ( 'can call a superclass instance method - Auto-mocked class' , ( ) => {
27
50
const mockTestMethod = jest
28
- . spyOn ( TestClass . prototype , 'testMethod' )
51
+ . spyOn ( testTypes . TestClass . prototype , 'testMethod' )
29
52
. mockImplementation ( ( ) => {
30
53
return 'mockTestMethod' ;
31
54
} ) ;
32
- const testClassInstance = new TestClass ( ) ;
55
+ const testClassInstance = new testTypes . TestClass ( ) ;
33
56
expect ( testClassInstance . testMethod ( ) ) . toBe ( 'mockTestMethod' ) ;
34
57
expect ( mockTestMethod ) . toHaveBeenCalledTimes ( 1 ) ;
35
58
} ) ;
@@ -49,11 +72,11 @@ describe('Testing the mocking of a class hierarchy defined in a single import',
49
72
50
73
it ( 'can call a superclass instance method named "get" - Auto-mocked class' , ( ) => {
51
74
const mockTestMethod = jest
52
- . spyOn ( TestClass . prototype , 'get' )
75
+ . spyOn ( testTypes . TestClass . prototype , 'get' )
53
76
. mockImplementation ( ( ) => {
54
77
return 'mockTestMethod' ;
55
78
} ) ;
56
- const testClassInstance = new TestClass ( ) ;
79
+ const testClassInstance = new testTypes . TestClass ( ) ;
57
80
expect ( testClassInstance . get ( ) ) . toBe ( 'mockTestMethod' ) ;
58
81
expect ( mockTestMethod ) . toHaveBeenCalledTimes ( 1 ) ;
59
82
@@ -75,65 +98,17 @@ describe('Testing the mocking of a class hierarchy defined in a single import',
75
98
76
99
it ( 'can call a superclass instance method named "set" - Auto-mocked class' , ( ) => {
77
100
const mockTestMethod = jest
78
- . spyOn ( TestClass . prototype , 'set' )
101
+ . spyOn ( testTypes . TestClass . prototype , 'set' )
79
102
. mockImplementation ( ( ) => {
80
103
return 'mockTestMethod' ;
81
104
} ) ;
82
- const testClassInstance = new TestClass ( ) ;
105
+ const testClassInstance = new testTypes . TestClass ( ) ;
83
106
expect ( testClassInstance . set ( ) ) . toBe ( 'mockTestMethod' ) ;
84
107
expect ( mockTestMethod ) . toHaveBeenCalledTimes ( 1 ) ;
85
108
86
109
mockTestMethod . mockClear ( ) ;
87
110
} ) ;
88
111
89
- it ( 'can read a value from an instance getter - Auto-mocked class' , ( ) => {
90
- const mockTestMethod = jest
91
- . spyOn ( SuperTestClass . prototype , 'testAccessor' , 'get' )
92
- . mockImplementation ( ( ) => {
93
- return 'mockTestAccessor' ;
94
- } ) ;
95
- const testClassInstance = new SuperTestClass ( ) ;
96
- expect ( testClassInstance . testAccessor ) . toBe ( 'mockTestAccessor' ) ;
97
- expect ( mockTestMethod ) . toHaveBeenCalledTimes ( 1 ) ;
98
-
99
- mockTestMethod . mockClear ( ) ;
100
- } ) ;
101
-
102
- it ( 'can read a value from a superclass instance getter - Auto-mocked class' , ( ) => {
103
- const mockTestMethod = jest
104
- . spyOn ( TestClass . prototype , 'testAccessor' , 'get' )
105
- . mockImplementation ( ( ) => {
106
- return 'mockTestAccessor' ;
107
- } ) ;
108
- const testClassInstance = new TestClass ( ) ;
109
- expect ( testClassInstance . testAccessor ) . toBe ( 'mockTestAccessor' ) ;
110
- expect ( mockTestMethod ) . toHaveBeenCalledTimes ( 1 ) ;
111
- } ) ;
112
-
113
- it ( 'can write a value to an instance setter - Auto-mocked class' , ( ) => {
114
- const mockTestMethod = jest
115
- . spyOn ( SuperTestClass . prototype , 'testAccessor' , 'set' )
116
- . mockImplementation ( ( _x : string ) => {
117
- return ( ) => { } ;
118
- } ) ;
119
- const testClassInstance = new SuperTestClass ( ) ;
120
- testClassInstance . testAccessor = '' ;
121
- expect ( mockTestMethod ) . toHaveBeenCalledTimes ( 1 ) ;
122
-
123
- mockTestMethod . mockClear ( ) ;
124
- } ) ;
125
-
126
- it ( 'can write a value to a superclass instance setter - Auto-mocked class' , ( ) => {
127
- const mockTestMethod = jest
128
- . spyOn ( TestClass . prototype , 'testAccessor' , 'set' )
129
- . mockImplementation ( ( _x : string ) => {
130
- return ( ) => { } ;
131
- } ) ;
132
- const testClassInstance = new TestClass ( ) ;
133
- testClassInstance . testAccessor = '' ;
134
- expect ( mockTestMethod ) . toHaveBeenCalledTimes ( 1 ) ;
135
- } ) ;
136
-
137
112
it ( 'can call a static method - Auto-mocked class' , ( ) => {
138
113
const mockTestMethod = jest
139
114
. spyOn ( SuperTestClass , 'staticTestMethod' )
@@ -148,11 +123,11 @@ describe('Testing the mocking of a class hierarchy defined in a single import',
148
123
149
124
it ( 'can call a superclass static method - Auto-mocked class' , ( ) => {
150
125
const mockTestMethod = jest
151
- . spyOn ( TestClass , 'staticTestMethod' )
126
+ . spyOn ( testTypes . TestClass , 'staticTestMethod' )
152
127
. mockImplementation ( ( ) => {
153
128
return 'mockTestMethod' ;
154
129
} ) ;
155
- expect ( TestClass . staticTestMethod ( ) ) . toBe ( 'mockTestMethod' ) ;
130
+ expect ( testTypes . TestClass . staticTestMethod ( ) ) . toBe ( 'mockTestMethod' ) ;
156
131
expect ( mockTestMethod ) . toHaveBeenCalledTimes ( 1 ) ;
157
132
} ) ;
158
133
@@ -170,11 +145,11 @@ describe('Testing the mocking of a class hierarchy defined in a single import',
170
145
171
146
it ( 'can call a superclass static method named "get" - Auto-mocked class' , ( ) => {
172
147
const mockTestMethod = jest
173
- . spyOn ( TestClass , 'get' )
148
+ . spyOn ( testTypes . TestClass , 'get' )
174
149
. mockImplementation ( ( ) => {
175
150
return 'mockTestMethod' ;
176
151
} ) ;
177
- expect ( TestClass . get ( ) ) . toBe ( 'mockTestMethod' ) ;
152
+ expect ( testTypes . TestClass . get ( ) ) . toBe ( 'mockTestMethod' ) ;
178
153
expect ( mockTestMethod ) . toHaveBeenCalledTimes ( 1 ) ;
179
154
180
155
mockTestMethod . mockClear ( ) ;
@@ -194,57 +169,13 @@ describe('Testing the mocking of a class hierarchy defined in a single import',
194
169
195
170
it ( 'can call a superclass static method named "set" - Auto-mocked class' , ( ) => {
196
171
const mockTestMethod = jest
197
- . spyOn ( TestClass , 'set' )
172
+ . spyOn ( testTypes . TestClass , 'set' )
198
173
. mockImplementation ( ( ) => {
199
174
return 'mockTestMethod' ;
200
175
} ) ;
201
- expect ( TestClass . set ( ) ) . toBe ( 'mockTestMethod' ) ;
202
- expect ( mockTestMethod ) . toHaveBeenCalledTimes ( 1 ) ;
203
-
204
- mockTestMethod . mockClear ( ) ;
205
- } ) ;
206
-
207
- it ( 'can read a value from a static getter - Auto-mocked class' , ( ) => {
208
- const mockTestMethod = jest
209
- . spyOn ( SuperTestClass , 'staticTestAccessor' , 'get' )
210
- . mockImplementation ( ( ) => {
211
- return 'mockStaticTestAccessor' ;
212
- } ) ;
213
- expect ( SuperTestClass . staticTestAccessor ) . toBe ( 'mockStaticTestAccessor' ) ;
214
- expect ( mockTestMethod ) . toHaveBeenCalledTimes ( 1 ) ;
215
-
216
- mockTestMethod . mockClear ( ) ;
217
- } ) ;
218
-
219
- it ( 'can read a value from a superclass static getter - Auto-mocked class' , ( ) => {
220
- const mockTestMethod = jest
221
- . spyOn ( TestClass , 'staticTestAccessor' , 'get' )
222
- . mockImplementation ( ( ) => {
223
- return 'mockStaticTestAccessor' ;
224
- } ) ;
225
- expect ( TestClass . staticTestAccessor ) . toBe ( 'mockStaticTestAccessor' ) ;
226
- expect ( mockTestMethod ) . toHaveBeenCalledTimes ( 1 ) ;
227
- } ) ;
228
-
229
- it ( 'can write a value to a static setter - Auto-mocked class' , ( ) => {
230
- const mockTestMethod = jest
231
- . spyOn ( SuperTestClass , 'staticTestAccessor' , 'set' )
232
- . mockImplementation ( ( _x : string ) => {
233
- return ( ) => { } ;
234
- } ) ;
235
- SuperTestClass . staticTestAccessor = '' ;
176
+ expect ( testTypes . TestClass . set ( ) ) . toBe ( 'mockTestMethod' ) ;
236
177
expect ( mockTestMethod ) . toHaveBeenCalledTimes ( 1 ) ;
237
178
238
179
mockTestMethod . mockClear ( ) ;
239
180
} ) ;
240
-
241
- it ( 'can write a value to a superclass static setter - Auto-mocked class' , ( ) => {
242
- const mockTestMethod = jest
243
- . spyOn ( TestClass , 'staticTestAccessor' , 'set' )
244
- . mockImplementation ( ( _x : string ) => {
245
- return ( ) => { } ;
246
- } ) ;
247
- TestClass . staticTestAccessor = '' ;
248
- expect ( mockTestMethod ) . toHaveBeenCalledTimes ( 1 ) ;
249
- } ) ;
250
181
} ) ;
0 commit comments