-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: call
onTransactionId
immediately when tx id is available
Previously it was called only after gateway polling has finished which is not what it was intended to do.
- Loading branch information
1 parent
1936867
commit be34bce
Showing
6 changed files
with
181 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
packages/dapp-toolkit/src/modules/wallet-request/wallet-request.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { Logger } from './../../helpers/logger' | ||
import { describe, expect, it, vi } from 'vitest' | ||
import { WalletRequestModule } from './wallet-request' | ||
import { GatewayModule, RadixNetwork, TransactionStatus } from '../gateway' | ||
import { LocalStorageModule } from '../storage' | ||
import { ok, okAsync, ResultAsync } from 'neverthrow' | ||
import { WalletInteractionItems } from '../../schemas' | ||
import { | ||
RequestResolverModule, | ||
sendTransactionResponseResolver, | ||
} from './request-resolver' | ||
import { RequestItemModule } from './request-items' | ||
import { delayAsync } from '../../test-helpers/delay-async' | ||
|
||
const createMockEnvironment = () => { | ||
const storageModule = LocalStorageModule(`rdt:${crypto.randomUUID()}:1`) | ||
const requestItemModule = RequestItemModule({ | ||
providers: { | ||
storageModule, | ||
}, | ||
}) | ||
const gatewayModule = { | ||
pollTransactionStatus: (hash: string) => | ||
ResultAsync.fromSafePromise(delayAsync(2000)).map(() => | ||
ok({ status: 'success' as TransactionStatus }), | ||
), | ||
} as any | ||
const updateConnectButtonStatus = () => {} | ||
const stateModule = {} as any | ||
return { | ||
storageModule, | ||
requestItemModule, | ||
gatewayModule, | ||
updateConnectButtonStatus, | ||
stateModule, | ||
} | ||
} | ||
|
||
describe('WalletRequestModule', () => { | ||
describe('given `onTransactionId` callback is provided', () => { | ||
it('should call the callback before polling is finished', async () => { | ||
// Arange | ||
const { | ||
storageModule, | ||
requestItemModule, | ||
gatewayModule, | ||
updateConnectButtonStatus, | ||
stateModule, | ||
} = createMockEnvironment() | ||
|
||
const requestResolverModule = RequestResolverModule({ | ||
providers: { | ||
stateModule, | ||
gatewayModule, | ||
storageModule, | ||
requestItemModule, | ||
resolvers: [ | ||
sendTransactionResponseResolver({ | ||
gatewayModule, | ||
requestItemModule, | ||
updateConnectButtonStatus, | ||
}), | ||
], | ||
updateConnectButtonStatus, | ||
}, | ||
}) | ||
|
||
const interactionId = 'abcdef' | ||
const resultReturned = vi.fn() | ||
const onTransactionIdSpy = vi.fn() | ||
|
||
const walletRequestModule = WalletRequestModule({ | ||
useCache: false, | ||
networkId: RadixNetwork.Stokenet, | ||
dAppDefinitionAddress: '', | ||
providers: { | ||
stateModule: {} as any, | ||
storageModule, | ||
requestItemModule, | ||
requestResolverModule, | ||
gatewayModule, | ||
walletRequestSdk: { | ||
sendInteraction: () => okAsync({}), | ||
createWalletInteraction: (items: WalletInteractionItems) => ({ | ||
items, | ||
interactionId, | ||
metadata: {} as any, | ||
}), | ||
} as any, | ||
}, | ||
}) | ||
|
||
// Act | ||
walletRequestModule | ||
.sendTransaction({ | ||
transactionManifest: ``, | ||
onTransactionId: onTransactionIdSpy, | ||
}) | ||
.map(resultReturned) | ||
|
||
await delayAsync(50) | ||
|
||
requestResolverModule.addWalletResponses([ | ||
{ | ||
interactionId, | ||
discriminator: 'success', | ||
items: { | ||
discriminator: 'transaction', | ||
send: { | ||
transactionIntentHash: 'intent_hash', | ||
}, | ||
}, | ||
}, | ||
]) | ||
|
||
// Assert | ||
expect(resultReturned).not.toHaveBeenCalled() | ||
await expect | ||
.poll(() => onTransactionIdSpy, { | ||
timeout: 1000, | ||
}) | ||
.toHaveBeenCalledWith('intent_hash') | ||
await expect | ||
.poll(() => resultReturned, { | ||
timeout: 3000, | ||
}) | ||
.toHaveBeenCalledWith( | ||
expect.objectContaining({ transactionIntentHash: 'intent_hash' }), | ||
) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const delayAsync = (ms: number) => | ||
new Promise<void>((resolve) => { | ||
setTimeout(() => { | ||
resolve() | ||
}, ms) | ||
}) |