-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
916482f
commit a725081
Showing
4 changed files
with
296 additions
and
38 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,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); | ||
}, [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.