-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #191 from AElfProject/feature/simplified-init-flow
chore: refactor wallet-adapter-react to modular files and simplify interfaces
- Loading branch information
Showing
20 changed files
with
469 additions
and
263 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ jobs: | |
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
package: [utils, base] | ||
package: [utils, base, react] | ||
steps: | ||
- name: Run frontend CI | ||
uses: AElfProject/[email protected] | ||
|
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 @@ | ||
lts/iron |
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,2 +1,6 @@ | ||
{"total": {"lines":{"total":0,"covered":0,"skipped":0,"pct":"Unknown"},"statements":{"total":0,"covered":0,"skipped":0,"pct":"Unknown"},"functions":{"total":0,"covered":0,"skipped":0,"pct":"Unknown"},"branches":{"total":0,"covered":0,"skipped":0,"pct":"Unknown"},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":"Unknown"}} | ||
{"total": {"lines":{"total":69,"covered":58,"skipped":0,"pct":84.05},"statements":{"total":73,"covered":61,"skipped":0,"pct":83.56},"functions":{"total":17,"covered":12,"skipped":0,"pct":70.58},"branches":{"total":17,"covered":14,"skipped":0,"pct":82.35},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":"Unknown"}} | ||
,"/Users/aelf/Documents/Projects/aelf/aelf-web-login/packages/react/src/context.tsx": {"lines":{"total":15,"covered":13,"skipped":0,"pct":86.66},"functions":{"total":3,"covered":2,"skipped":0,"pct":66.66},"statements":{"total":16,"covered":14,"skipped":0,"pct":87.5},"branches":{"total":4,"covered":3,"skipped":0,"pct":75}} | ||
,"/Users/aelf/Documents/Projects/aelf/aelf-web-login/packages/react/src/init.ts": {"lines":{"total":20,"covered":18,"skipped":0,"pct":90},"functions":{"total":5,"covered":4,"skipped":0,"pct":80},"statements":{"total":21,"covered":19,"skipped":0,"pct":90.47},"branches":{"total":7,"covered":6,"skipped":0,"pct":85.71}} | ||
,"/Users/aelf/Documents/Projects/aelf/aelf-web-login/packages/react/src/useConnectWallet.tsx": {"lines":{"total":23,"covered":16,"skipped":0,"pct":69.56},"functions":{"total":4,"covered":2,"skipped":0,"pct":50},"statements":{"total":23,"covered":16,"skipped":0,"pct":69.56},"branches":{"total":6,"covered":5,"skipped":0,"pct":83.33}} | ||
,"/Users/aelf/Documents/Projects/aelf/aelf-web-login/packages/react/src/useExternalStore.tsx": {"lines":{"total":11,"covered":11,"skipped":0,"pct":100},"functions":{"total":5,"covered":4,"skipped":0,"pct":80},"statements":{"total":13,"covered":12,"skipped":0,"pct":92.3},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}} | ||
} |
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,3 +1,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<testsuites name="jest tests" tests="0" failures="0" errors="0" time="0.392"> | ||
<testsuites name="jest tests" tests="7" failures="0" errors="0" time="1.605"> | ||
<testsuite name="useConnectWallet" errors="0" failures="0" skipped="0" timestamp="2024-11-20T04:06:56" time="0.55" tests="1"> | ||
<testcase classname="useConnectWallet should render hook" name="useConnectWallet should render hook" time="0.006"> | ||
</testcase> | ||
</testsuite> | ||
<testsuite name="WebLoginProvider" errors="0" failures="0" skipped="0" timestamp="2024-11-20T04:06:57" time="0.172" tests="2"> | ||
<testcase classname="WebLoginProvider should render children with provided bridgeAPI" name="WebLoginProvider should render children with provided bridgeAPI" time="0.012"> | ||
</testcase> | ||
<testcase classname="WebLoginProvider should return null if no bridgeAPI is provided" name="WebLoginProvider should return null if no bridgeAPI is provided" time="0.001"> | ||
</testcase> | ||
</testsuite> | ||
<testsuite name="init" errors="0" failures="0" skipped="0" timestamp="2024-11-20T04:06:57" time="0.163" tests="3"> | ||
<testcase classname="init should initialize VConsole if showVconsole is true" name="init should initialize VConsole if showVconsole is true" time="0.054"> | ||
</testcase> | ||
<testcase classname="init should not initialize VConsole if showVconsole is false" name="init should not initialize VConsole if showVconsole is false" time="0.001"> | ||
</testcase> | ||
<testcase classname="init should call initBridge with the given options" name="init should call initBridge with the given options" time="0.001"> | ||
</testcase> | ||
</testsuite> | ||
<testsuite name="useExternalStore" errors="0" failures="0" skipped="0" timestamp="2024-11-20T04:06:57" time="0.15" tests="1"> | ||
<testcase classname="useExternalStore should render hook" name="useExternalStore should render hook" time="0.005"> | ||
</testcase> | ||
</testsuite> | ||
</testsuites> |
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,27 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import { WebLoginProvider } from '../context'; | ||
|
||
const mockBridgeAPI = { | ||
getSignIn: jest.fn((children) => children), | ||
}; | ||
|
||
describe('WebLoginProvider', () => { | ||
it('should render children with provided bridgeAPI', () => { | ||
render( | ||
<WebLoginProvider bridgeAPI={mockBridgeAPI}> | ||
<div>Test Child</div> | ||
</WebLoginProvider>, | ||
); | ||
|
||
expect(screen.getByText('Test Child')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should return null if no bridgeAPI is provided', () => { | ||
const { container } = render( | ||
<WebLoginProvider bridgeAPI={null}> | ||
<div>Test Child</div> | ||
</WebLoginProvider>, | ||
); | ||
expect(container.firstChild).toBeNull(); | ||
}); | ||
}); |
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 was deleted.
Oops, something went wrong.
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,29 @@ | ||
import { render } from '@testing-library/react'; | ||
import useConnectWallet from '../useConnectWallet'; | ||
import { WebLoginProvider } from '../context'; | ||
import { IBridgeAPI } from '@aelf-web-login/wallet-adapter-bridge'; | ||
|
||
const mockBridgeAPI: IBridgeAPI = { | ||
getSignIn: jest.fn((children) => children), | ||
store: { | ||
getState: () => null, | ||
subscribe: () => null, | ||
}, | ||
instance: { | ||
connect: () => null, | ||
}, | ||
}; | ||
|
||
const Comp = () => { | ||
useConnectWallet(); | ||
return null; | ||
}; | ||
describe('useConnectWallet', () => { | ||
it('should render hook', () => { | ||
render( | ||
<WebLoginProvider bridgeAPI={mockBridgeAPI}> | ||
<Comp /> | ||
</WebLoginProvider>, | ||
); | ||
}); | ||
}); |
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,26 @@ | ||
import { render } from '@testing-library/react'; | ||
import useExternalStore from '../useExternalStore'; | ||
import { WebLoginProvider } from '../context'; | ||
import { IBridgeAPI } from '@aelf-web-login/wallet-adapter-bridge'; | ||
|
||
const mockBridgeAPI: Partial<IBridgeAPI> = { | ||
getSignIn: jest.fn((children) => children), | ||
store: { | ||
getState: () => null, | ||
subscribe: () => null, | ||
}, | ||
}; | ||
|
||
const Comp = () => { | ||
useExternalStore(); | ||
return null; | ||
}; | ||
describe('useExternalStore', () => { | ||
it('should render hook', () => { | ||
render( | ||
<WebLoginProvider bridgeAPI={mockBridgeAPI}> | ||
<Comp /> | ||
</WebLoginProvider>, | ||
); | ||
}); | ||
}); |
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,39 @@ | ||
import { IBridgeAPI } from '@aelf-web-login/wallet-adapter-bridge'; | ||
import React from 'react'; | ||
|
||
const HOOK_ERROR_MESSAGE = | ||
'Must call the provided initialization method`init` method before using hooks.'; | ||
|
||
const WebLoginContext: React.Context<IBridgeAPI> = React.createContext<IBridgeAPI>( | ||
{} as IBridgeAPI, | ||
); | ||
|
||
export default WebLoginContext; | ||
|
||
export function useWebLoginContext(): IBridgeAPI { | ||
const bridgeAPI = React.useContext(WebLoginContext); | ||
|
||
if (!bridgeAPI) { | ||
throw new Error(HOOK_ERROR_MESSAGE); | ||
} | ||
|
||
return bridgeAPI; | ||
} | ||
|
||
export interface IWebLoginProviderProps { | ||
children: React.ReactNode; | ||
bridgeAPI: IBridgeAPI; | ||
} | ||
|
||
export const WebLoginProvider: React.FC<IWebLoginProviderProps> = ({ children, bridgeAPI }) => { | ||
const { getSignIn } = bridgeAPI ?? { | ||
getSignIn: () => null, | ||
}; | ||
|
||
if (!bridgeAPI) { | ||
return null; | ||
} | ||
return ( | ||
<WebLoginContext.Provider value={bridgeAPI}>{getSignIn(children)}</WebLoginContext.Provider> | ||
); | ||
}; |
Oops, something went wrong.