-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: create bound modal object (#9849)
- Loading branch information
Showing
4 changed files
with
62 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { describe, expect, it, vi, beforeAll } from 'vitest' | ||
import { SingletonModal } from './index.js' | ||
|
||
describe('SingletonModal', () => { | ||
const modal = new SingletonModal<void, { closeProp: number }>() | ||
const open = vi.fn() | ||
const close = vi.fn() | ||
const abort = vi.fn() | ||
let closeCallback: (props: { closeProp: number }) => void | ||
let rejectCallback: typeof modal.abort | ||
beforeAll(() => { | ||
modal.register((registerOnOpen, registerOnClose, registerOnAbort) => { | ||
closeCallback = registerOnClose | ||
rejectCallback = registerOnAbort | ||
return { | ||
open, | ||
close, | ||
abort, | ||
} | ||
}) | ||
}) | ||
it('should register successfully', () => { | ||
open.mockReset() | ||
modal.open() | ||
expect(open).toBeCalledTimes(1) | ||
}) | ||
it('can be called without object', () => { | ||
open.mockReset() | ||
;(0, modal.open)() | ||
expect(open).toBeCalledTimes(1) | ||
}) | ||
|
||
it('opens and waits for closing', async () => { | ||
const promise = modal.openAndWaitForClose() | ||
closeCallback({ closeProp: 1 }) | ||
expect(promise).resolves.toEqual({ closeProp: 1 }) | ||
}) | ||
|
||
it('gets rejected error', () => { | ||
const promise = modal.openAndWaitForClose() | ||
rejectCallback(new Error('Mock Error Message')) | ||
expect(promise).rejects.toThrow('Mock Error Message') | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,21 +1,22 @@ | ||
import { SingletonModal } from '@masknet/shared-base' | ||
import { WalletConnectQRCode, type WalletConnectQRCodeOpenProps } from './WalletConnectQRCodeDialog/index.js' | ||
import { memo } from 'react' | ||
import { SelectProviderModal, type SelectProviderDialogOpenProps } from './SelectProviderDialog/index.js' | ||
import { WalletStatusModal, type WalletStatusModalOpenProps } from './WalletStatusDialog/index.js' | ||
import { WalletConnectQRCode, type WalletConnectQRCodeOpenProps } from './WalletConnectQRCodeDialog/index.js' | ||
import { WalletRiskWarningModal, type WalletRiskWarningModalOpenProps } from './WalletRiskWarningDialog/index.js' | ||
import { WalletStatusModal, type WalletStatusModalOpenProps } from './WalletStatusDialog/index.js' | ||
|
||
export const WalletConnectQRCodeDialog = new SingletonModal<WalletConnectQRCodeOpenProps>() | ||
export const SelectProviderDialog = new SingletonModal<SelectProviderDialogOpenProps>() | ||
export const WalletStatusDialog = new SingletonModal<WalletStatusModalOpenProps>() | ||
export const WalletRiskWarningDialog = new SingletonModal<WalletRiskWarningModalOpenProps>() | ||
|
||
export function Modals() { | ||
export const Modals = memo(function Modals() { | ||
return ( | ||
<> | ||
<WalletConnectQRCode ref={WalletConnectQRCodeDialog.register.bind(WalletConnectQRCodeDialog)} /> | ||
<SelectProviderModal ref={SelectProviderDialog.register.bind(SelectProviderDialog)} /> | ||
<WalletStatusModal ref={WalletStatusDialog.register.bind(WalletStatusDialog)} /> | ||
<WalletRiskWarningModal ref={WalletRiskWarningDialog.register.bind(WalletRiskWarningDialog)} /> | ||
<WalletConnectQRCode ref={WalletConnectQRCodeDialog.register} /> | ||
<SelectProviderModal ref={SelectProviderDialog.register} /> | ||
<WalletStatusModal ref={WalletStatusDialog.register} /> | ||
<WalletRiskWarningModal ref={WalletRiskWarningDialog.register} /> | ||
</> | ||
) | ||
} | ||
}) |
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