-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add tests for heartbit react #61
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
77c3d4d
added core intital tests
Joshua-onwuzu ee9de5c
updated test case v2
Joshua-onwuzu 7fbfb21
updated test cmd
Joshua-onwuzu 80db098
refactored core test-cases
Joshua-onwuzu 1479248
fix import issues with core testcases
Joshua-onwuzu 9b94513
added test workflow
Joshua-onwuzu 916482f
resolved PR comments
Joshua-onwuzu a725081
initial tests for react pkg
Joshua-onwuzu 686ee74
Fix testing for react
nadeem-fileverse 8ef750f
update lock
nadeem-fileverse be25327
added tests for react provider and hook
Joshua-onwuzu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,63 @@ | ||
name: 🚀 Run tests | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
workflow_dispatch: | ||
|
||
concurrency: ${{ github.workflow }}-${{ github.ref }} | ||
|
||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
jobs: | ||
test: | ||
name: 🚀 Test | ||
strategy: | ||
matrix: | ||
os: [ubuntu-latest] | ||
node-version: [lts/*] | ||
pnpm-version: [latest] | ||
runs-on: ${{ matrix.os }} | ||
|
||
steps: | ||
- name: ⬇️ Checkout | ||
uses: actions/[email protected] | ||
with: | ||
token: ${{ env.GITHUB_TOKEN }} | ||
fetch-depth: 0 | ||
|
||
- name: 🟢 Setup node | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
|
||
- name: 🥡 Setup pnpm | ||
uses: pnpm/[email protected] | ||
with: | ||
version: ${{ matrix.pnpm-version }} | ||
run_install: false | ||
|
||
- name: 🔆 Cache pnpm modules | ||
uses: actions/cache@v3 | ||
with: | ||
path: ~/.pnpm-store | ||
key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }} | ||
restore-keys: | | ||
${{ runner.os }}-pnpm- | ||
|
||
- name: 🧩 Install Dependencies | ||
run: pnpm install | ||
working-directory: ./packages/heartbit-core | ||
|
||
- name: 🏗️ Build | ||
run: pnpm run build | ||
working-directory: ./packages/heartbit-core | ||
|
||
- name: 🧪 Run Tests | ||
run: pnpm test | ||
working-directory: ./packages/heartbit-core |
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,14 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
globals: { | ||
'ts-jest': { | ||
useESM: true, | ||
tsconfig: { | ||
verbatimModuleSyntax: false, | ||
}, | ||
}, | ||
}, | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
transform: {}, | ||
}; |
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,83 @@ | ||
import { HeartBitCore } from '../index'; | ||
import { getMinterContract } from '../utils'; | ||
import { describe, expect, jest, it } from '@jest/globals'; | ||
import { HEART_BIT_CONFIG } from '../constants'; | ||
import { mintData, unsignedMintData } from './mockData'; | ||
|
||
jest.mock('../utils', () => ({ | ||
getMinterContract: jest.fn().mockImplementation(() => { | ||
return { | ||
totalSupply: jest.fn().mockResolvedValue('10' as never), | ||
hashTokenMap: jest.fn().mockResolvedValue('token1' as never), | ||
balanceOf: jest.fn().mockResolvedValue('5' as never), | ||
} | ||
}), | ||
})); | ||
|
||
global.fetch = jest.fn(() => | ||
Promise.resolve(new Response(JSON.stringify({ success: true }), { | ||
status: 200, | ||
headers: { 'Content-Type': 'application/json' }, | ||
})) | ||
); | ||
|
||
describe('HeartBitCore', () => { | ||
const chain = '0xaa36a7'; | ||
const rpcUrl = HEART_BIT_CONFIG[chain].publicRPCUrl; | ||
let core = new HeartBitCore({ chain, rpcUrl }); | ||
|
||
it('should throw an error if chain is not provided', () => { | ||
expect(() => new HeartBitCore({} as any)).toThrow("Chain is required"); | ||
}); | ||
|
||
it('should initialize correctly with the provided options', () => { | ||
expect(core).toBeDefined(); | ||
expect(getMinterContract).toHaveBeenCalledWith(chain, expect.anything()); | ||
}); | ||
|
||
it('should call fetch with the correct parameters on mintHeartBit', async () => { | ||
await core.mintHeartBit(mintData); | ||
|
||
expect(fetch).toHaveBeenCalledWith(`${HEART_BIT_CONFIG[chain].relayerUrl}/signed-mint`, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify(mintData), | ||
}); | ||
}); | ||
|
||
it('should call fetch with the correct parameters on unSignedMintHeartBit', async () => { | ||
|
||
const {apiKey, ...rest} = unsignedMintData | ||
await core.unSignedMintHeartBit(unsignedMintData); | ||
|
||
expect(fetch).toHaveBeenCalledWith(HEART_BIT_CONFIG[chain].relayerUrl, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'x-api-key': apiKey | ||
}, | ||
body: JSON.stringify(rest), | ||
}); | ||
}); | ||
|
||
it('should handle getTotalHeartBitCountByHash correctly', async () => { | ||
const hash = 'dummyHash'; | ||
const count = await core.getTotalHeartBitCountByHash({ hash }); | ||
expect(count).toBe(10); | ||
}); | ||
|
||
it('should handle getHeartbitHashTokenMap correctly', async () => { | ||
const hash = 'dummyHash'; | ||
const tokenMap = await core.getHeartbitHashTokenMap(hash); | ||
|
||
expect(tokenMap).toBe('token1'); | ||
}); | ||
|
||
it('should handle getHeartBitByUser correctly', async () => { | ||
const account = '0x123'; | ||
const hash = 'dummyHash'; | ||
const balance = await core.getHeartBitByUser({ account, hash }); | ||
|
||
expect(balance).toBe(5); | ||
}); | ||
}); |
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,14 @@ | ||
export const mintData = { | ||
message: "p6f7gr-5173.csb.app wants you to sign in with your Ethereum account:\n0x852bd84A4dcDc150648AE4e8D6D6e59b1797c86f\n\nHello World!\n\nURI: https://p6f7gr-5173.csb.app\nVersion: 1\nChain ID: undefined\nNonce: 6RnPMnIbRS1yQ02ZK\nIssued At: 2024-02-15T09:14:18.897Z", | ||
signature: "0xfff4ce944d488abf2f8b2fd80bf3f585cbd14bbafe37ffd8b88e69ad88bd0c86426898416eb2c9c4f913eaa01430f4cac405c5a5c53a6410652b56874b9bd45e1c", | ||
startTime: 1706898250, | ||
endTime: 1706898251, | ||
hash: "0xba0c379a9b364b41e69a6a00a8652e28cb7dda3ca684309b940807634919a940" | ||
}; | ||
export const unsignedMintData = { | ||
startTime: 1706898250, | ||
endTime: 1706898251, | ||
hash: "ipfs://cid", | ||
account: "0x852bd84A4dcDc150648AE4e8D6D6e59b1797c86f", | ||
apiKey: 'hello' | ||
}; |
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,14 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
globals: { | ||
'ts-jest': { | ||
useESM: true, | ||
tsconfig: { | ||
verbatimModuleSyntax: false, | ||
}, | ||
}, | ||
}, | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
transform: {}, | ||
}; |
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,109 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import React, { useEffect } from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import { HeartBitContext, HeartBitProvider, IHeartBitContext } from './components'; | ||
import { describe, expect, jest, it } from '@jest/globals'; | ||
import HeartBitCore from '@fileverse/heartbit-core'; // Adjust the import path | ||
import { HeartBitCoreOptions } from '.'; | ||
|
||
jest.mock('@fileverse/heartbit-core', () => ({ | ||
HeartBitCore: jest.fn().mockImplementation(() => { | ||
return { | ||
getHeartBitByUser: jest.fn(), | ||
getTotalHeartBitCountByHash: jest.fn(), | ||
mintHeartBit: jest.fn(), | ||
} | ||
}) | ||
})); | ||
|
||
describe('HeartBitProvider', () => { | ||
const coreOptions: HeartBitCoreOptions = { chain: "0xaa36a7" }; | ||
const core: any = { | ||
getHeartBitByUser: jest.fn(), | ||
getTotalHeartBitCountByHash: jest.fn(), | ||
mintHeartBit: jest.fn(), | ||
}; | ||
const testCases: [keyof IHeartBitContext, keyof any][] = [ | ||
['getTotalHeartMintsByUser', 'getHeartBitByUser'], | ||
['getTotalHeartBitByHash', 'getTotalHeartBitCountByHash'], | ||
['mintHeartBit', 'mintHeartBit'] | ||
]; | ||
|
||
it('initializes HeartBitCore with coreOptions', () => { | ||
render( | ||
<HeartBitProvider coreOptions={coreOptions}> | ||
<div>Testing</div> | ||
</HeartBitProvider> | ||
); | ||
|
||
expect(HeartBitCore).toHaveBeenCalledWith(coreOptions); | ||
}); | ||
|
||
it('provides context functions', () => { | ||
const TestComponent = () => { | ||
const context = React.useContext(HeartBitContext); | ||
|
||
expect(context).not.toBeNull(); | ||
expect(context?.getTotalHeartMintsByUser).toBeDefined(); | ||
expect(context?.getTotalHeartBitByHash).toBeDefined(); | ||
expect(context?.mintHeartBit).toBeDefined(); | ||
|
||
return <div>Testing</div>; | ||
}; | ||
|
||
render( | ||
<HeartBitProvider coreOptions={coreOptions}> | ||
<TestComponent /> | ||
</HeartBitProvider> | ||
); | ||
}); | ||
|
||
|
||
it.each(testCases)('calls %s through the provided context', async (contextFunction: keyof IHeartBitContext, coreFunction: any) => { | ||
const args = { id: 'test' }; | ||
const TestComponent = () => { | ||
const context = React.useContext(HeartBitContext); | ||
|
||
useEffect(() => { | ||
context?.[contextFunction](args); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Different functions have different args this will not run |
||
}, [context]); | ||
|
||
return <div>Testing</div>; | ||
}; | ||
|
||
render( | ||
<HeartBitProvider coreOptions={coreOptions}> | ||
<TestComponent /> | ||
</HeartBitProvider> | ||
); | ||
|
||
expect(core[coreFunction]).toHaveBeenCalledWith(args); | ||
}); | ||
|
||
it.each(testCases)('handles errors in %s', async (contextFunction, coreFunction) => { | ||
const error = new Error('Test Error'); | ||
core[coreFunction].mockRejectedValue(error); | ||
|
||
const args = { id: 'test' }; | ||
const TestComponent = () => { | ||
const context: any = React.useContext(HeartBitContext); | ||
|
||
useEffect(() => { | ||
const fetch = async () => { | ||
await expect(context?.[contextFunction](args)).rejects.toThrow(error); | ||
}; | ||
fetch(); | ||
}, [context]); | ||
|
||
return <div>Testing</div>; | ||
}; | ||
|
||
render( | ||
<HeartBitProvider coreOptions={coreOptions}> | ||
<TestComponent /> | ||
</HeartBitProvider> | ||
); | ||
|
||
expect(core[coreFunction]).toHaveBeenCalledWith(args); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Wrong import