@@ -5,11 +5,15 @@ import {
55import { TestScheduler } from 'rxjs/testing' ;
66
77import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service' ;
8+ import { RequestParam } from '../cache/models/request-param.model' ;
89import { ObjectCacheService } from '../cache/object-cache.service' ;
910import { HALEndpointService } from '../shared/hal-endpoint.service' ;
1011import { Site } from '../shared/site.model' ;
1112import { createPaginatedList } from '../testing/utils.test' ;
12- import { createSuccessfulRemoteDataObject } from '../utilities/remote-data.utils' ;
13+ import {
14+ createFailedRemoteDataObject ,
15+ createSuccessfulRemoteDataObject ,
16+ } from '../utilities/remote-data.utils' ;
1317import { testFindAllDataImplementation } from './base/find-all-data.spec' ;
1418import { DefaultChangeAnalyzer } from './default-change-analyzer.service' ;
1519import { FindListOptions } from './find-list-options.model' ;
@@ -78,14 +82,45 @@ describe('SiteDataService', () => {
7882 } ) ;
7983
8084 describe ( 'find' , ( ) => {
81- it ( 'should return the Site object' , ( ) => {
82-
83- spyOn ( service , 'findAll' ) . and . returnValue ( cold ( 'a' , {
85+ it ( 'should call findAll with allLanguages projection searchParam and return the first Site object' , ( ) => {
86+ const findAllSpy = spyOn ( service , 'findAll' ) . and . returnValue ( cold ( 'a' , {
8487 a : createSuccessfulRemoteDataObject ( createPaginatedList ( [ testObject ] ) ) ,
8588 } ) ) ;
8689
90+ const result = service . find ( options ) ;
8791 const expected = cold ( '(b|)' , { b : testObject } ) ;
88- const result = service . find ( ) ;
92+
93+ expect ( result ) . toBeObservable ( expected ) ;
94+
95+ const calledOptions : FindListOptions = findAllSpy . calls . mostRecent ( ) . args [ 0 ] ;
96+ const projectionParam = calledOptions . searchParams ?. find ( ( p : RequestParam ) => p . fieldName === 'projection' ) ;
97+ expect ( projectionParam ) . toBeDefined ( ) ;
98+ expect ( projectionParam . fieldValue ) . toBe ( 'allLanguages' ) ;
99+ } ) ;
100+
101+ it ( 'should fall back to findAll without searchParams when first call fails, and return the Site object' , ( ) => {
102+ const findAllSpy = spyOn ( service , 'findAll' ) . and . returnValues (
103+ cold ( 'a' , { a : createFailedRemoteDataObject < any > ( 'Error' , 500 ) } ) ,
104+ cold ( 'b' , { b : createSuccessfulRemoteDataObject ( createPaginatedList ( [ testObject ] ) ) } ) ,
105+ ) ;
106+
107+ const result = service . find ( options ) ;
108+ const expected = cold ( '(b|)' , { b : testObject } ) ;
109+
110+ expect ( result ) . toBeObservable ( expected ) ;
111+
112+ // Second call should use original options (without injected searchParams)
113+ const fallbackOptions : FindListOptions = findAllSpy . calls . argsFor ( 1 ) [ 0 ] ;
114+ expect ( fallbackOptions ) . toEqual ( options ) ;
115+ } ) ;
116+
117+ it ( 'should return null when both findAll calls fail' , ( ) => {
118+ spyOn ( service , 'findAll' ) . and . returnValue (
119+ cold ( 'a' , { a : createFailedRemoteDataObject < any > ( 'Error' , 500 ) } ) ,
120+ ) ;
121+
122+ const result = service . find ( options ) ;
123+ const expected = cold ( '(b|)' , { b : null } ) ;
89124
90125 expect ( result ) . toBeObservable ( expected ) ;
91126 } ) ;
0 commit comments