Skip to content

Commit

Permalink
initial tests for react pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua-onwuzu committed Apr 8, 2024
1 parent 916482f commit a725081
Show file tree
Hide file tree
Showing 4 changed files with 296 additions and 38 deletions.
14 changes: 14 additions & 0 deletions packages/heartbit-react/jest.config.cjs
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: {},
};
13 changes: 10 additions & 3 deletions packages/heartbit-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@
"scripts": {
"dev": "vite build --watch",
"build": "tsc --project tsconfig.json && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0"
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"test": "jest"
},
"peerDependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@testing-library/react": "^14.2.2",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.6",
"@types/node-sass": "^4.11.7",
"@types/react": "^18.2.43",
Expand All @@ -48,14 +52,17 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"sass": "^1.70.0",
"tsup": "^8.0.2",
"typescript": "^5.2.2",
"vite": "^5.0.8",
"vite-plugin-css-injected-by-js": "^3.3.1",
"vite-plugin-dts": "^3.7.2",
"vite-plugin-node-polyfills": "^0.19.0"
},
"dependencies": {
"@fileverse/heartbit-core": "^2.4.0",
"classnames": "^2.5.1"
"@fileverse/heartbit-core": "workspace:^",
"classnames": "^2.5.1",
"jest": "^29.7.0",
"ts-jest": "^29.1.2"
}
}
109 changes: 109 additions & 0 deletions packages/heartbit-react/src/app.test.tsx
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);
});
});
Loading

0 comments on commit a725081

Please sign in to comment.