This directory contains comprehensive test coverage for the docx_viewer package.
test/
├── docx_viewer_test.dart # Main test entry point
├── fixtures/
│ └── test_docx_generator.dart # Helper to generate test DOCX files
├── src/
│ ├── docx_view_test.dart # Widget tests for DocxView
│ ├── extract_text_from_docx_test.dart # Tests for text extraction
│ ├── file_io_stub_test.dart # Tests for stub implementation
│ └── file_io_web_test.dart # Tests for web implementation
└── utils/
└── support_type_test.dart # Tests for utility classes
- ✅ Extract text from simple DOCX files
- ✅ Extract text from DOCX with multiple paragraphs
- ✅ Handle empty DOCX documents
- ✅ Extract and number items from DOCX with numbering
- ✅ Handle special characters and unicode
- ✅ Handle invalid ZIP/DOCX data
- ✅ Handle empty paragraphs and whitespace
- ✅ Handle long text content
- ✅ Test FirstOrNullExtension utility
- ✅ Display loading indicator during content load
- ✅ Display content after loading with bytes parameter
- ✅ Apply custom font size
- ✅ Use default font size when not specified
- ✅ Display multiple paragraphs with newlines
- ✅ Handle empty documents
- ✅ Call onError callback when no input provided
- ✅ Display error messages without callback
- ✅ Validate error when both filePath and bytes provided
- ✅ Handle invalid bytes gracefully
- ✅ Render content in scrollable view
- ✅ Handle numbered lists
- ✅ Apply correct padding
- ✅ Stub implementation tests (
src/file_io_stub_test.dart) - ✅ Web implementation tests (
src/file_io_web_test.dart) - ✅ Verify proper error messages for unsupported operations
- ✅ Validate Supporttype constants
flutter testflutter test --coverageflutter test test/src/docx_view_test.dartAfter running tests with coverage, you can generate an HTML report:
# Install lcov (Ubuntu/Debian)
sudo apt-get install lcov
# Generate HTML report
genhtml coverage/lcov.info -o coverage/html
# Open in browser
open coverage/html/index.htmlThe fixtures/test_docx_generator.dart file provides helper methods to generate test DOCX files:
createSimpleDocx(String text)- Creates a simple DOCX with given textcreateDocxWithNumbering(List<String> items)- Creates DOCX with numbered listcreateEmptyDocx()- Creates an empty DOCX filecreateDocxWithMultipleParagraphs(List<String> paragraphs)- Creates DOCX with multiple paragraphs
These helpers create proper DOCX files (ZIP archives) with the correct XML structure for testing.
Tests are automatically run on every pull request through GitHub Actions (.github/workflows/ci.yml):
- Analyze Job: Runs static analysis and formatting checks
- Test Job: Runs all tests with coverage
- Generates coverage report
- Posts coverage summary as PR comment
- Uploads coverage artifacts
The CI workflow:
- ✅ Runs on pull requests to
mainanddevbranches - ✅ Runs on push to
mainanddevbranches - ✅ Generates test coverage reports
- ✅ Comments on PRs with coverage information
- ✅ Provides coverage badges
When adding new tests:
- Create test files in appropriate directories (
src/,utils/, etc.) - Follow the existing test structure and naming conventions
- Use descriptive test names that explain what is being tested
- Include arrange-act-assert comments in tests for clarity
- Import the test file in
docx_viewer_test.dartto include in the main test suite - Run tests locally before committing
Example:
import 'package:flutter_test/flutter_test.dart';
void main() {
group('FeatureName', () {
test('should do something specific', () {
// Arrange
final input = 'test';
// Act
final result = functionUnderTest(input);
// Assert
expect(result, equals('expected'));
});
});
}- Isolation: Each test should be independent and not rely on other tests
- Clarity: Use descriptive test names that explain the scenario
- Coverage: Aim for high coverage but focus on meaningful tests
- Edge Cases: Test boundary conditions, error cases, and edge cases
- Maintainability: Keep tests simple and maintainable
- Performance: Tests should run quickly to support rapid development
The package aims for:
- Minimum: 80% code coverage
- Target: 90%+ code coverage
- Focus: All critical paths and error handling must be tested
Current coverage is tracked automatically in CI and reported on pull requests.