-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
π Add modals support in stories (#4457)
* Add modal and transaction support in page stories * Mock chain numbers as Codec instead of BNs * Simplify mocking balances * Improve the `MockApi` type * Mock transaction results with `TxMock` * Move the Pages section to the top menu * Mock the `api.tx.proposalsEngine.vote` transaction * Always redirects to the current story * Improve TxMock * Use a single spy for `onCall` and `onSend` Because Storybook actions are converted to jest.fn
- Loading branch information
Showing
9 changed files
with
196 additions
and
108 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { createType } from '@joystream/types' | ||
import BN from 'bn.js' | ||
import { asyncScheduler, from, of, scheduled } from 'rxjs' | ||
|
||
import { Balance } from '../providers/accounts' | ||
import { BLOCK_HASH } from '../providers/api' | ||
|
||
import { joy } from '.' | ||
|
||
export type TxMock = { | ||
data?: any[] | any | ||
failure?: string | ||
event?: string | ||
fee?: Balance | ||
onCall?: CallableFunction | ||
onSend?: CallableFunction | ||
} | ||
|
||
export const fromTxMock = ( | ||
{ data, failure, event: eventName = 'Default Event', fee = 5, onCall, onSend }: TxMock, | ||
moduleName: string | ||
) => { | ||
const event = failure ? createErrorEvents(failure) : createSuccessEvents([data ?? []].flat(), moduleName, eventName) | ||
const txResult = stubTransactionResult(event) | ||
|
||
const paymentInfo = () => of({ partialFee: createType('BalanceOf', joy(fee)) }) | ||
|
||
return (...args: any[]) => { | ||
onCall?.(...args) | ||
return { | ||
paymentInfo, | ||
signAndSend: () => { | ||
onSend?.(...args) | ||
return txResult | ||
}, | ||
} | ||
} | ||
} | ||
|
||
export const stubTransactionResult = (events: any[]) => | ||
scheduled( | ||
from([ | ||
{ | ||
status: { isReady: true, type: 'Ready' }, | ||
}, | ||
{ | ||
status: { type: 'InBlock', isInBlock: true, asInBlock: BLOCK_HASH }, | ||
events: [...events], | ||
}, | ||
{ | ||
status: { type: 'Finalized', isFinalized: true, asFinalized: BLOCK_HASH }, | ||
events: [...events], | ||
}, | ||
]), | ||
asyncScheduler | ||
) | ||
|
||
export const createSuccessEvents = (data: any[], section: string, method: string) => [ | ||
{ | ||
phase: { ApplyExtrinsic: 2 }, | ||
event: { index: '0x0502', data, method, section }, | ||
}, | ||
{ | ||
phase: { ApplyExtrinsic: 2 }, | ||
event: { index: '0x0000', data: [{ weight: 190949000, class: 'Normal', paysFee: 'Yes' }] }, | ||
}, | ||
] | ||
|
||
export const createErrorEvents = (errorMessage: string) => [ | ||
{ | ||
phase: { ApplyExtrinsic: 2 }, | ||
event: { | ||
index: '0x0001', | ||
data: [ | ||
{ | ||
Module: { index: new BN(5), error: new BN(3) }, | ||
isModule: true, | ||
registry: { findMetaError: () => ({ docs: [errorMessage] }) }, | ||
}, | ||
{ weight: 190949000, class: 'Normal', paysFee: 'Yes' }, | ||
], | ||
section: 'system', | ||
method: 'ExtrinsicFailed', | ||
}, | ||
}, | ||
] |
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
Oops, something went wrong.
195f04a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
pioneer-2-storybook β ./
pioneer-2-storybook-joystream.vercel.app
pioneer-2-storybook-git-dev-joystream.vercel.app
pioneer-2-storybook.vercel.app
195f04a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
pioneer-2 β ./
pioneer-2-git-dev-joystream.vercel.app
pioneer-2.vercel.app
pioneer-2-joystream.vercel.app