1- import { Component , NO_ERRORS_SCHEMA } from '@angular/core' ;
1+ import { ChangeDetectorRef , Component , NO_ERRORS_SCHEMA } from '@angular/core' ;
22import { ComponentFixture , inject , TestBed , waitForAsync } from '@angular/core/testing' ;
33import { CdkTreeModule } from '@angular/cdk/tree' ;
4- import { By } from '@angular/platform-browser' ;
54
65import { of as observableOf } from 'rxjs' ;
6+ import { StoreModule } from '@ngrx/store' ;
77import { TranslateModule } from '@ngx-translate/core' ;
88import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap' ;
9+ import { provideMockStore } from '@ngrx/store/testing' ;
910
1011import { createTestComponent } from '../../testing/utils.test' ;
1112import { VocabularyTreeviewComponent } from './vocabulary-treeview.component' ;
@@ -15,31 +16,29 @@ import { TreeviewFlatNode, TreeviewNode } from './vocabulary-treeview-node.model
1516import { FormFieldMetadataValueObject } from '../builder/models/form-field-metadata-value.model' ;
1617import { VocabularyOptions } from '../../../core/submission/vocabularies/models/vocabulary-options.model' ;
1718import { PageInfo } from '../../../core/shared/page-info.model' ;
18- import { VocabularyService } from '../../../core/submission/vocabularies/vocabulary.service' ;
1919import { VocabularyEntry } from '../../../core/submission/vocabularies/models/vocabulary-entry.model' ;
20+ import { AuthTokenInfo } from '../../../core/auth/models/auth-token-info.model' ;
21+ import { authReducer } from '../../../core/auth/auth.reducer' ;
22+ import { storeModuleConfig } from '../../../app.reducer' ;
23+ import { By } from '@angular/platform-browser' ;
24+ import { VocabularyService } from '../../../core/submission/vocabularies/vocabulary.service' ;
2025
2126describe ( 'VocabularyTreeviewComponent test suite' , ( ) => {
2227
2328 let comp : VocabularyTreeviewComponent ;
2429 let compAsAny : any ;
2530 let fixture : ComponentFixture < VocabularyTreeviewComponent > ;
31+ let initialState ;
2632 let de ;
2733
2834 const item = new VocabularyEntryDetail ( ) ;
2935 item . id = 'node1' ;
3036 const item2 = new VocabularyEntryDetail ( ) ;
3137 item2 . id = 'node2' ;
32- const entryWithAuthority = new VocabularyEntryDetail ( ) ;
33- entryWithAuthority . authority = 'entryWithAuthority' ;
34- entryWithAuthority . id = 'entryWithAuthority' ;
35- entryWithAuthority . value = 'test' ;
36- const entryWithoutAuthority = new VocabularyEntryDetail ( ) ;
37- entryWithoutAuthority . id = 'entryWithoutAuthority' ;
38- entryWithoutAuthority . value = 'test2' ;
3938 const emptyNodeMap = new Map < string , TreeviewFlatNode > ( ) ;
4039 const storedNodeMap = new Map < string , TreeviewFlatNode > ( ) . set ( 'test' , new TreeviewFlatNode ( item2 ) ) ;
4140 const nodeMap = new Map < string , TreeviewFlatNode > ( ) . set ( 'test' , new TreeviewFlatNode ( item ) ) ;
42- const vocabularyOptions = new VocabularyOptions ( 'vocabularyTest' , null , null , false ) ;
41+ const vocabularyOptions = new VocabularyOptions ( 'vocabularyTest' , ' false' ) ;
4342 const modalStub = jasmine . createSpyObj ( 'modalStub' , [ 'close' ] ) ;
4443 const vocabularyTreeviewServiceStub = jasmine . createSpyObj ( 'VocabularyTreeviewService' , {
4544 initialize : jasmine . createSpy ( 'initialize' ) ,
@@ -60,10 +59,25 @@ describe('VocabularyTreeviewComponent test suite', () => {
6059 clearSearchTopRequests : jasmine . createSpy ( 'clearSearchTopRequests' )
6160 } ) ;
6261
62+ initialState = {
63+ core : {
64+ auth : {
65+ authenticated : true ,
66+ loaded : true ,
67+ blocking : false ,
68+ loading : false ,
69+ authToken : new AuthTokenInfo ( 'test_token' ) ,
70+ userId : 'testid' ,
71+ authMethods : [ ]
72+ }
73+ }
74+ } ;
75+
6376 beforeEach ( waitForAsync ( ( ) => {
6477 TestBed . configureTestingModule ( {
6578 imports : [
6679 CdkTreeModule ,
80+ StoreModule . forRoot ( { auth : authReducer } , storeModuleConfig ) ,
6781 TranslateModule . forRoot ( )
6882 ] ,
6983 declarations : [
@@ -74,6 +88,8 @@ describe('VocabularyTreeviewComponent test suite', () => {
7488 { provide : VocabularyTreeviewService , useValue : vocabularyTreeviewServiceStub } ,
7589 { provide : VocabularyService , useValue : vocabularyServiceStub } ,
7690 { provide : NgbActiveModal , useValue : modalStub } ,
91+ provideMockStore ( { initialState } ) ,
92+ ChangeDetectorRef ,
7793 VocabularyTreeviewComponent
7894 ] ,
7995 schemas : [ NO_ERRORS_SCHEMA ]
@@ -128,10 +144,10 @@ describe('VocabularyTreeviewComponent test suite', () => {
128144 currentValue . otherInformation = {
129145 id : 'entryID'
130146 } ;
131- comp . selectedItems = [ currentValue ] ;
147+ comp . selectedItems = [ currentValue . value ] ;
132148 fixture . detectChanges ( ) ;
133149 expect ( comp . dataSource . data ) . toEqual ( [ ] ) ;
134- expect ( vocabularyTreeviewServiceStub . initialize ) . toHaveBeenCalledWith ( comp . vocabularyOptions , new PageInfo ( ) , [ 'entryID ' ] , 'entryID' ) ;
150+ expect ( vocabularyTreeviewServiceStub . initialize ) . toHaveBeenCalledWith ( comp . vocabularyOptions , new PageInfo ( ) , [ 'testValue ' ] , null ) ;
135151 } ) ;
136152
137153 it ( 'should should init component properly with init value as VocabularyEntry' , ( ) => {
@@ -140,20 +156,10 @@ describe('VocabularyTreeviewComponent test suite', () => {
140156 currentValue . otherInformation = {
141157 id : 'entryID'
142158 } ;
143- comp . selectedItems = [ currentValue ] ;
144- fixture . detectChanges ( ) ;
145- expect ( comp . dataSource . data ) . toEqual ( [ ] ) ;
146- expect ( vocabularyTreeviewServiceStub . initialize ) . toHaveBeenCalledWith ( comp . vocabularyOptions , new PageInfo ( ) , [ 'entryID' ] , 'entryID' ) ;
147- } ) ;
148-
149- it ( 'should should init component properly with init value as VocabularyEntryDetail' , ( ) => {
150- const currentValue = new VocabularyEntryDetail ( ) ;
151- currentValue . value = 'testValue' ;
152- currentValue . id = 'entryID' ;
153- comp . selectedItems = [ currentValue ] ;
159+ comp . selectedItems = [ currentValue . value ] ;
154160 fixture . detectChanges ( ) ;
155161 expect ( comp . dataSource . data ) . toEqual ( [ ] ) ;
156- expect ( vocabularyTreeviewServiceStub . initialize ) . toHaveBeenCalledWith ( comp . vocabularyOptions , new PageInfo ( ) , [ 'entryID ' ] , 'entryID' ) ;
162+ expect ( vocabularyTreeviewServiceStub . initialize ) . toHaveBeenCalledWith ( comp . vocabularyOptions , new PageInfo ( ) , [ 'testValue ' ] , null ) ;
157163 } ) ;
158164
159165 it ( 'should call loadMore function' , ( ) => {
@@ -176,31 +182,11 @@ describe('VocabularyTreeviewComponent test suite', () => {
176182 expect ( vocabularyTreeviewServiceStub . loadMore ) . toHaveBeenCalledWith ( node . item , [ ] , true ) ;
177183 } ) ;
178184
179- it ( 'should emit proper FormFieldMetadataValueObject when VocabularyEntryDetail has authority' , ( ) => {
180- spyOn ( compAsAny , 'getSelectedEntryIds' ) . and . returnValue ( [ ] ) ;
181- spyOn ( comp . select , 'emit' ) ;
182- comp . onSelect ( entryWithAuthority ) ;
183-
184- const expected = new FormFieldMetadataValueObject ( entryWithAuthority . value , null , null , entryWithAuthority . authority ) ;
185- expect ( comp . select . emit ) . toHaveBeenCalledWith ( expected ) ;
186- } ) ;
187-
188- it ( 'should emit proper FormFieldMetadataValueObject when VocabularyEntryDetail has no authority' , ( ) => {
189- spyOn ( compAsAny , 'getSelectedEntryIds' ) . and . returnValue ( [ ] ) ;
190- spyOn ( comp . select , 'emit' ) ;
191- comp . onSelect ( entryWithoutAuthority ) ;
192-
193- const expected = new FormFieldMetadataValueObject ( entryWithoutAuthority . value ) ;
194- expect ( comp . select . emit ) . toHaveBeenCalledWith ( expected ) ;
195- } ) ;
196-
197- it ( 'should emit deselect when entry is already present' , ( ) => {
198- spyOn ( compAsAny , 'getSelectedEntryIds' ) . and . returnValue ( [ entryWithAuthority . id ] ) ;
199- spyOn ( comp . select , 'emit' ) ;
200- spyOn ( comp . deselect , 'emit' ) ;
201- comp . onSelect ( entryWithAuthority ) ;
185+ it ( 'should emit select event' , ( ) => {
186+ spyOn ( comp , 'onSelect' ) ;
187+ comp . onSelect ( item ) ;
202188
203- expect ( comp . deselect . emit ) . toHaveBeenCalled ( ) ;
189+ expect ( comp . onSelect ) . toHaveBeenCalledWith ( item ) ;
204190 } ) ;
205191
206192 it ( 'should call searchByQuery function and set storedNodeMap properly' , ( ) => {
@@ -302,7 +288,7 @@ describe('VocabularyTreeviewComponent test suite', () => {
302288} )
303289class TestComponent {
304290
305- vocabularyOptions : VocabularyOptions = new VocabularyOptions ( 'vocabularyTest' , null , null , false ) ;
291+ vocabularyOptions : VocabularyOptions = new VocabularyOptions ( 'vocabularyTest' , ' false' ) ;
306292 preloadLevel = 2 ;
307293
308294}
0 commit comments