1
1
import { ComponentFixture , TestBed } from '@angular/core/testing' ;
2
- import { HttpClientModule } from '@angular/common/http' ;
3
- import { HttpClientTestingModule } from '@angular/common/http/testing' ;
4
- import { RouterTestingModule } from '@angular/router/testing' ;
5
- import { of , throwError } from 'rxjs' ;
2
+ import { Router , NavigationEnd , RouterModule } from '@angular/router' ;
3
+ import { of } from 'rxjs' ;
6
4
import { IntacctComponent } from './intacct.component' ;
7
- import { errorResponse , workspaceResponse } from './intacct.fixture' ;
5
+ import { HelperService } from 'src/app/core/services/common/helper.service' ;
6
+ import { StorageService } from 'src/app/core/services/common/storage.service' ;
7
+ import { WindowService } from 'src/app/core/services/common/window.service' ;
8
+ import { AppcuesService } from 'src/app/core/services/integration/appcues.service' ;
9
+ import { UserService } from 'src/app/core/services/misc/user.service' ;
8
10
import { SiWorkspaceService } from 'src/app/core/services/si/si-core/si-workspace.service' ;
11
+ import { AppName , AppUrl , IntacctOnboardingState } from 'src/app/core/models/enum/enum.model' ;
12
+ import { mockUser , testOnboardingState , workspaceResponse } from './intacct.fixture' ;
13
+ import { IntacctWorkspace } from 'src/app/core/models/intacct/db/workspaces.model' ;
9
14
10
- xdescribe ( 'SiComponent ', ( ) => {
15
+ describe ( 'IntacctComponent ', ( ) => {
11
16
let component : IntacctComponent ;
12
17
let fixture : ComponentFixture < IntacctComponent > ;
13
- let workspace : SiWorkspaceService ;
18
+ let userServiceSpy : jasmine . SpyObj < UserService > ;
19
+ let workspaceServiceSpy : jasmine . SpyObj < SiWorkspaceService > ;
20
+ let helperServiceSpy : jasmine . SpyObj < HelperService > ;
21
+ let storageServiceSpy : jasmine . SpyObj < StorageService > ;
22
+ let routerSpy : jasmine . SpyObj < Router > ;
23
+ let windowServiceMock : Partial < WindowService > ;
24
+ let appcuesServiceSpy : jasmine . SpyObj < AppcuesService > ;
14
25
15
26
beforeEach ( async ( ) => {
16
- const localStorageDump = {
17
-
18
- org_id : 'abcdefgh'
19
- } ;
20
- localStorage . setItem ( 'user' , JSON . stringify ( localStorageDump ) ) ;
21
- const service1 = {
22
- getWorkspace : ( ) => of ( workspaceResponse ) ,
23
- postWorkspace : ( ) => of ( workspaceResponse )
24
- } ;
25
- await TestBed . configureTestingModule ( {
26
- imports : [ RouterTestingModule , HttpClientModule , HttpClientTestingModule ] ,
27
- declarations : [ IntacctComponent ] ,
28
- providers : [
29
- { provide : SiWorkspaceService , useValue : service1 }
30
- ]
31
- } )
32
- . compileComponents ( ) ;
33
-
34
- fixture = TestBed . createComponent ( IntacctComponent ) ;
35
- component = fixture . componentInstance ;
36
- workspace = TestBed . inject ( SiWorkspaceService ) ;
37
- fixture . detectChanges ( ) ;
38
- } ) ;
39
-
40
- it ( 'should create' , ( ) => {
41
- expect ( component ) . toBeTruthy ( ) ;
42
- } ) ;
43
-
44
- it ( 'ngOnIng function check' , async ( ) => {
45
- expect ( ( component as any ) . getOrCreateWorkspace ( ) ) . toBeUndefined ( ) ;
46
- spyOn ( workspace , 'getWorkspace' ) . and . returnValue ( of ( [ ] ) ) ;
47
- expect ( ( component as any ) . getOrCreateWorkspace ( ) ) . toBeUndefined ( ) ;
48
- } ) ;
49
- } ) ;
27
+ const userSpy = jasmine . createSpyObj ( 'UserService' , [ 'getUserProfile' ] ) ;
28
+ const workspaceSpy = jasmine . createSpyObj ( 'SiWorkspaceService' , [ 'getWorkspace' , 'postWorkspace' , 'syncFyleDimensions' , 'syncIntacctDimensions' ] ) ;
29
+ const helperSpy = jasmine . createSpyObj ( 'HelperService' , [ 'setBaseApiURL' ] ) ;
30
+ const storageSpy = jasmine . createSpyObj ( 'StorageService' , [ 'set' ] ) ;
31
+ const routerSpyObj = jasmine . createSpyObj ( 'Router' , [ 'navigateByUrl' , 'events' ] ) ;
32
+ const appcuesSpy = jasmine . createSpyObj ( 'AppcuesService' , [ 'initialiseAppcues' ] ) ;
33
+
34
+ windowServiceMock = {
35
+ get nativeWindow ( ) {
36
+ return {
37
+ location : {
38
+ pathname : '/integrations/intacct'
39
+ }
40
+ } as Window ;
41
+ }
42
+ } ;
43
+
44
+ await TestBed . configureTestingModule ( {
45
+ declarations : [ IntacctComponent ] ,
46
+ imports : [ RouterModule ] ,
47
+ providers : [
48
+ { provide : HelperService , useValue : helperSpy } ,
49
+ { provide : AppcuesService , useValue : appcuesSpy } ,
50
+ { provide : Router , useValue : routerSpyObj } ,
51
+ { provide : StorageService , useValue : storageSpy } ,
52
+ { provide : UserService , useValue : userSpy } ,
53
+ { provide : SiWorkspaceService , useValue : workspaceSpy } ,
54
+ { provide : WindowService , useValue : windowServiceMock }
55
+ ]
56
+ } ) . compileComponents ( ) ;
57
+
58
+ userServiceSpy = TestBed . inject ( UserService ) as jasmine . SpyObj < UserService > ;
59
+ workspaceServiceSpy = TestBed . inject ( SiWorkspaceService ) as jasmine . SpyObj < SiWorkspaceService > ;
60
+ helperServiceSpy = TestBed . inject ( HelperService ) as jasmine . SpyObj < HelperService > ;
61
+ storageServiceSpy = TestBed . inject ( StorageService ) as jasmine . SpyObj < StorageService > ;
62
+ routerSpy = TestBed . inject ( Router ) as jasmine . SpyObj < Router > ;
63
+ ( routerSpy . events as any ) = of ( new NavigationEnd ( 0 , '' , '' ) ) ;
64
+ appcuesServiceSpy = TestBed . inject ( AppcuesService ) as jasmine . SpyObj < AppcuesService > ;
65
+
66
+ userServiceSpy . getUserProfile . and . returnValue ( mockUser ) ;
67
+ workspaceServiceSpy . getWorkspace . and . returnValue ( of ( workspaceResponse ) ) ;
68
+ workspaceServiceSpy . syncFyleDimensions . and . returnValue ( of ( ) ) ;
69
+ workspaceServiceSpy . syncIntacctDimensions . and . returnValue ( of ( ) ) ;
70
+
71
+ fixture = TestBed . createComponent ( IntacctComponent ) ;
72
+ component = fixture . componentInstance ;
73
+ } ) ;
74
+
75
+ it ( 'should create' , ( ) => {
76
+ expect ( component ) . toBeTruthy ( ) ;
77
+ } ) ;
78
+
79
+ it ( 'should setup workspace and navigate when workspace exists' , ( ) => {
80
+ fixture . detectChanges ( ) ;
81
+
82
+ expect ( helperServiceSpy . setBaseApiURL ) . toHaveBeenCalledWith ( AppUrl . INTACCT ) ;
83
+ expect ( workspaceServiceSpy . getWorkspace ) . toHaveBeenCalledWith ( 'mock org id' ) ;
84
+ expect ( storageServiceSpy . set ) . toHaveBeenCalledWith ( 'workspaceId' , 1 ) ;
85
+ expect ( storageServiceSpy . set ) . toHaveBeenCalledWith ( 'onboarding-state' , IntacctOnboardingState . CONNECTION ) ;
86
+ expect ( workspaceServiceSpy . syncFyleDimensions ) . toHaveBeenCalled ( ) ;
87
+ expect ( workspaceServiceSpy . syncIntacctDimensions ) . toHaveBeenCalled ( ) ;
88
+ expect ( routerSpy . navigateByUrl ) . toHaveBeenCalledWith ( '/integrations/intacct/onboarding/landing' ) ;
89
+ } ) ;
90
+
91
+ it ( 'should create a new workspace if none exists' , ( ) => {
92
+ workspaceServiceSpy . getWorkspace . and . returnValue ( of ( [ ] ) ) ;
93
+ workspaceServiceSpy . postWorkspace . and . returnValue ( of ( workspaceResponse [ 0 ] ) ) ;
94
+
95
+ fixture . detectChanges ( ) ;
96
+
97
+ expect ( workspaceServiceSpy . postWorkspace ) . toHaveBeenCalled ( ) ;
98
+ expect ( storageServiceSpy . set ) . toHaveBeenCalledWith ( 'workspaceId' , 1 ) ;
99
+ expect ( storageServiceSpy . set ) . toHaveBeenCalledWith ( 'onboarding-state' , IntacctOnboardingState . CONNECTION ) ;
100
+ expect ( routerSpy . navigateByUrl ) . toHaveBeenCalledWith ( '/integrations/intacct/onboarding/landing' ) ;
101
+ } ) ;
102
+
103
+ it ( 'should navigate to correct route based on onboarding state' , ( ) => {
104
+ Object . entries ( testOnboardingState ) . forEach ( ( [ state , route ] ) => {
105
+ routerSpy . navigateByUrl . calls . reset ( ) ;
106
+ const testWorkspace : IntacctWorkspace = { ...workspaceResponse [ 0 ] , onboarding_state : state as IntacctOnboardingState } ;
107
+ workspaceServiceSpy . getWorkspace . and . returnValue ( of ( [ testWorkspace ] ) ) ;
108
+
109
+ fixture . detectChanges ( ) ;
110
+
111
+ expect ( routerSpy . navigateByUrl ) . toHaveBeenCalledWith ( route ) ;
112
+
113
+ fixture = TestBed . createComponent ( IntacctComponent ) ;
114
+ component = fixture . componentInstance ;
115
+ } ) ;
116
+ } ) ;
117
+
118
+ it ( 'should not navigate if pathname is not /integrations/intacct' , ( ) => {
119
+ component . windowReference . location . pathname = '/some/other/path' ;
120
+ fixture . detectChanges ( ) ;
121
+
122
+ expect ( routerSpy . navigateByUrl ) . not . toHaveBeenCalled ( ) ;
123
+ } ) ;
124
+
125
+ it ( 'should initialise Appcues' , ( ) => {
126
+ ( window as any ) . Appcues = {
127
+ page : jasmine . createSpy ( 'Appcues.page' )
128
+ } ;
129
+
130
+ fixture . detectChanges ( ) ;
131
+
132
+ expect ( appcuesServiceSpy . initialiseAppcues ) . toHaveBeenCalled ( ) ;
133
+ expect ( ( window as any ) . Appcues . page ) . toHaveBeenCalled ( ) ;
134
+ } ) ;
135
+ } ) ;
0 commit comments