@@ -5,45 +5,107 @@ import { CommonModule } from '@angular/common';
55import { FormsModule , ReactiveFormsModule } from '@angular/forms' ;
66import { TranslateModule } from '@ngx-translate/core' ;
77import { ActivatedRoute , Params } from '@angular/router' ;
8- import { createSuccessfulRemoteDataObject$ } from '../shared/remote-data.utils' ;
8+ import { Directive , Input , NO_ERRORS_SCHEMA } from '@angular/core' ;
9+ import { createFailedRemoteDataObject$ , createSuccessfulRemoteDataObject$ } from '../shared/remote-data.utils' ;
910import { CollectionDataService } from '../core/data/collection-data.service' ;
1011import { Collection } from '../core/shared/collection.model' ;
11- import { mockLicenseRD$ } from '../shared/testing/clarin-license-mock' ;
12- import { take } from 'rxjs/operators' ;
13- import { getFirstCompletedRemoteData } from '../core/shared/operators' ;
12+ import { License } from '../core/shared/license.model' ;
13+ import { PaginationService } from '../core/pagination/pagination.service' ;
14+ import { buildPaginatedList } from '../core/data/paginated-list.model' ;
15+ import { PageInfo } from '../core/shared/page-info.model' ;
16+ import { of as observableOf } from 'rxjs' ;
17+ import { FindListOptions } from '../core/data/find-list-options.model' ;
18+ import { PaginationComponentOptions } from '../shared/pagination/pagination-component-options.model' ;
19+ import { SortDirection , SortOptions } from '../core/cache/models/sort-options.model' ;
20+
21+ /* eslint-disable @angular-eslint/directive-selector */
22+ @Directive ( {
23+ selector : '[ngVar]'
24+ } )
25+ class MockNgVarDirective {
26+ @Input ( ) ngVar : unknown ;
27+ }
1428
1529describe ( 'LicenseContractPageComponent' , ( ) => {
1630 let component : LicenseContractPageComponent ;
1731 let fixture : ComponentFixture < LicenseContractPageComponent > ;
1832
19- let collection : Collection ;
20-
2133 let routeStub : any ;
22- let collectionService : CollectionDataService ;
34+ let collectionService : jasmine . SpyObj < CollectionDataService > ;
35+ let paginationService : jasmine . SpyObj < PaginationService > ;
2336
2437 const paramCollectionId = 'collectionId' ;
2538 const paramCollectionIdValue = '1' ;
2639
2740 const paramObject : Params = { } ;
2841 paramObject [ paramCollectionId ] = paramCollectionIdValue ;
2942
30- collection = Object . assign ( new Collection ( ) , {
43+ const singleCollectionLicense = Object . assign ( new License ( ) , {
44+ text : 'Single collection license text'
45+ } ) ;
46+
47+ const collection = Object . assign ( new Collection ( ) , {
3148 uuid : 'fake-collection-id' ,
49+ name : 'Single collection' ,
3250 _links : {
3351 self : { href : 'collection-selflink' } ,
3452 license : { href : 'license-link' }
3553 } ,
36- license : mockLicenseRD$
54+ license : createSuccessfulRemoteDataObject$ ( singleCollectionLicense )
3755 } ) ;
3856
57+ const secondCollectionLicense = Object . assign ( new License ( ) , {
58+ text : 'Second collection license text'
59+ } ) ;
60+
61+ const authorizedCollections = [
62+ collection ,
63+ Object . assign ( new Collection ( ) , {
64+ uuid : 'second-collection-id' ,
65+ name : 'Second collection' ,
66+ _links : {
67+ self : { href : 'second-collection-selflink' } ,
68+ license : { href : 'second-license-link' }
69+ } ,
70+ license : createSuccessfulRemoteDataObject$ ( secondCollectionLicense )
71+ } )
72+ ] ;
73+
74+ const authorizedCollectionsRD$ = createSuccessfulRemoteDataObject$ (
75+ buildPaginatedList (
76+ new PageInfo ( {
77+ currentPage : 1 ,
78+ elementsPerPage : 10 ,
79+ totalElements : authorizedCollections . length ,
80+ totalPages : 1
81+ } ) ,
82+ authorizedCollections
83+ )
84+ ) ;
85+
3986 routeStub = {
4087 snapshot : {
41- queryParams : paramObject ,
88+ queryParams : { ... paramObject } ,
4289 }
4390 } ;
4491
45- collectionService = jasmine . createSpyObj ( 'collectionService' , {
46- findById : createSuccessfulRemoteDataObject$ ( collection )
92+ collectionService = jasmine . createSpyObj < CollectionDataService > ( 'collectionService' , {
93+ findById : createSuccessfulRemoteDataObject$ ( collection ) ,
94+ getAuthorizedCollection : authorizedCollectionsRD$
95+ } ) ;
96+
97+ paginationService = jasmine . createSpyObj < PaginationService > ( 'paginationService' , {
98+ getFindListOptions : observableOf ( Object . assign ( new FindListOptions ( ) , {
99+ currentPage : 1 ,
100+ elementsPerPage : 10
101+ } ) ) ,
102+ getCurrentPagination : observableOf ( Object . assign ( new PaginationComponentOptions ( ) , {
103+ currentPage : 1 ,
104+ pageSize : 10 ,
105+ pageSizeOptions : [ 1 , 5 , 10 , 20 , 40 , 60 , 80 , 100 ] ,
106+ } ) ) ,
107+ getCurrentSort : observableOf ( new SortOptions ( 'name' , SortDirection . ASC ) ) ,
108+ clearPagination : undefined
47109 } ) ;
48110
49111 beforeEach ( async ( ) => {
@@ -53,20 +115,29 @@ describe('LicenseContractPageComponent', () => {
53115 CommonModule ,
54116 FormsModule ,
55117 ReactiveFormsModule ,
56- TranslateModule . forRoot ( )
118+ TranslateModule . forRoot ( ) ,
57119 ] ,
58120 declarations : [
59- LicenseContractPageComponent
121+ LicenseContractPageComponent ,
122+ MockNgVarDirective
60123 ] ,
61124 providers : [
62125 { provide : ActivatedRoute , useValue : routeStub } ,
63126 { provide : CollectionDataService , useValue : collectionService } ,
64- ]
127+ { provide : PaginationService , useValue : paginationService } ,
128+ ] ,
129+ schemas : [ NO_ERRORS_SCHEMA ]
65130 } )
66131 . compileComponents ( ) ;
67132 } ) ;
68133
69134 beforeEach ( ( ) => {
135+ routeStub . snapshot . queryParams = { ...paramObject } ;
136+ collectionService . findById . and . returnValue ( createSuccessfulRemoteDataObject$ ( collection ) ) ;
137+ collectionService . findById . calls . reset ( ) ;
138+ collectionService . getAuthorizedCollection . calls . reset ( ) ;
139+ paginationService . getFindListOptions . calls . reset ( ) ;
140+ paginationService . clearPagination . calls . reset ( ) ;
70141 fixture = TestBed . createComponent ( LicenseContractPageComponent ) ;
71142 component = fixture . componentInstance ;
72143 fixture . detectChanges ( ) ;
@@ -77,19 +148,52 @@ describe('LicenseContractPageComponent', () => {
77148 } ) ;
78149
79150 it ( 'should load collectionRD$' , ( ) => {
80- collectionService . findById ( collection . uuid )
81- . pipe ( getFirstCompletedRemoteData ( ) )
82- . subscribe ( collectionRD$ => {
83- expect ( component . collectionRD$ . value ) . toEqual ( collectionRD$ ) ;
84- } ) ;
151+ expect ( component . collectionRD$ . value . payload ) . toEqual ( collection ) ;
85152 } ) ;
86153
87154 it ( 'should load licenseRD$' , ( ) => {
88- collection . license
89- . pipe ( take ( 1 ) )
90- . subscribe ( licenseRD$ => {
91- expect ( component . licenseRD$ . value ) . toEqual ( licenseRD$ ) ;
92- } ) ;
155+ expect ( component . licenseRD$ . value . payload ) . toEqual ( singleCollectionLicense ) ;
156+ } ) ;
157+
158+ it ( 'should set hasFailed on collectionRD$ when collectionId is bogus' , ( ) => {
159+ collectionService . findById . and . returnValue ( createFailedRemoteDataObject$ ( 'Not Found' , 404 ) ) ;
160+ collectionService . findById . calls . reset ( ) ;
161+
162+ const failFixture = TestBed . createComponent ( LicenseContractPageComponent ) ;
163+ const failComponent = failFixture . componentInstance ;
164+ failFixture . detectChanges ( ) ;
165+
166+ expect ( collectionService . findById ) . toHaveBeenCalled ( ) ;
167+ expect ( failComponent . collectionRD$ . value . hasFailed ) . toBeTrue ( ) ;
168+ } ) ;
169+
170+ it ( 'should load authorized collections when collectionId is missing' , ( ) => {
171+ routeStub . snapshot . queryParams = { } ;
172+ collectionService . findById . calls . reset ( ) ;
173+ collectionService . getAuthorizedCollection . calls . reset ( ) ;
174+
175+ const listFixture = TestBed . createComponent ( LicenseContractPageComponent ) ;
176+ const listComponent = listFixture . componentInstance ;
177+ listFixture . detectChanges ( ) ;
178+
179+ expect ( collectionService . findById ) . not . toHaveBeenCalled ( ) ;
180+ expect ( collectionService . getAuthorizedCollection ) . toHaveBeenCalled ( ) ;
181+
182+ listComponent . collectionsRD$ . subscribe ( ( collectionsRD ) => {
183+ expect ( collectionsRD . payload . page ) . toEqual ( authorizedCollections ) ;
184+ } ) ;
185+ } ) ;
186+
187+ it ( 'should clear pagination state on destroy in list mode' , ( ) => {
188+ routeStub . snapshot . queryParams = { } ;
189+ paginationService . clearPagination . calls . reset ( ) ;
190+
191+ const listFixture = TestBed . createComponent ( LicenseContractPageComponent ) ;
192+ const listComponent = listFixture . componentInstance ;
193+ listFixture . detectChanges ( ) ;
194+ listComponent . ngOnDestroy ( ) ;
195+
196+ expect ( paginationService . clearPagination ) . toHaveBeenCalledWith ( listComponent . paginationId ) ;
93197 } ) ;
94198
95199} ) ;
0 commit comments