@@ -119,7 +119,10 @@ describe('GuestbooksRepository', () => {
119119 createdGuestbookId = await sut . createGuestbook ( testCollectionId , createGuestbookDTO )
120120 const actual = await sut . getGuestbooksByCollectionId ( testCollectionId )
121121 expect ( actual . length ) . toBeGreaterThan ( 0 )
122- expect ( actual . some ( ( guestbook ) => guestbook . id === createdGuestbookId ) ) . toBe ( true )
122+ const createdGuestbook = actual . find ( ( guestbook ) => guestbook . id === createdGuestbookId )
123+ expect ( createdGuestbook ) . toBeDefined ( )
124+ expect ( createdGuestbook ?. usageCount ) . toBeUndefined ( )
125+ expect ( createdGuestbook ?. responseCount ) . toBeUndefined ( )
123126 } )
124127
125128 test ( 'should list guestbooks for collection by collection alias' , async ( ) => {
@@ -220,6 +223,132 @@ describe('GuestbooksRepository', () => {
220223 }
221224 } )
222225
226+ test ( 'should include hierarchical owner guestbooks when includeInherited is true' , async ( ) => {
227+ const uniqueSuffix = Date . now ( ) . toString ( )
228+ const childCollectionAlias = `testGuestbooksInheritedChild${ uniqueSuffix } `
229+ const parentGuestbookName = `parent inherited guestbook ${ uniqueSuffix } `
230+ const childGuestbookName = `child inherited guestbook ${ uniqueSuffix } `
231+
232+ let childCollectionId : number | undefined
233+
234+ try {
235+ await createCollectionViaApi ( childCollectionAlias , testCollectionAlias ) . then (
236+ ( collectionPayload : CollectionPayload ) => ( childCollectionId = collectionPayload . id )
237+ )
238+ await publishCollectionViaApi ( childCollectionAlias )
239+
240+ const parentGuestbookId = await sut . createGuestbook ( testCollectionAlias , {
241+ ...createGuestbookDTO ,
242+ name : parentGuestbookName ,
243+ customQuestions : [ ]
244+ } )
245+ const childGuestbookId = await sut . createGuestbook ( childCollectionAlias , {
246+ ...createGuestbookDTO ,
247+ name : childGuestbookName ,
248+ customQuestions : [ ]
249+ } )
250+
251+ const withoutInherited = await sut . getGuestbooksByCollectionId ( childCollectionAlias )
252+ const withInherited = await sut . getGuestbooksByCollectionId (
253+ childCollectionAlias ,
254+ false ,
255+ true
256+ )
257+
258+ expect ( childCollectionId ) . toBeDefined ( )
259+ expect ( withoutInherited . some ( ( guestbook ) => guestbook . id === childGuestbookId ) ) . toBe ( true )
260+ expect ( withoutInherited . some ( ( guestbook ) => guestbook . id === parentGuestbookId ) ) . toBe ( false )
261+
262+ expect ( withInherited . some ( ( guestbook ) => guestbook . id === childGuestbookId ) ) . toBe ( true )
263+ expect ( withInherited . some ( ( guestbook ) => guestbook . id === parentGuestbookId ) ) . toBe ( true )
264+
265+ const inheritedGuestbook = withInherited . find (
266+ ( guestbook ) => guestbook . id === parentGuestbookId
267+ )
268+ expect ( inheritedGuestbook ?. name ) . toBe ( parentGuestbookName )
269+ } finally {
270+ if ( childCollectionId !== undefined ) {
271+ await deleteCollectionViaApi ( childCollectionAlias )
272+ }
273+ }
274+ } )
275+
276+ test ( 'should not include hierarchical owner guestbooks when includeInherited is false' , async ( ) => {
277+ const uniqueSuffix = Date . now ( ) . toString ( )
278+ const childCollectionAlias = `testGuestbooksNoInheritedChild${ uniqueSuffix } `
279+ const parentGuestbookName = `parent non inherited guestbook ${ uniqueSuffix } `
280+ const childGuestbookName = `child non inherited guestbook ${ uniqueSuffix } `
281+
282+ let childCollectionId : number | undefined
283+
284+ try {
285+ await createCollectionViaApi ( childCollectionAlias , testCollectionAlias ) . then (
286+ ( collectionPayload : CollectionPayload ) => ( childCollectionId = collectionPayload . id )
287+ )
288+ await publishCollectionViaApi ( childCollectionAlias )
289+
290+ const parentGuestbookId = await sut . createGuestbook ( testCollectionAlias , {
291+ ...createGuestbookDTO ,
292+ name : parentGuestbookName ,
293+ customQuestions : [ ]
294+ } )
295+ const childGuestbookId = await sut . createGuestbook ( childCollectionAlias , {
296+ ...createGuestbookDTO ,
297+ name : childGuestbookName ,
298+ customQuestions : [ ]
299+ } )
300+
301+ const withoutInherited = await sut . getGuestbooksByCollectionId (
302+ childCollectionAlias ,
303+ false ,
304+ false
305+ )
306+
307+ expect ( childCollectionId ) . toBeDefined ( )
308+ expect ( withoutInherited . some ( ( guestbook ) => guestbook . id === childGuestbookId ) ) . toBe ( true )
309+ expect ( withoutInherited . some ( ( guestbook ) => guestbook . id === parentGuestbookId ) ) . toBe ( false )
310+ } finally {
311+ if ( childCollectionId !== undefined ) {
312+ await deleteCollectionViaApi ( childCollectionAlias )
313+ }
314+ }
315+ } )
316+
317+ test ( 'should return inherited guestbooks for unpublished child collection when includeInherited is true' , async ( ) => {
318+ const uniqueSuffix = Date . now ( ) . toString ( )
319+ const unpublishedChildCollectionAlias = `testGuestbooksUnpublishedChild${ uniqueSuffix } `
320+ const parentGuestbookName = `parent unpublished child guestbook ${ uniqueSuffix } `
321+ let childCollectionId : number | undefined
322+ let parentGuestbookId : number | undefined
323+
324+ try {
325+ await createCollectionViaApi ( unpublishedChildCollectionAlias , testCollectionAlias ) . then (
326+ ( collectionPayload : CollectionPayload ) => ( childCollectionId = collectionPayload . id )
327+ )
328+
329+ parentGuestbookId = await sut . createGuestbook ( testCollectionAlias , {
330+ ...createGuestbookDTO ,
331+ name : parentGuestbookName ,
332+ customQuestions : [ ]
333+ } )
334+
335+ const actual = await sut . getGuestbooksByCollectionId (
336+ unpublishedChildCollectionAlias ,
337+ false ,
338+ true
339+ )
340+
341+ expect ( childCollectionId ) . toBeDefined ( )
342+ expect ( parentGuestbookId ) . toBeDefined ( )
343+ expect ( actual . some ( ( guestbook ) => guestbook . id === parentGuestbookId ) ) . toBe ( true )
344+ expect ( actual . some ( ( guestbook ) => guestbook . name === parentGuestbookName ) ) . toBe ( true )
345+ } finally {
346+ if ( childCollectionId !== undefined ) {
347+ await deleteCollectionViaApi ( unpublishedChildCollectionAlias )
348+ }
349+ }
350+ } )
351+
223352 test ( 'should return error when collection does not exist' , async ( ) => {
224353 await expect ( sut . getGuestbooksByCollectionId ( 999999 ) ) . rejects . toThrow ( ReadError )
225354 } )
0 commit comments