Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 26 additions & 36 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,50 +1,40 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/blockchain/node_modules
/.pnp
# Dependency directories
node_modules/
.pnp/
.pnp.js

# vscode
.vscode

# hardhat
artifacts
cache
deployments
cache/
coverage*
blockchain/typechain-types/@chainlink
blockchain/typechain-types/@openzeppelin
blockchain/typechain-types/factories/@chainlink
blockchain/typechain-types/factories/@openzeppelin

# testing
/coverage
# Production build
/build
/dist

# next.js
# Next.js
/.next/
/out/

# production
/build
# Environment variables
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

# misc
.DS_Store
*.pem
# Testing
/coverage
*.lcov

# debug
# Logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
# Temporary files
.DS_Store
*.pem

# TypeScript
*.tsbuildinfo

# vercel
# Miscellaneous
.vercel
.idea/
.vscode/
74 changes: 74 additions & 0 deletions components/LotteryHistory.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import React, { useState, useEffect } from 'react';
import { useLotteryContext } from '../context/context';
import styles from '../styles/LotteryHistory.module.css';

interface LotteryRound {
id: number;
timestamp: number;
potSize: string;
winner: string;
}

const LotteryHistory: React.FC = () => {
const { lotteryContract } = useLotteryContext();
const [history, setHistory] = useState<LotteryRound[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);

useEffect(() => {
const fetchLotteryHistory = async () => {
try {
if (!lotteryContract) {
throw new Error('Lottery contract not initialized');
}

// Simulate fetching lottery history from the contract
// In a real implementation, replace with actual contract method
const rounds: LotteryRound[] = await lotteryContract.getPastRounds();

setHistory(rounds);
setLoading(false);
} catch (err) {
setError(err instanceof Error ? err.message : 'An unknown error occurred');
setLoading(false);
}
};

fetchLotteryHistory();
}, [lotteryContract]);

if (loading) return <div>Loading history...</div>;
if (error) return <div>Error: {error}</div>;

return (
<div className={styles.historyContainer} data-testid="lottery-history">
<h2>Lottery History</h2>
{history.length === 0 ? (
<p>No past lottery rounds yet.</p>
) : (
<table className={styles.historyTable}>
<thead>
<tr>
<th>Round</th>
<th>Date</th>
<th>Pot Size</th>
<th>Winner</th>
</tr>
</thead>
<tbody>
{history.map((round) => (
<tr key={round.id}>
<td>{round.id}</td>
<td>{new Date(round.timestamp * 1000).toLocaleDateString()}</td>
<td>{round.potSize} ETH</td>
<td>{round.winner.slice(0, 6)}...{round.winner.slice(-4)}</td>
</tr>
))}
</tbody>
</table>
)}
</div>
);
};

export default LotteryHistory;
81 changes: 81 additions & 0 deletions components/__tests__/LotteryHistory.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import React from 'react';
import { describe, it, expect, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import LotteryHistory from '../LotteryHistory';
import { LotteryContext } from '../../context/context';

// Mock context
const mockLotteryContract = {
getPastRounds: vi.fn().mockResolvedValue([
{
id: 1,
timestamp: Math.floor(Date.now() / 1000),
potSize: '10',
winner: '0x1234567890123456789012345678901234567890'
}
])
};

const mockContextValue = {
lotteryContract: mockLotteryContract,
// Add other mock context values as needed
};

describe('LotteryHistory Component', () => {
it('renders loading state initially', async () => {
render(
<LotteryContext.Provider value={mockContextValue}>
<LotteryHistory />
</LotteryContext.Provider>
);

expect(screen.getByText('Loading history...')).toBeTruthy();
});

it('renders lottery history table when data is available', async () => {
render(
<LotteryContext.Provider value={mockContextValue}>
<LotteryHistory />
</LotteryContext.Provider>
);

// Wait for loading to complete and table to render
await screen.findByTestId('lottery-history');

expect(screen.getByText('Lottery History')).toBeTruthy();
expect(screen.getByText('Round')).toBeTruthy();
expect(screen.getByText('Date')).toBeTruthy();
expect(screen.getByText('Pot Size')).toBeTruthy();
expect(screen.getByText('Winner')).toBeTruthy();
});

it('handles empty history gracefully', async () => {
const emptyMockContract = {
...mockLotteryContract,
getPastRounds: vi.fn().mockResolvedValue([])
};

render(
<LotteryContext.Provider value={{...mockContextValue, lotteryContract: emptyMockContract}}>
<LotteryHistory />
</LotteryContext.Provider>
);

await screen.findByText('No past lottery rounds yet.');
});

it('handles contract initialization error', async () => {
const errorMockContract = {
...mockLotteryContract,
getPastRounds: vi.fn().mockRejectedValue(new Error('Contract not initialized'))
};

render(
<LotteryContext.Provider value={{...mockContextValue, lotteryContract: errorMockContract}}>
<LotteryHistory />
</LotteryContext.Provider>
);

await screen.findByText(/Error:/);
});
});
Loading