@@ -15,6 +15,11 @@ import { TranslateModule } from '@ngx-translate/core';
15
15
import { By } from '@angular/platform-browser' ;
16
16
import { SearchResult } from '../../shared/search/models/search-result.model' ;
17
17
import { 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' ;
18
23
19
24
describe ( 'SearchComponent' , ( ) => {
20
25
let fixture : ComponentFixture < LuckySearchComponent > ;
@@ -39,6 +44,9 @@ describe('SearchComponent', () => {
39
44
}
40
45
] ) )
41
46
} ) ;
47
+ const bitstreamDataService = jasmine . createSpyObj ( 'bitstreamDataService' , {
48
+ findByItem : jasmine . createSpy ( 'findByItem' )
49
+ } ) ;
42
50
const mockSearchOptions = observableOf ( new PaginatedSearchOptions ( {
43
51
pagination : Object . assign ( new PaginationComponentOptions ( ) , {
44
52
id : 'search-page-configuration' ,
@@ -52,27 +60,21 @@ describe('SearchComponent', () => {
52
60
} ;
53
61
let component : LuckySearchComponent ;
54
62
55
- const itemPageUrl = '/lucky-search?index=xxx&value=yyyy' ;
56
63
const urlTree = new UrlTree ( ) ;
57
64
urlTree . queryParams = {
58
65
index : 'test' ,
59
66
'value' : 'test'
60
67
} ;
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 ( ) ;
67
69
beforeEach ( async ( ) => {
68
70
await TestBed . configureTestingModule ( {
69
- declarations : [ LuckySearchComponent ] ,
71
+ declarations : [ LuckySearchComponent , FileSizePipe ] ,
70
72
imports : [ TranslateModule . forRoot ( ) ] ,
71
73
providers : [
72
74
{ provide : Router , useValue : routerStub } ,
73
75
{ provide : SearchConfigurationService , useValue : searchConfigServiceStub } ,
74
76
{ provide : LuckySearchService , useValue : searchServiceStub } ,
75
-
77
+ { provide : BitstreamDataService , useValue : bitstreamDataService }
76
78
] ,
77
79
} )
78
80
. compileComponents ( ) ;
@@ -84,61 +86,139 @@ describe('SearchComponent', () => {
84
86
fixture . detectChanges ( ) ;
85
87
} ) ;
86
88
87
- it ( 'should create' , ( ) => {
88
- expect ( component ) . toBeTruthy ( ) ;
89
- } ) ;
89
+ describe ( 'should search items' , ( ) => {
90
90
91
- it ( 'should show multiple results' , ( ) => {
92
- expect ( component . showMultipleSearchSection ) . toEqual ( true ) ;
93
- } ) ;
91
+ beforeEach ( ( ) => {
92
+ spyOn ( routerStub , 'parseUrl' ) . and . returnValue ( urlTree ) ;
93
+ } ) ;
94
94
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
+ } ) ;
99
98
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 ) ;
114
101
} ) ;
115
102
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
+ } ) ;
122
107
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
+ } ) ;
126
130
131
+ it ( 'should call navigate or router' , ( ) => {
132
+ expect ( routerStub . navigateByUrl ) . toHaveBeenCalled ( ) ;
133
+ } ) ;
127
134
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
+ } ) ;
135
142
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
+ } ) ;
138
151
} ) ;
139
152
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
+ } ) ;
143
223
} ) ;
144
224
} ) ;
0 commit comments