From 24281e983e8b6fa6352906832adcbcab176a69f9 Mon Sep 17 00:00:00 2001 From: jorbuedo <jorbuedo@gmail.com> Date: Wed, 18 Sep 2024 12:26:10 +0200 Subject: [PATCH] Fix tests --- packages/claim/src/manager.test.ts | 40 +++--- packages/claim/src/transformers.test.ts | 63 ++++++---- .../portfolio/src/balance-manager.test.ts | 119 ++---------------- packages/portfolio/src/index.ts | 1 + packages/portfolio/src/token-manager.mock.ts | 24 ++++ 5 files changed, 88 insertions(+), 159 deletions(-) create mode 100644 packages/portfolio/src/token-manager.mock.ts diff --git a/packages/claim/src/manager.test.ts b/packages/claim/src/manager.test.ts index 4b017dd7e7..777196bed0 100644 --- a/packages/claim/src/manager.test.ts +++ b/packages/claim/src/manager.test.ts @@ -1,5 +1,9 @@ -import {fetchData} from '@yoroi/common' -import {tokenInfoMocks, tokenMocks} from '@yoroi/portfolio' +import {cacheRecordMaker, fetchData} from '@yoroi/common' +import { + createTokenManagerMock, + tokenInfoMocks, + tokenMocks, +} from '@yoroi/portfolio' import {Api, Portfolio, Scan} from '@yoroi/types' import {claimManagerMaker} from './manager' @@ -27,32 +31,24 @@ describe('claimManagerMaker - postClaimTokens', () => { jest.clearAllMocks() }) - const tokenManagerMock = { - sync: jest.fn(), - api: { - tokenActivity: jest.fn(), - tokenHistory: jest.fn(), - tokenDiscovery: jest.fn(), - tokenImageInvalidate: jest.fn(), - tokenInfo: jest.fn(), - tokenInfos: jest.fn(), - tokenTraits: jest.fn(), - }, - clear: jest.fn(), - destroy: jest.fn(), - hydrate: jest.fn(), - subscribe: jest.fn(), - unsubscribe: jest.fn(), - observable$: {} as any, - } + const tokenManagerMock = createTokenManagerMock() tokenManagerMock.sync.mockResolvedValue( new Map([ [ tokenMocks.nftCryptoKitty.info.id, - {record: tokenMocks.nftCryptoKitty.info}, + cacheRecordMaker( + {expires: Date.now() + 3_600_000, hash: 'hash3'}, + tokenMocks.nftCryptoKitty.info, + ), + ], + [ + tokenMocks.rnftWhatever.info.id, + cacheRecordMaker( + {expires: Date.now() + 3_600_000, hash: 'hash3'}, + tokenMocks.rnftWhatever.info, + ), ], - [tokenMocks.rnftWhatever.info.id, {record: tokenMocks.rnftWhatever.info}], ]), ) diff --git a/packages/claim/src/transformers.test.ts b/packages/claim/src/transformers.test.ts index 2c81677982..386fa4941b 100644 --- a/packages/claim/src/transformers.test.ts +++ b/packages/claim/src/transformers.test.ts @@ -1,27 +1,12 @@ import {Api, Claim, Portfolio} from '@yoroi/types' -import {tokenMocks} from '@yoroi/portfolio' +import {tokenMocks, createTokenManagerMock} from '@yoroi/portfolio' import {asClaimApiError, asClaimToken} from './transformers' import {claimFaucetResponses} from './api-faucet.mocks' import {claimApiMockResponses} from './manager.mocks' +import {cacheRecordMaker} from '@yoroi/common' -const tokenManagerMock = { - sync: jest.fn(), - api: { - tokenActivity: jest.fn(), - tokenDiscovery: jest.fn(), - tokenImageInvalidate: jest.fn(), - tokenInfo: jest.fn(), - tokenInfos: jest.fn(), - tokenTraits: jest.fn(), - }, - clear: jest.fn(), - destroy: jest.fn(), - hydrate: jest.fn(), - subscribe: jest.fn(), - unsubscribe: jest.fn(), - observable$: {} as any, -} +const tokenManagerMock = createTokenManagerMock() describe('asClaimApiError', () => { afterEach(() => { @@ -65,11 +50,17 @@ describe('asClaimToken', () => { new Map([ [ tokenMocks.nftCryptoKitty.info.id, - {record: tokenMocks.nftCryptoKitty.info}, + cacheRecordMaker( + {expires: Date.now() + 3_600_000, hash: 'hash3'}, + tokenMocks.nftCryptoKitty.info, + ), ], [ tokenMocks.rnftWhatever.info.id, - {record: tokenMocks.rnftWhatever.info}, + cacheRecordMaker( + {expires: Date.now() + 3_600_000, hash: 'hash3'}, + tokenMocks.rnftWhatever.info, + ), ], ]), ) @@ -98,11 +89,17 @@ describe('asClaimToken', () => { new Map([ [ tokenMocks.nftCryptoKitty.info.id, - {record: tokenMocks.nftCryptoKitty.info}, + cacheRecordMaker( + {expires: Date.now() + 3_600_000, hash: 'hash3'}, + tokenMocks.nftCryptoKitty.info, + ), ], [ tokenMocks.rnftWhatever.info.id, - {record: tokenMocks.rnftWhatever.info}, + cacheRecordMaker( + {expires: Date.now() + 3_600_000, hash: 'hash3'}, + tokenMocks.rnftWhatever.info, + ), ], ]), ) @@ -123,11 +120,17 @@ describe('asClaimToken', () => { new Map([ [ tokenMocks.nftCryptoKitty.info.id, - {record: tokenMocks.nftCryptoKitty.info}, + cacheRecordMaker( + {expires: Date.now() + 3_600_000, hash: 'hash3'}, + tokenMocks.nftCryptoKitty.info, + ), ], [ tokenMocks.rnftWhatever.info.id, - {record: tokenMocks.rnftWhatever.info}, + cacheRecordMaker( + {expires: Date.now() + 3_600_000, hash: 'hash3'}, + tokenMocks.rnftWhatever.info, + ), ], ]), ) @@ -156,13 +159,19 @@ describe('asClaimToken', () => { new Map([ [ tokenMocks.nftCryptoKitty.info.id, - {record: tokenMocks.nftCryptoKitty.info}, + cacheRecordMaker( + {expires: Date.now() + 3_600_000, hash: 'hash3'}, + tokenMocks.nftCryptoKitty.info, + ), ], [ tokenMocks.rnftWhatever.info.id, - {record: tokenMocks.rnftWhatever.info}, + cacheRecordMaker( + {expires: Date.now() + 3_600_000, hash: 'hash3'}, + tokenMocks.rnftWhatever.info, + ), ], - ['invalid.', undefined], + ['invalid.', undefined] as any, ['dead.', {record: tokenMocks.rnftWhatever.info}], ]), ) diff --git a/packages/portfolio/src/balance-manager.test.ts b/packages/portfolio/src/balance-manager.test.ts index 929828eac2..bffab5c420 100644 --- a/packages/portfolio/src/balance-manager.test.ts +++ b/packages/portfolio/src/balance-manager.test.ts @@ -10,6 +10,7 @@ import {portfolioBalanceStorageMaker} from './adapters/mmkv-storage/balance-stor import {tokenInfoMocks} from './adapters/token-info.mocks' import {isFt} from './helpers/is-ft' import {isNft} from './helpers/is-nft' +import {createTokenManagerMock} from './token-manager.mock' const tokenInfoStorage = observableStorageMaker( mountMMKVStorage<Portfolio.Token.Id>({ @@ -37,24 +38,7 @@ describe('portfolioBalanceManagerMaker', () => { primaryBreakdownStorage, primaryTokenId, }) - const tokenManager: Portfolio.Manager.Token = { - destroy: jest.fn(), - hydrate: jest.fn(), - subscribe: jest.fn(), - unsubscribe: jest.fn(), - observable$: new BehaviorSubject({} as any).asObservable(), - sync: jest.fn().mockResolvedValue(new Map()), - clear: jest.fn(), - api: { - tokenInfo: jest.fn(), - tokenInfos: jest.fn(), - tokenDiscovery: jest.fn(), - tokenTraits: jest.fn(), - tokenActivity: jest.fn(), - tokenHistory: jest.fn(), - tokenImageInvalidate: jest.fn(), - }, - } + const tokenManager = createTokenManagerMock() it('should be instantiated', () => { const manager = portfolioBalanceManagerMaker({ @@ -86,24 +70,7 @@ describe('hydrate', () => { primaryBreakdownStorage, primaryTokenId, }) - const tokenManager: Portfolio.Manager.Token = { - destroy: jest.fn(), - hydrate: jest.fn(), - subscribe: jest.fn(), - unsubscribe: jest.fn(), - observable$: new BehaviorSubject({} as any).asObservable(), - sync: jest.fn().mockResolvedValue(new Map()), - clear: jest.fn(), - api: { - tokenInfo: jest.fn(), - tokenInfos: jest.fn(), - tokenDiscovery: jest.fn(), - tokenTraits: jest.fn(), - tokenActivity: jest.fn(), - tokenHistory: jest.fn(), - tokenImageInvalidate: jest.fn(), - }, - } + const tokenManager = createTokenManagerMock() afterEach(() => { storage.clear() @@ -176,25 +143,7 @@ describe('destroy', () => { primaryBreakdownStorage, primaryTokenId, }) - const tokenManagerObservable = new BehaviorSubject({} as any).asObservable() - const tokenManager: jest.Mocked<Portfolio.Manager.Token> = { - destroy: jest.fn(), - hydrate: jest.fn(), - subscribe: jest.fn(), - unsubscribe: jest.fn(), - observable$: tokenManagerObservable, - sync: jest.fn().mockResolvedValue(new Map()), - clear: jest.fn(), - api: { - tokenInfo: jest.fn(), - tokenInfos: jest.fn(), - tokenDiscovery: jest.fn(), - tokenTraits: jest.fn(), - tokenActivity: jest.fn(), - tokenHistory: jest.fn(), - tokenImageInvalidate: jest.fn(), - }, - } + const tokenManager = createTokenManagerMock() const queueDestroy = jest.fn() const observerDestroy = jest.fn() @@ -261,25 +210,7 @@ describe('primary updates', () => { primaryBreakdownStorage, primaryTokenId, }) - const tokenManagerObservable = new BehaviorSubject({} as any) - const tokenManager: jest.Mocked<Portfolio.Manager.Token> = { - destroy: jest.fn(), - hydrate: jest.fn(), - subscribe: jest.fn(), - unsubscribe: jest.fn(), - observable$: tokenManagerObservable.asObservable(), - sync: jest.fn().mockResolvedValue(new Map()), - clear: jest.fn(), - api: { - tokenInfo: jest.fn(), - tokenInfos: jest.fn(), - tokenDiscovery: jest.fn(), - tokenTraits: jest.fn(), - tokenActivity: jest.fn(), - tokenHistory: jest.fn(), - tokenImageInvalidate: jest.fn(), - }, - } + const tokenManager = createTokenManagerMock() afterEach(() => { storage.clear() @@ -422,24 +353,9 @@ describe('sync & refresh', () => { primaryTokenId, }) const tokenManagerObservable = new BehaviorSubject({} as any) - const tokenManager: jest.Mocked<Portfolio.Manager.Token> = { - destroy: jest.fn(), - hydrate: jest.fn(), - subscribe: jest.fn(), - unsubscribe: jest.fn(), - observable$: tokenManagerObservable.asObservable(), - sync: jest.fn().mockResolvedValue(new Map()), - clear: jest.fn(), - api: { - tokenInfo: jest.fn(), - tokenInfos: jest.fn(), - tokenDiscovery: jest.fn(), - tokenTraits: jest.fn(), - tokenActivity: jest.fn(), - tokenHistory: jest.fn(), - tokenImageInvalidate: jest.fn(), - }, - } + const tokenManager = createTokenManagerMock( + tokenManagerObservable.asObservable(), + ) afterEach(() => { storage.clear() @@ -715,24 +631,7 @@ describe('clear', () => { primaryBreakdownStorage, primaryTokenId, }) - const tokenManager: Portfolio.Manager.Token = { - destroy: jest.fn(), - hydrate: jest.fn(), - subscribe: jest.fn(), - unsubscribe: jest.fn(), - observable$: new BehaviorSubject({} as any).asObservable(), - sync: jest.fn().mockResolvedValue(new Map()), - clear: jest.fn(), - api: { - tokenInfo: jest.fn(), - tokenInfos: jest.fn(), - tokenDiscovery: jest.fn(), - tokenTraits: jest.fn(), - tokenActivity: jest.fn(), - tokenHistory: jest.fn(), - tokenImageInvalidate: jest.fn(), - }, - } + const tokenManager = createTokenManagerMock() afterEach(() => { storage.clear() diff --git a/packages/portfolio/src/index.ts b/packages/portfolio/src/index.ts index 14fe8e508e..c6b07dcc37 100644 --- a/packages/portfolio/src/index.ts +++ b/packages/portfolio/src/index.ts @@ -42,6 +42,7 @@ export * from './translators/reactjs/usePortfolioTokenInfo' export * from './balance-manager' export * from './token-manager' +export * from './token-manager.mock' export * from './types' export * from './constants' diff --git a/packages/portfolio/src/token-manager.mock.ts b/packages/portfolio/src/token-manager.mock.ts new file mode 100644 index 0000000000..cbdad48d6f --- /dev/null +++ b/packages/portfolio/src/token-manager.mock.ts @@ -0,0 +1,24 @@ +import {Portfolio} from '@yoroi/types' +import {BehaviorSubject, Observable} from 'rxjs' + +export const createTokenManagerMock = ( + tokenManagerObservable?: Observable<any>, +): jest.Mocked<Portfolio.Manager.Token> => ({ + destroy: jest.fn(), + hydrate: jest.fn(), + subscribe: jest.fn(), + unsubscribe: jest.fn(), + observable$: + tokenManagerObservable ?? new BehaviorSubject({} as any).asObservable(), + sync: jest.fn().mockResolvedValue(new Map()), + clear: jest.fn(), + api: { + tokenInfo: jest.fn(), + tokenInfos: jest.fn(), + tokenDiscovery: jest.fn(), + tokenTraits: jest.fn(), + tokenActivity: jest.fn(), + tokenHistory: jest.fn(), + tokenImageInvalidate: jest.fn(), + }, +})