This document describes the comprehensive unit tests implemented for the H1B Data Viewer's data processing functionality. The tests cover three main areas as specified in task 2.3:
- Date parsing functionality
- Salary normalization across different pay units
- Error handling for malformed data
h1b-data-viewer-tests.html- Standalone HTML test runner with visual interfacetest-data-processing.js- Node.js command-line test runner- Integrated tests in
h1b-data-viewer.html- Tests embedded in the main application
A lightweight custom test framework was implemented with the following features:
describe()andit()functions for organizing testsexpect()assertion library with common matchers- Console output with colored results (✅ pass, ❌ fail)
- Test summary statistics
- Error reporting with detailed messages
Purpose: Verify that the parseDate() method correctly handles various date formats and edge cases.
-
Parse valid ISO date strings (
2023-01-15)- Verifies correct Date object creation
- Checks year, month, and day extraction
- Uses UTC date to avoid timezone issues
-
Parse MM/DD/YYYY format dates (
01/15/2023)- Tests alternative date format support
- Ensures consistent year parsing
-
Return null for empty date strings
- Tests handling of
'',null,undefined,'N/A' - Ensures graceful handling of missing data
- Tests handling of
-
Return null for invalid date strings
- Tests malformed dates like
'invalid-date','not-a-date' - Verifies error handling without throwing exceptions
- Tests malformed dates like
-
Handle various date formats
- Tests multiple valid formats: ISO, US format, long format, ISO with time
- Ensures broad compatibility with different data sources
Purpose: Verify that the normalizeSalary() method correctly converts different pay units to annual salaries.
-
Normalize hourly wages to annual salary
- Input:
'50','Hour'→ Expected:104000(50 × 2080 hours) - Tests standard full-time hour calculation
- Input:
-
Normalize monthly wages to annual salary
- Input:
'5000','Month'→ Expected:60000(5000 × 12 months) - Tests monthly to annual conversion
- Input:
-
Normalize weekly wages to annual salary
- Input:
'1000','Week'→ Expected:52000(1000 × 52 weeks) - Tests weekly to annual conversion
- Input:
-
Keep annual wages unchanged
- Input:
'75000','Year'→ Expected:75000 - Ensures annual salaries pass through unchanged
- Input:
-
Handle bi-weekly wages
- Input:
'2000','Bi-Weekly'→ Expected:52000(2000 × 26 periods) - Tests bi-weekly pay period conversion
- Input:
-
Return 0 for invalid wage amounts
- Tests
'',null,'invalid',undefinedinputs - Ensures graceful handling of malformed salary data
- Tests
-
Handle case-insensitive wage units
- Tests
'HOUR','hour','Hour','hourly' - Ensures consistent behavior regardless of case
- Tests
-
Default to annual for unknown wage units
- Input:
'75000','unknown-unit'→ Expected:75000 - Tests fallback behavior for unrecognized units
- Input:
-
Handle decimal wage amounts
- Input:
'25.50','Hour'→ Expected:53040(25.50 × 2080, rounded) - Tests floating-point salary calculations
- Input:
Purpose: Verify that the system gracefully handles invalid, incomplete, or malformed H1B records.
-
Validate required fields and return errors
- Tests empty record
{} - Expects errors for missing: case number, employer name, job title, case status
- Verifies comprehensive validation
- Tests empty record
-
Return no errors for valid record
- Tests complete valid record with all required fields
- Ensures validation passes for good data
-
Handle partial validation errors
- Tests record with only some required fields
- Verifies specific missing field identification
-
Process data with mixed valid and invalid records
- Tests array with both valid and invalid records
- Ensures processing continues despite some bad records
- Verifies correct handling of invalid salary and date data
-
Handle empty or null input data
- Tests
[],null, andundefinedinput arrays - Ensures system doesn't crash with no data
- Tests
-
Handle records with null or undefined field values
- Tests record with
null/undefinedsalary and date fields - Verifies graceful degradation with missing field data
- Tests record with
Purpose: Verify that the complete data processing pipeline works correctly.
-
Process complete H1B record correctly
- Tests end-to-end processing of a realistic H1B record
- Verifies all processing steps work together
- Checks salary normalization, date parsing, and data structure
-
Maintain original data while adding processed fields
- Ensures original record fields are preserved
- Verifies new processed fields are added correctly
- Tests data integrity throughout processing
- Browser Console: Open
h1b-data-viewer.htmland runrunDataProcessingTests() - Browser UI: Click the "Run Tests" button in the application
- Node.js: Run
node test-data-processing.js - Standalone: Open
h1b-data-viewer-tests.htmlfor visual test runner
All tests pass with 100% success rate:
- Total Tests: 20+ comprehensive test cases
- Coverage: Date parsing, salary normalization, error handling, integration
- Validation: Requirements 1.1 and 6.1 fully tested
- Date parsing tests ensure proper handling of H1B date fields
- Integration tests verify complete record processing
- Error handling tests ensure robust data loading
- Salary normalization tests ensure accurate wage calculations
- Statistical processing tests verify data aggregation accuracy
- Validation tests ensure data quality for statistics
- Reliability: Comprehensive test coverage ensures data processing accuracy
- Maintainability: Tests catch regressions during code changes
- Documentation: Tests serve as executable specifications
- Debugging: Failed tests pinpoint exact issues
- Confidence: High test coverage enables safe refactoring
- Performance Tests: Add tests for large dataset processing
- Browser Compatibility: Test across different browsers
- Edge Cases: Add more exotic date/salary format tests
- Mock Data: Create comprehensive test datasets
- Automated CI: Integrate tests into continuous integration pipeline