Testing Documentation
This document outlines the testing strategy and testing practices for the Neo Rust Website.
Testing Strategyโ
The website uses Jest and React Testing Library for testing components and functionality. Our testing approach follows these principles:
- Component-focused testing: Each UI component should have its own test file.
- Behavior-driven: Tests focus on behavior rather than implementation details.
- User-centric: Tests should simulate user interactions and verify expected outcomes.
- Mocking external dependencies: External services and APIs are mocked to ensure tests are reliable and fast.
Test Files Structureโ
Tests are located in the src/__tests__
directory and follow a naming convention:
ComponentName.test.tsx
- for component tests
Types of Testsโ
Unit Testsโ
Test individual components in isolation, mocking any dependencies.
Integration Testsโ
Test interactions between multiple components or functionalities.
Blockchain Data Testsโ
Special tests for blockchain data components like BlockchainInfo.test.tsx
that:
- Mock API responses
- Test loading states
- Test error handling
- Verify periodic updates
Running Testsโ
# Run all tests
npm test
# Run tests with coverage reporting
npm run test:coverage
# Run tests in watch mode
npm test:watch
# Run a specific test file
npm run test:file src/__tests__/ComponentName.test.tsx
Mockingโ
The tests use Jest's mocking capabilities to mock external dependencies:
- Axios for API requests
- Gatsby components (Link, StaticImage)
- Third-party components (Particles, etc.)
Testing the Blockchain Info Componentโ
The blockchain info component on the homepage displays live data from the Neo blockchain. The BlockchainInfo.test.tsx
file provides comprehensive tests for this component, including:
- Initial loading state: Tests that loading indicators are shown while data is being fetched.
- Data display: Tests that blockchain data is correctly displayed after fetching.
- Refresh functionality: Tests that clicking the refresh button triggers a new data fetch.
- Error handling: Tests the component's behavior when API requests fail.
- Automatic updates: Tests that the component updates data automatically on the specified interval.
- Cleanup: Tests that intervals are properly cleaned up when the component unmounts.
Example Blockchain Info Testโ
it('fetches and displays blockchain information', async () => {
render(<IndexPage />);
// Wait for data to be loaded and displayed
await screen.findByText('12,344'); // Block height
// Verify all blockchain info is displayed
expect(screen.getByText('12,344')).toBeInTheDocument();
expect(screen.getByText('123456...abcdef')).toBeInTheDocument();
expect(screen.getByText('10')).toBeInTheDocument();
expect(screen.getByText('/Neo:3.5.0/')).toBeInTheDocument();
// Verify the API calls were made
expect(mockAxios.post).toHaveBeenCalledTimes(3);
});
Writing New Testsโ
When writing new tests, follow these guidelines:
- Create a test file in the
src/__tests__
directory - Import necessary testing utilities and the component to test
- Mock any external dependencies
- Structure your tests with
describe
andit
blocks - Use
render
to mount your component - Query elements using
screen
methods - Use
fireEvent
to simulate user interactions - Use
act
for asynchronous operations - Make assertions with
expect
Continuous Integrationโ
All tests run automatically in the CI pipeline. A test failure will prevent deployment.