@@ -15,6 +15,11 @@ import { TranslateModule } from '@ngx-translate/core';
1515import { By } from '@angular/platform-browser' ;
1616import { SearchResult } from '../../shared/search/models/search-result.model' ;
1717import { DSpaceObject } from '../../core/shared/dspace-object.model' ;
18+ import { BitstreamDataService , MetadataFilter } from '../../core/data/bitstream-data.service' ;
19+ import { Bitstream } from '../../core/shared/bitstream.model' ;
20+ import { RouterMock } from '../../shared/mocks/router.mock' ;
21+ import { MetadataMap , MetadataValue } from '../../core/shared/metadata.models' ;
22+ import { FileSizePipe } from '../../shared/utils/file-size-pipe' ;
1823
1924describe ( 'SearchComponent' , ( ) => {
2025 let fixture : ComponentFixture < LuckySearchComponent > ;
@@ -39,6 +44,9 @@ describe('SearchComponent', () => {
3944 }
4045 ] ) )
4146 } ) ;
47+ const bitstreamDataService = jasmine . createSpyObj ( 'bitstreamDataService' , {
48+ findByItem : jasmine . createSpy ( 'findByItem' )
49+ } ) ;
4250 const mockSearchOptions = observableOf ( new PaginatedSearchOptions ( {
4351 pagination : Object . assign ( new PaginationComponentOptions ( ) , {
4452 id : 'search-page-configuration' ,
@@ -52,27 +60,21 @@ describe('SearchComponent', () => {
5260 } ;
5361 let component : LuckySearchComponent ;
5462
55- const itemPageUrl = '/lucky-search?index=xxx&value=yyyy' ;
5663 const urlTree = new UrlTree ( ) ;
5764 urlTree . queryParams = {
5865 index : 'test' ,
5966 'value' : 'test'
6067 } ;
61- const routerStub = jasmine . createSpyObj ( 'router' , {
62- parseUrl : urlTree ,
63- createUrlTree : new UrlTree ( ) ,
64- url : itemPageUrl ,
65- navigateByUrl : void { }
66- } ) ;
68+ const routerStub = new RouterMock ( ) ;
6769 beforeEach ( async ( ) => {
6870 await TestBed . configureTestingModule ( {
69- declarations : [ LuckySearchComponent ] ,
71+ declarations : [ LuckySearchComponent , FileSizePipe ] ,
7072 imports : [ TranslateModule . forRoot ( ) ] ,
7173 providers : [
7274 { provide : Router , useValue : routerStub } ,
7375 { provide : SearchConfigurationService , useValue : searchConfigServiceStub } ,
7476 { provide : LuckySearchService , useValue : searchServiceStub } ,
75-
77+ { provide : BitstreamDataService , useValue : bitstreamDataService }
7678 ] ,
7779 } )
7880 . compileComponents ( ) ;
@@ -84,61 +86,139 @@ describe('SearchComponent', () => {
8486 fixture . detectChanges ( ) ;
8587 } ) ;
8688
87- it ( 'should create' , ( ) => {
88- expect ( component ) . toBeTruthy ( ) ;
89- } ) ;
89+ describe ( 'should search items' , ( ) => {
9090
91- it ( 'should show multiple results' , ( ) => {
92- expect ( component . showMultipleSearchSection ) . toEqual ( true ) ;
93- } ) ;
91+ beforeEach ( ( ) => {
92+ spyOn ( routerStub , 'parseUrl' ) . and . returnValue ( urlTree ) ;
93+ } ) ;
9494
95- it ( 'should display basic search form results' , ( ) => {
96- expect ( fixture . debugElement . query ( By . css ( 'ds-search-results' ) ) )
97- . toBeTruthy ( ) ;
98- } ) ;
95+ it ( 'should create' , ( ) => {
96+ expect ( component ) . toBeTruthy ( ) ;
97+ } ) ;
9998
100- beforeEach ( ( ) => {
101- fixture = TestBed . createComponent ( LuckySearchComponent ) ;
102- component = fixture . componentInstance ;
103- const firstSearchResult = Object . assign ( new SearchResult ( ) , {
104- indexableObject : Object . assign ( new DSpaceObject ( ) , {
105- id : 'd317835d-7b06-4219-91e2-1191900cb897' ,
106- uuid : 'd317835d-7b06-4219-91e2-1191900cb897' ,
107- name : 'My first publication' ,
108- metadata : {
109- 'dspace.entity.type' : [
110- { value : 'Publication' }
111- ]
112- }
113- } )
99+ it ( 'should show multiple results' , ( ) => {
100+ expect ( component . showMultipleSearchSection ) . toEqual ( true ) ;
114101 } ) ;
115102
116- const data = createSuccessfulRemoteDataObject ( createPaginatedList ( [
117- firstSearchResult
118- ] ) ) ;
119- component . resultsRD$ . next ( data as any ) ;
120- fixture . detectChanges ( ) ;
121- } ) ;
103+ it ( 'should display basic search form results' , ( ) => {
104+ expect ( fixture . debugElement . query ( By . css ( 'ds-search-results' ) ) )
105+ . toBeTruthy ( ) ;
106+ } ) ;
122107
123- it ( 'should call navigate or router' , ( ) => {
124- expect ( routerStub . navigateByUrl ) . toHaveBeenCalled ( ) ;
125- } ) ;
108+ beforeEach ( ( ) => {
109+ fixture = TestBed . createComponent ( LuckySearchComponent ) ;
110+ component = fixture . componentInstance ;
111+ const firstSearchResult = Object . assign ( new SearchResult ( ) , {
112+ indexableObject : Object . assign ( new DSpaceObject ( ) , {
113+ id : 'd317835d-7b06-4219-91e2-1191900cb897' ,
114+ uuid : 'd317835d-7b06-4219-91e2-1191900cb897' ,
115+ name : 'My first publication' ,
116+ metadata : {
117+ 'dspace.entity.type' : [
118+ { value : 'Publication' }
119+ ]
120+ }
121+ } )
122+ } ) ;
123+
124+ const data = createSuccessfulRemoteDataObject ( createPaginatedList ( [
125+ firstSearchResult
126+ ] ) ) ;
127+ component . resultsRD$ . next ( data as any ) ;
128+ fixture . detectChanges ( ) ;
129+ } ) ;
126130
131+ it ( 'should call navigate or router' , ( ) => {
132+ expect ( routerStub . navigateByUrl ) . toHaveBeenCalled ( ) ;
133+ } ) ;
127134
128- beforeEach ( ( ) => {
129- fixture = TestBed . createComponent ( LuckySearchComponent ) ;
130- component = fixture . componentInstance ;
131- const data = createSuccessfulRemoteDataObject ( createPaginatedList ( [ ] ) ) ;
132- component . resultsRD$ . next ( data as any ) ;
133- fixture . detectChanges ( ) ;
134- } ) ;
135+ beforeEach ( ( ) => {
136+ fixture = TestBed . createComponent ( LuckySearchComponent ) ;
137+ component = fixture . componentInstance ;
138+ const data = createSuccessfulRemoteDataObject ( createPaginatedList ( [ ] ) ) ;
139+ component . resultsRD$ . next ( data as any ) ;
140+ fixture . detectChanges ( ) ;
141+ } ) ;
135142
136- it ( 'should not have results' , ( ) => {
137- expect ( component . showEmptySearchSection ) . toEqual ( true ) ;
143+ it ( 'should not have results' , ( ) => {
144+ expect ( component . showEmptySearchSection ) . toEqual ( true ) ;
145+ } ) ;
146+
147+ it ( 'should display basic search form' , ( ) => {
148+ expect ( fixture . debugElement . query ( By . css ( 'ds-search-form' ) ) )
149+ . toBeTruthy ( ) ;
150+ } ) ;
138151 } ) ;
139152
140- it ( 'should display basic search form' , ( ) => {
141- expect ( fixture . debugElement . query ( By . css ( 'ds-search-form' ) ) )
142- . toBeTruthy ( ) ;
153+ describe ( 'should search bitstreams' , ( ) => {
154+
155+ const bitstreamMetadata = {
156+ 'dc.title' : [ { value : 'test.pdf' } as MetadataValue ] ,
157+ 'dc.description' : [ { value : 'TestDescription' } as MetadataValue ]
158+ } as MetadataMap ;
159+ const bitstream = Object . assign (
160+ new Bitstream ( ) ,
161+ { _name : 'test.pdf' , sizeBytes : 15 , uuid : 'fa272dbf-e458-4ad2-868b-b4a27c6eac15' , metadata : bitstreamMetadata }
162+ ) as Bitstream ;
163+
164+ beforeEach ( ( ) => {
165+ fixture = TestBed . createComponent ( LuckySearchComponent ) ;
166+ component = fixture . componentInstance ;
167+
168+ const bitstreamSearchTree = new UrlTree ( ) ;
169+ bitstreamSearchTree . queryParams = {
170+ index : 'testIndex' ,
171+ value : 'testValue' ,
172+ bitstreamMetadata : 'testMetadata' ,
173+ bitstreamValue : 'testMetadataValue'
174+ } ;
175+
176+ const itemUUID = 'd317835d-7b06-4219-91e2-1191900cb897' ;
177+ const firstSearchResult = Object . assign ( new SearchResult ( ) , {
178+ indexableObject : Object . assign ( new DSpaceObject ( ) , {
179+ id : 'd317835d-7b06-4219-91e2-1191900cb897' ,
180+ uuid : itemUUID ,
181+ name : 'My first publication' ,
182+ metadata : {
183+ 'dspace.entity.type' : [
184+ { value : 'Publication' }
185+ ]
186+ }
187+ } )
188+ } ) ;
189+ const data = createSuccessfulRemoteDataObject ( createPaginatedList ( [ firstSearchResult ] ) ) ;
190+ const metadataFilters = [ { metadataName : 'dc.title' , metadataValue : 'test.pdf' } ] as MetadataFilter [ ] ;
191+ component . bitstreamFilters$ . next ( metadataFilters ) ;
192+ bitstreamDataService . findByItem . withArgs ( itemUUID , 'ORIGINAL' , metadataFilters , { } )
193+ . and . returnValue ( createSuccessfulRemoteDataObject$ ( createPaginatedList ( [ bitstream ] ) ) ) ;
194+
195+ spyOn ( component , 'redirect' ) ;
196+ spyOn ( component . bitstreams$ , 'next' ) . and . callThrough ( ) ;
197+ spyOn ( routerStub , 'parseUrl' ) . and . returnValue ( bitstreamSearchTree ) ;
198+
199+ component . resultsRD$ . next ( data as any ) ;
200+
201+ fixture . detectChanges ( ) ;
202+ } ) ;
203+
204+ it ( 'should load item bitstreams' , ( ) => {
205+ expect ( component . bitstreams$ . next ) . toHaveBeenCalledWith ( [ bitstream ] ) ;
206+ } ) ;
207+
208+ it ( 'should redirect to bitstream' , ( ) => {
209+ expect ( component . redirect ) . toHaveBeenCalledWith ( `/bitstreams/${ bitstream . uuid } /download` ) ;
210+ } ) ;
211+
212+ it ( 'should return bitstream filename' , ( ) => {
213+ expect ( component . fileName ( bitstream ) ) . toEqual ( 'test.pdf' ) ;
214+ } ) ;
215+
216+ it ( 'should return bitstream description' , ( ) => {
217+ expect ( component . getDescription ( bitstream ) ) . toEqual ( 'TestDescription' ) ;
218+ } ) ;
219+
220+ it ( 'should return bitstream file size' , ( ) => {
221+ expect ( component . getSize ( bitstream ) ) . toEqual ( 15 ) ;
222+ } ) ;
143223 } ) ;
144224} ) ;
0 commit comments