Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jorbuedo committed Sep 18, 2024
1 parent 925a487 commit 24281e9
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 159 deletions.
40 changes: 18 additions & 22 deletions packages/claim/src/manager.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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}],
]),
)

Expand Down
63 changes: 36 additions & 27 deletions packages/claim/src/transformers.test.ts
Original file line number Diff line number Diff line change
@@ -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(() => {
Expand Down Expand Up @@ -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,
),
],
]),
)
Expand Down Expand Up @@ -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,
),
],
]),
)
Expand All @@ -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,
),
],
]),
)
Expand Down Expand Up @@ -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}],
]),
)
Expand Down
119 changes: 9 additions & 110 deletions packages/portfolio/src/balance-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>({
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions packages/portfolio/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
24 changes: 24 additions & 0 deletions packages/portfolio/src/token-manager.mock.ts
Original file line number Diff line number Diff line change
@@ -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(),
},
})

0 comments on commit 24281e9

Please sign in to comment.