@@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react'
22import { renderHook , waitFor } from '@testing-library/react'
33import { generateTestingUtils } from 'eth-testing'
44import { appConfig } from '../config'
5- import { Catalog , AuthToken , SubscriptionsAndServicesDDOs } from '../../src'
5+ import { Catalog , AuthToken , SubscriptionsAndServicesDDOs , DDO } from '../../src'
66import jwt from 'jsonwebtoken'
77import { ddo , ddo2 , ddo3 , walletAddress , nevermined } from '../mockups'
88import { faker } from '@faker-js/faker'
@@ -532,16 +532,42 @@ describe('Nevermined account', () => {
532532 } )
533533
534534 it ( 'should get all the subscriptions published by the address passed' , async ( ) => {
535- const sdk = await jest . requireActual ( '../mockups' ) . nevermined . getInstance ( )
536- jest . spyOn ( nevermined , 'getInstance' ) . mockResolvedValue ( {
537- ...sdk ,
538- assets : {
539- ...sdk . assets ,
540- resolve : ( id : string ) => [ ddo , ddo2 , ddo3 ] . find ( item => item . id === id )
535+ const { result } = renderHook (
536+ ( ) => {
537+ const { account, isLoadingSDK, updateSDK } = Catalog . useNevermined ( )
538+ const [ subscriptions , setSubscriptions ] = useState < DDO [ ] > ( [ ] )
539+
540+ useEffect ( ( ) => {
541+ if ( isLoadingSDK ) {
542+ appConfig . web3Provider = testingUtils . getProvider ( )
543+ updateSDK ( appConfig )
544+ return
545+ }
546+
547+ ( async ( ) => {
548+ try {
549+ const result = await account . getPublishedSubscriptions ( walletAddress )
550+
551+ setSubscriptions ( [ ...result ] )
552+ } catch ( error : any ) {
553+ console . error ( error . message )
554+ }
555+ } ) ( )
556+ } , [ isLoadingSDK ] )
557+
558+ return subscriptions
559+ } ,
560+ {
561+ wrapper : wrapperProvider
541562 }
542-
563+ )
564+
565+ await waitFor ( ( ) => {
566+ expect ( result . current ) . toStrictEqual ( [ ddo ] )
543567 } )
568+ } )
544569
570+ it ( 'should get all the subscriptions published and services by the address passed' , async ( ) => {
545571 const { result } = renderHook (
546572 ( ) => {
547573 const { account, isLoadingSDK, updateSDK } = Catalog . useNevermined ( )
@@ -556,7 +582,7 @@ describe('Nevermined account', () => {
556582
557583 ( async ( ) => {
558584 try {
559- const result = await account . getPublishedSubscriptions ( walletAddress )
585+ const result = await account . getPublishedSubscriptionsAndServices ( walletAddress )
560586
561587 setSubscriptions ( [ ...result ] )
562588 } catch ( error : any ) {
@@ -581,16 +607,42 @@ describe('Nevermined account', () => {
581607 } )
582608
583609 it ( 'should get all the subscriptions purchased by the address passed' , async ( ) => {
584- const sdk = await jest . requireActual ( '../mockups' ) . nevermined . getInstance ( )
585- jest . spyOn ( nevermined , 'getInstance' ) . mockResolvedValue ( {
586- ...sdk ,
587- assets : {
588- ...sdk . assets ,
589- resolve : ( id : string ) => [ ddo , ddo2 , ddo3 ] . find ( item => item . id === id )
610+ const { result } = renderHook (
611+ ( ) => {
612+ const { account, isLoadingSDK, updateSDK } = Catalog . useNevermined ( )
613+ const [ subscriptions , setSubscriptions ] = useState < DDO [ ] > ( [ ] )
614+
615+ useEffect ( ( ) => {
616+ if ( isLoadingSDK ) {
617+ appConfig . web3Provider = testingUtils . getProvider ( )
618+ updateSDK ( appConfig )
619+ return
620+ }
621+
622+ ( async ( ) => {
623+ try {
624+ const result = await account . getPurchasedSubscriptions ( walletAddress )
625+
626+ setSubscriptions ( [ ...result ] )
627+ } catch ( error : any ) {
628+ console . error ( error . message )
629+ }
630+ } ) ( )
631+ } , [ isLoadingSDK ] )
632+
633+ return subscriptions
634+ } ,
635+ {
636+ wrapper : wrapperProvider
590637 }
591-
638+ )
639+
640+ await waitFor ( ( ) => {
641+ expect ( result . current ) . toStrictEqual ( [ ddo ] )
592642 } )
643+ } )
593644
645+ it ( 'should get all the subscriptions purchased and services by the address passed' , async ( ) => {
594646 const { result } = renderHook (
595647 ( ) => {
596648 const { account, isLoadingSDK, updateSDK } = Catalog . useNevermined ( )
@@ -605,7 +657,7 @@ describe('Nevermined account', () => {
605657
606658 ( async ( ) => {
607659 try {
608- const result = await account . getPurchasedSubscriptions ( walletAddress )
660+ const result = await account . getPurchasedSubscriptionsAndServices ( walletAddress )
609661
610662 setSubscriptions ( [ ...result ] )
611663 } catch ( error : any ) {
@@ -628,4 +680,40 @@ describe('Nevermined account', () => {
628680 } ] )
629681 } )
630682 } )
683+
684+ it ( 'should get all the services associated to the subscription id passed' , async ( ) => {
685+ const { result } = renderHook (
686+ ( ) => {
687+ const { account, isLoadingSDK, updateSDK } = Catalog . useNevermined ( )
688+ const [ services , setServices ] = useState < DDO [ ] > ( [ ] )
689+
690+ useEffect ( ( ) => {
691+ if ( isLoadingSDK ) {
692+ appConfig . web3Provider = testingUtils . getProvider ( )
693+ updateSDK ( appConfig )
694+ return
695+ }
696+
697+ ( async ( ) => {
698+ try {
699+ const result = await account . getAssociatedServices ( ddo . id )
700+
701+ setServices ( [ ...result ] )
702+ } catch ( error : any ) {
703+ console . error ( error . message )
704+ }
705+ } ) ( )
706+ } , [ isLoadingSDK ] )
707+
708+ return services
709+ } ,
710+ {
711+ wrapper : wrapperProvider
712+ }
713+ )
714+
715+ await waitFor ( ( ) => {
716+ expect ( result . current ) . toStrictEqual ( [ ddo2 , ddo3 ] )
717+ } )
718+ } )
631719} )
0 commit comments