-
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 (#287)
* 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. * fix: adjust preauthorization response model
- Loading branch information
1 parent
e0d26fc
commit 0458176
Showing
9 changed files
with
189 additions
and
32 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
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
126 changes: 126 additions & 0 deletions
126
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,126 @@ | ||
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 = () => {} | ||
return { | ||
storageModule, | ||
requestItemModule, | ||
gatewayModule, | ||
updateConnectButtonStatus, | ||
} | ||
} | ||
|
||
describe('WalletRequestModule', () => { | ||
describe('given `onTransactionId` callback is provided', () => { | ||
it('should call the callback before polling is finished', async () => { | ||
// Arange | ||
const { | ||
storageModule, | ||
requestItemModule, | ||
gatewayModule, | ||
updateConnectButtonStatus, | ||
} = createMockEnvironment() | ||
|
||
const requestResolverModule = RequestResolverModule({ | ||
providers: { | ||
storageModule, | ||
requestItemModule, | ||
resolvers: [ | ||
sendTransactionResponseResolver({ | ||
gatewayModule, | ||
requestItemModule, | ||
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
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) | ||
}) |