1
1
import { Store } from '@ngxs/store' ;
2
2
3
- import { of } from 'rxjs' ;
3
+ import { Observable , of , throwError } from 'rxjs' ;
4
4
5
5
import { ComponentFixture , TestBed } from '@angular/core/testing' ;
6
6
7
+ import { SENTRY_TOKEN } from '@core/factory/sentry.factory' ;
8
+
7
9
import { GoogleFilePickerDownloadService } from './service/google-file-picker.download.service' ;
8
10
import { GoogleFilePickerComponent } from './google-file-picker.component' ;
9
11
@@ -12,11 +14,21 @@ import { OSFTestingModule, OSFTestingStoreModule } from '@testing/osf.testing.mo
12
14
describe ( 'Component: Google File Picker' , ( ) => {
13
15
let component : GoogleFilePickerComponent ;
14
16
let fixture : ComponentFixture < GoogleFilePickerComponent > ;
17
+
15
18
const googlePickerServiceSpy = {
16
- loadScript : jest . fn ( ) . mockReturnValue ( of ( void 0 ) ) ,
17
- loadGapiModules : jest . fn ( ) . mockReturnValue ( of ( void 0 ) ) ,
19
+ loadScript : jest . fn ( ( ) : Observable < void > => {
20
+ return throwLoadScriptError ? throwError ( ( ) => new Error ( 'loadScript failed' ) ) : of ( void 0 ) ;
21
+ } ) ,
22
+ loadGapiModules : jest . fn ( ( ) : Observable < void > => {
23
+ return throwLoadGapiError ? throwError ( ( ) => new Error ( 'loadGapiModules failed' ) ) : of ( void 0 ) ;
24
+ } ) ,
18
25
} ;
19
26
27
+ let sentrySpy : any ;
28
+
29
+ let throwLoadScriptError = false ;
30
+ let throwLoadGapiError = false ;
31
+
20
32
const handleFolderSelection = jest . fn ( ) ;
21
33
const setDeveloperKey = jest . fn ( ) . mockReturnThis ( ) ;
22
34
const setAppId = jest . fn ( ) . mockReturnThis ( ) ;
@@ -40,7 +52,16 @@ describe('Component: Google File Picker', () => {
40
52
selectSnapshot : jest . fn ( ) . mockReturnValue ( 'mock-token' ) ,
41
53
} ;
42
54
55
+ beforeEach ( ( ) => {
56
+ throwLoadScriptError = false ;
57
+ throwLoadGapiError = false ;
58
+ jest . clearAllMocks ( ) ;
59
+ } ) ;
60
+
43
61
beforeAll ( ( ) => {
62
+ throwLoadScriptError = false ;
63
+ throwLoadGapiError = false ;
64
+
44
65
window . google = {
45
66
picker : {
46
67
Action : null ,
@@ -54,8 +75,6 @@ describe('Component: Google File Picker', () => {
54
75
55
76
describe ( 'isFolderPicker - true' , ( ) => {
56
77
beforeEach ( async ( ) => {
57
- jest . clearAllMocks ( ) ;
58
-
59
78
( window as any ) . google = {
60
79
picker : {
61
80
ViewId : {
@@ -86,6 +105,7 @@ describe('Component: Google File Picker', () => {
86
105
await TestBed . configureTestingModule ( {
87
106
imports : [ OSFTestingModule , GoogleFilePickerComponent ] ,
88
107
providers : [
108
+ { provide : SENTRY_TOKEN , useValue : { captureException : jest . fn ( ) } } ,
89
109
{ provide : GoogleFilePickerDownloadService , useValue : googlePickerServiceSpy } ,
90
110
{
91
111
provide : Store ,
@@ -94,6 +114,9 @@ describe('Component: Google File Picker', () => {
94
114
] ,
95
115
} ) . compileComponents ( ) ;
96
116
117
+ sentrySpy = TestBed . inject ( SENTRY_TOKEN ) ;
118
+ jest . spyOn ( sentrySpy , 'captureException' ) ;
119
+
97
120
fixture = TestBed . createComponent ( GoogleFilePickerComponent ) ;
98
121
component = fixture . componentInstance ;
99
122
fixture . componentRef . setInput ( 'isFolderPicker' , true ) ;
@@ -108,6 +131,7 @@ describe('Component: Google File Picker', () => {
108
131
it ( 'should load script and then GAPI modules and initialize picker' , ( ) => {
109
132
expect ( googlePickerServiceSpy . loadScript ) . toHaveBeenCalled ( ) ;
110
133
expect ( googlePickerServiceSpy . loadGapiModules ) . toHaveBeenCalled ( ) ;
134
+ expect ( sentrySpy . captureException ) . not . toHaveBeenCalled ( ) ;
111
135
112
136
expect ( component . visible ( ) ) . toBeTruthy ( ) ;
113
137
expect ( component . isGFPDisabled ( ) ) . toBeFalsy ( ) ;
@@ -172,7 +196,6 @@ describe('Component: Google File Picker', () => {
172
196
173
197
describe ( 'isFolderPicker - false' , ( ) => {
174
198
beforeEach ( async ( ) => {
175
- jest . clearAllMocks ( ) ;
176
199
( window as any ) . google = {
177
200
picker : {
178
201
ViewId : {
@@ -203,6 +226,7 @@ describe('Component: Google File Picker', () => {
203
226
await TestBed . configureTestingModule ( {
204
227
imports : [ OSFTestingStoreModule , GoogleFilePickerComponent ] ,
205
228
providers : [
229
+ { provide : SENTRY_TOKEN , useValue : { captureException : jest . fn ( ) } } ,
206
230
{ provide : GoogleFilePickerDownloadService , useValue : googlePickerServiceSpy } ,
207
231
{
208
232
provide : Store ,
@@ -211,25 +235,48 @@ describe('Component: Google File Picker', () => {
211
235
] ,
212
236
} ) . compileComponents ( ) ;
213
237
238
+ sentrySpy = TestBed . inject ( SENTRY_TOKEN ) ;
239
+ jest . spyOn ( sentrySpy , 'captureException' ) ;
240
+
214
241
fixture = TestBed . createComponent ( GoogleFilePickerComponent ) ;
215
242
component = fixture . componentInstance ;
216
243
fixture . componentRef . setInput ( 'isFolderPicker' , false ) ;
217
244
fixture . componentRef . setInput ( 'rootFolder' , {
218
245
itemId : 'root-folder-id' ,
219
246
} ) ;
220
247
fixture . componentRef . setInput ( 'handleFolderSelection' , jest . fn ( ) ) ;
248
+ } ) ;
249
+
250
+ it ( 'should fail to load script' , ( ) => {
251
+ throwLoadScriptError = true ;
221
252
fixture . detectChanges ( ) ;
253
+ expect ( googlePickerServiceSpy . loadScript ) . toHaveBeenCalled ( ) ;
254
+ expect ( sentrySpy . captureException ) . toHaveBeenCalledWith ( Error ( 'loadScript failed' ) , {
255
+ tags : {
256
+ feature : 'google-picker load' ,
257
+ } ,
258
+ } ) ;
259
+
260
+ expect ( component . visible ( ) ) . toBeFalsy ( ) ;
261
+ expect ( component . isGFPDisabled ( ) ) . toBeTruthy ( ) ;
222
262
} ) ;
223
263
224
- it ( 'should load script and then GAPI modules and initialize picker' , ( ) => {
264
+ it ( 'should load script and then failr GAPI modules' , ( ) => {
265
+ throwLoadGapiError = true ;
266
+ fixture . detectChanges ( ) ;
225
267
expect ( googlePickerServiceSpy . loadScript ) . toHaveBeenCalled ( ) ;
226
268
expect ( googlePickerServiceSpy . loadGapiModules ) . toHaveBeenCalled ( ) ;
227
-
269
+ expect ( sentrySpy . captureException ) . toHaveBeenCalledWith ( Error ( 'loadGapiModules failed' ) , {
270
+ tags : {
271
+ feature : 'google-picker auth' ,
272
+ } ,
273
+ } ) ;
228
274
expect ( component . visible ( ) ) . toBeFalsy ( ) ;
229
275
expect ( component . isGFPDisabled ( ) ) . toBeTruthy ( ) ;
230
276
} ) ;
231
277
232
278
it ( 'should build the picker with correct configuration' , ( ) => {
279
+ fixture . detectChanges ( ) ;
233
280
component . createPicker ( ) ;
234
281
235
282
expect ( window . google . picker . DocsView ) . toHaveBeenCalledWith ( 'docs' ) ;
0 commit comments