@@ -518,6 +518,152 @@ describe('Note API', () => {
518518
519519 expect ( response ?. json ( ) . message ) . toStrictEqual ( expectedMessage ) ;
520520 } ) ;
521+
522+ test ( 'Returns one parents note in case when note has one parent' , async ( ) => {
523+ /** Create test user */
524+ const user = await global . db . insertUser ( ) ;
525+
526+ /** Create access token for the user */
527+ const accessToken = global . auth ( user . id ) ;
528+
529+ /** Create test note - a parent note */
530+ const parentNote = await global . db . insertNote ( {
531+ creatorId : user . id ,
532+ } ) ;
533+
534+ /** Create test note - a child note */
535+ const childNote = await global . db . insertNote ( {
536+ creatorId : user . id ,
537+ } ) ;
538+
539+ /** Create test note settings */
540+ await global . db . insertNoteSetting ( {
541+ noteId : childNote . id ,
542+ isPublic : true ,
543+ } ) ;
544+
545+ /** Create test note relation */
546+ await global . db . insertNoteRelation ( {
547+ parentId : parentNote . id ,
548+ noteId : childNote . id ,
549+ } ) ;
550+
551+ const response = await global . api ?. fakeRequest ( {
552+ method : 'GET' ,
553+ headers : {
554+ authorization : `Bearer ${ accessToken } ` ,
555+ } ,
556+ url : `/note/${ childNote . publicId } ` ,
557+ } ) ;
558+
559+ expect ( response ?. statusCode ) . toBe ( 200 ) ;
560+
561+ expect ( response ?. json ( ) ) . toMatchObject ( {
562+ parents : [
563+ {
564+ id : parentNote . publicId ,
565+ content : parentNote . content ,
566+ } ,
567+ ] ,
568+ } ) ;
569+ } ) ;
570+
571+ test ( 'Returns note parents in correct order in case when parents created in a non-linear order' , async ( ) => {
572+ /** Create test user */
573+ const user = await global . db . insertUser ( ) ;
574+
575+ /** Create access token for the user */
576+ const accessToken = global . auth ( user . id ) ;
577+
578+ /** Create test note - a grand parent note */
579+ const firstNote = await global . db . insertNote ( {
580+ creatorId : user . id ,
581+ } ) ;
582+
583+ /** Create test note - a parent note */
584+ const secondNote = await global . db . insertNote ( {
585+ creatorId : user . id ,
586+ } ) ;
587+
588+ /** Create test note - a child note */
589+ const thirdNote = await global . db . insertNote ( {
590+ creatorId : user . id ,
591+ } ) ;
592+
593+ /** Create test note settings */
594+ await global . db . insertNoteSetting ( {
595+ noteId : secondNote . id ,
596+ isPublic : true ,
597+ } ) ;
598+
599+ /** Create note relation between parent and grandParentNote */
600+ await global . db . insertNoteRelation ( {
601+ parentId : firstNote . id ,
602+ noteId : thirdNote . id ,
603+ } ) ;
604+
605+ /** Create test note relation */
606+ await global . db . insertNoteRelation ( {
607+ parentId : thirdNote . id ,
608+ noteId : secondNote . id ,
609+ } ) ;
610+
611+ const response = await global . api ?. fakeRequest ( {
612+ method : 'GET' ,
613+ headers : {
614+ authorization : `Bearer ${ accessToken } ` ,
615+ } ,
616+ url : `/note/${ secondNote . publicId } ` ,
617+ } ) ;
618+
619+ expect ( response ?. statusCode ) . toBe ( 200 ) ;
620+
621+ expect ( response ?. json ( ) ) . toMatchObject ( {
622+ parents : [
623+ {
624+ id : firstNote . publicId ,
625+ content : firstNote . content ,
626+ } ,
627+ {
628+ id : thirdNote . publicId ,
629+ content : thirdNote . content ,
630+ } ,
631+ ] ,
632+ } ) ;
633+ } ) ;
634+
635+ test ( 'Returns empty array in case where there is no relation exist for the note' , async ( ) => {
636+ /** Create test user */
637+ const user = await global . db . insertUser ( ) ;
638+
639+ /** Create access token for the user */
640+ const accessToken = global . auth ( user . id ) ;
641+
642+ /** Create test note - a child note */
643+ const note = await global . db . insertNote ( {
644+ creatorId : user . id ,
645+ } ) ;
646+
647+ /** Create test note settings */
648+ await global . db . insertNoteSetting ( {
649+ noteId : note . id ,
650+ isPublic : true ,
651+ } ) ;
652+
653+ const response = await global . api ?. fakeRequest ( {
654+ method : 'GET' ,
655+ headers : {
656+ authorization : `Bearer ${ accessToken } ` ,
657+ } ,
658+ url : `/note/${ note . publicId } ` ,
659+ } ) ;
660+
661+ expect ( response ?. statusCode ) . toBe ( 200 ) ;
662+
663+ expect ( response ?. json ( ) ) . toMatchObject ( {
664+ parents : [ ] ,
665+ } ) ;
666+ } ) ;
521667 } ) ;
522668
523669 describe ( 'PATCH note/:notePublicId ' , ( ) => {
0 commit comments