-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathh1b-data-viewer-tests.html
More file actions
1349 lines (1143 loc) · 57.8 KB
/
Copy pathh1b-data-viewer-tests.html
File metadata and controls
1349 lines (1143 loc) · 57.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>H1B Data Viewer - Unit Tests</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
margin: 2rem;
background-color: #f9fafb;
}
.test-container {
max-width: 1200px;
margin: 0 auto;
background: white;
border-radius: 8px;
padding: 2rem;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}
h1 {
color: #1f2937;
margin-bottom: 2rem;
}
.test-suite {
margin-bottom: 2rem;
border: 1px solid #e5e7eb;
border-radius: 6px;
overflow: hidden;
}
.test-suite-header {
background: #f3f4f6;
padding: 1rem;
font-weight: 600;
color: #374151;
}
.test-case {
padding: 1rem;
border-bottom: 1px solid #f3f4f6;
display: flex;
justify-content: space-between;
align-items: center;
}
.test-case:last-child {
border-bottom: none;
}
.test-name {
flex: 1;
}
.test-result {
padding: 0.25rem 0.75rem;
border-radius: 4px;
font-size: 0.875rem;
font-weight: 600;
}
.test-pass {
background: #dcfce7;
color: #16a34a;
}
.test-fail {
background: #fee2e2;
color: #dc2626;
}
.test-error {
background: #fef3c7;
color: #d97706;
}
.summary {
background: #f9fafb;
padding: 1rem;
border-radius: 6px;
margin-top: 2rem;
display: flex;
justify-content: space-between;
align-items: center;
}
.summary-stats {
display: flex;
gap: 2rem;
}
.summary-stat {
text-align: center;
}
.summary-stat .value {
font-size: 1.5rem;
font-weight: 700;
color: #1f2937;
}
.summary-stat .label {
font-size: 0.875rem;
color: #6b7280;
}
.run-tests-btn {
background: #2563eb;
color: white;
border: none;
padding: 0.75rem 1.5rem;
border-radius: 6px;
font-weight: 600;
cursor: pointer;
}
.run-tests-btn:hover {
background: #1d4ed8;
}
.error-details {
margin-top: 0.5rem;
padding: 0.5rem;
background: #fef2f2;
border-radius: 4px;
font-size: 0.875rem;
color: #dc2626;
font-family: monospace;
}
</style>
</head>
<body>
<div class="test-container">
<h1>H1B Data Viewer - Unit Tests</h1>
<div class="summary">
<div class="summary-stats">
<div class="summary-stat">
<div class="value" id="totalTests">0</div>
<div class="label">Total Tests</div>
</div>
<div class="summary-stat">
<div class="value" id="passedTests">0</div>
<div class="label">Passed</div>
</div>
<div class="summary-stat">
<div class="value" id="failedTests">0</div>
<div class="label">Failed</div>
</div>
</div>
<button class="run-tests-btn" onclick="runAllTests()">Run All Tests</button>
</div>
<div id="testResults">
<!-- Test results will be populated here -->
</div>
</div>
<script>
/**
* DataManager class for handling H1B data operations
* This is a copy of the DataManager from the main application for testing
*/
class DataManager {
constructor(rawData = []) {
this.rawData = rawData;
this.processedData = [];
this.filteredData = [];
this.currentFilters = {
employer: '',
status: '',
jobTitle: '',
minSalary: null,
maxSalary: null,
searchTerm: ''
};
this.currentSort = {
column: null,
direction: 'asc'
};
if (rawData.length > 0) {
this.processData();
}
}
/**
* Parse date strings into Date objects
* Handles various date formats commonly found in H1B data
*/
parseDate(dateString) {
if (!dateString || dateString === '' || dateString === 'N/A') {
return null;
}
try {
// Handle common date formats: YYYY-MM-DD, MM/DD/YYYY, etc.
const date = new Date(dateString);
// Check if date is valid
if (isNaN(date.getTime())) {
return null;
}
return date;
} catch (error) {
console.warn(`Failed to parse date: ${dateString}`, error);
return null;
}
}
/**
* Normalize salary amounts to annual values
* Converts hourly, monthly, etc. to yearly amounts
*/
normalizeSalary(wageAmount, wageUnit) {
if (!wageAmount || isNaN(parseFloat(wageAmount))) {
return 0;
}
const amount = parseFloat(wageAmount);
const unit = (wageUnit || '').toLowerCase().trim();
// Conversion factors to annual salary
const conversionFactors = {
'year': 1,
'yearly': 1,
'annual': 1,
'month': 12,
'monthly': 12,
'week': 52,
'weekly': 52,
'hour': 2080, // 40 hours/week * 52 weeks
'hourly': 2080,
'bi-weekly': 26,
'biweekly': 26
};
// Find matching conversion factor
for (const [key, factor] of Object.entries(conversionFactors)) {
if (unit.includes(key)) {
return Math.round(amount * factor);
}
}
// Default to treating as annual if unit is unclear
return Math.round(amount);
}
/**
* Validate a single H1B record
* Returns validation errors if any
*/
validateRecord(record) {
const errors = [];
if (!record.CASE_NUMBER) {
errors.push('Missing case number');
}
if (!record.EMPLOYER_NAME) {
errors.push('Missing employer name');
}
if (!record.JOB_TITLE) {
errors.push('Missing job title');
}
if (!record.CASE_STATUS) {
errors.push('Missing case status');
}
return errors;
}
/**
* Process and validate raw H1B data
* Normalizes dates, salaries, and other fields
*/
processData() {
this.processedData = this.rawData.map(record => {
const processed = { ...record };
// Parse and normalize dates
processed.RECEIVED_DATE_PARSED = this.parseDate(record.RECEIVED_DATE);
processed.DECISION_DATE_PARSED = this.parseDate(record.DECISION_DATE);
processed.BEGIN_DATE_PARSED = this.parseDate(record.BEGIN_DATE);
processed.END_DATE_PARSED = this.parseDate(record.END_DATE);
// Normalize salary to annual amount
processed.ANNUAL_SALARY = this.normalizeSalary(
record.WAGE_RATE_OF_PAY_FROM,
record.WAGE_UNIT_OF_PAY
);
return processed;
});
// Initialize filtered data with all processed data
this.filteredData = [...this.processedData];
}
}
/**
* Simple test framework
*/
class TestFramework {
constructor() {
this.tests = [];
this.results = [];
}
describe(suiteName, testFunction) {
const suite = {
name: suiteName,
tests: []
};
const context = {
it: (testName, testFunction) => {
suite.tests.push({
name: testName,
function: testFunction
});
}
};
testFunction.call(context, context);
this.tests.push(suite);
}
expect(actual) {
return {
toBe: (expected) => {
if (actual !== expected) {
throw new Error(`Expected ${expected}, but got ${actual}`);
}
},
toEqual: (expected) => {
if (JSON.stringify(actual) !== JSON.stringify(expected)) {
throw new Error(`Expected ${JSON.stringify(expected)}, but got ${JSON.stringify(actual)}`);
}
},
toBeNull: () => {
if (actual !== null) {
throw new Error(`Expected null, but got ${actual}`);
}
},
toBeInstanceOf: (expectedClass) => {
if (!(actual instanceof expectedClass)) {
throw new Error(`Expected instance of ${expectedClass.name}, but got ${typeof actual}`);
}
},
toBeGreaterThan: (expected) => {
if (actual <= expected) {
throw new Error(`Expected ${actual} to be greater than ${expected}`);
}
},
toContain: (expected) => {
if (!actual.includes(expected)) {
throw new Error(`Expected ${actual} to contain ${expected}`);
}
},
toHaveLength: (expected) => {
if (actual.length !== expected) {
throw new Error(`Expected length ${expected}, but got ${actual.length}`);
}
}
};
}
runTests() {
this.results = [];
let totalTests = 0;
let passedTests = 0;
let failedTests = 0;
this.tests.forEach(suite => {
const suiteResult = {
name: suite.name,
tests: []
};
suite.tests.forEach(test => {
totalTests++;
try {
test.function.call(this);
suiteResult.tests.push({
name: test.name,
status: 'pass'
});
passedTests++;
} catch (error) {
suiteResult.tests.push({
name: test.name,
status: 'fail',
error: error.message
});
failedTests++;
}
});
this.results.push(suiteResult);
});
return {
total: totalTests,
passed: passedTests,
failed: failedTests,
results: this.results
};
}
}
// Initialize test framework
const testFramework = new TestFramework();
/**
* Date Parsing Tests
*/
testFramework.describe('Date Parsing Functionality', function() {
this.it('should parse valid ISO date strings', function() {
const dataManager = new DataManager();
const result = dataManager.parseDate('2023-01-15');
testFramework.expect(result).toBeInstanceOf(Date);
testFramework.expect(result.getFullYear()).toBe(2023);
testFramework.expect(result.getMonth()).toBe(0); // January is 0
testFramework.expect(result.getDate()).toBe(15);
});
this.it('should parse MM/DD/YYYY format dates', function() {
const dataManager = new DataManager();
const result = dataManager.parseDate('01/15/2023');
testFramework.expect(result).toBeInstanceOf(Date);
testFramework.expect(result.getFullYear()).toBe(2023);
});
this.it('should return null for empty date strings', function() {
const dataManager = new DataManager();
testFramework.expect(dataManager.parseDate('')).toBeNull();
testFramework.expect(dataManager.parseDate(null)).toBeNull();
testFramework.expect(dataManager.parseDate(undefined)).toBeNull();
testFramework.expect(dataManager.parseDate('N/A')).toBeNull();
});
this.it('should return null for invalid date strings', function() {
const dataManager = new DataManager();
testFramework.expect(dataManager.parseDate('invalid-date')).toBeNull();
testFramework.expect(dataManager.parseDate('2023-13-45')).toBeNull();
testFramework.expect(dataManager.parseDate('not-a-date')).toBeNull();
});
this.it('should handle various date formats', function() {
const dataManager = new DataManager();
// Test different valid formats
const formats = [
'2023-12-25',
'12/25/2023',
'December 25, 2023',
'2023-12-25T00:00:00Z'
];
formats.forEach(format => {
const result = dataManager.parseDate(format);
testFramework.expect(result).toBeInstanceOf(Date);
testFramework.expect(result.getFullYear()).toBe(2023);
});
});
});
/**
* Salary Normalization Tests
*/
testFramework.describe('Salary Normalization Functionality', function() {
this.it('should normalize hourly wages to annual salary', function() {
const dataManager = new DataManager();
const result = dataManager.normalizeSalary('50', 'Hour');
testFramework.expect(result).toBe(104000); // 50 * 2080 hours
});
this.it('should normalize monthly wages to annual salary', function() {
const dataManager = new DataManager();
const result = dataManager.normalizeSalary('5000', 'Month');
testFramework.expect(result).toBe(60000); // 5000 * 12 months
});
this.it('should normalize weekly wages to annual salary', function() {
const dataManager = new DataManager();
const result = dataManager.normalizeSalary('1000', 'Week');
testFramework.expect(result).toBe(52000); // 1000 * 52 weeks
});
this.it('should keep annual wages unchanged', function() {
const dataManager = new DataManager();
const result = dataManager.normalizeSalary('75000', 'Year');
testFramework.expect(result).toBe(75000);
});
this.it('should handle bi-weekly wages', function() {
const dataManager = new DataManager();
const result = dataManager.normalizeSalary('2000', 'Bi-Weekly');
testFramework.expect(result).toBe(52000); // 2000 * 26 bi-weekly periods
});
this.it('should return 0 for invalid wage amounts', function() {
const dataManager = new DataManager();
testFramework.expect(dataManager.normalizeSalary('', 'Hour')).toBe(0);
testFramework.expect(dataManager.normalizeSalary(null, 'Hour')).toBe(0);
testFramework.expect(dataManager.normalizeSalary('invalid', 'Hour')).toBe(0);
testFramework.expect(dataManager.normalizeSalary(undefined, 'Hour')).toBe(0);
});
this.it('should handle case-insensitive wage units', function() {
const dataManager = new DataManager();
testFramework.expect(dataManager.normalizeSalary('50', 'HOUR')).toBe(104000);
testFramework.expect(dataManager.normalizeSalary('50', 'hour')).toBe(104000);
testFramework.expect(dataManager.normalizeSalary('50', 'Hour')).toBe(104000);
testFramework.expect(dataManager.normalizeSalary('50', 'hourly')).toBe(104000);
});
this.it('should default to annual for unknown wage units', function() {
const dataManager = new DataManager();
const result = dataManager.normalizeSalary('75000', 'unknown-unit');
testFramework.expect(result).toBe(75000);
});
this.it('should handle decimal wage amounts', function() {
const dataManager = new DataManager();
const result = dataManager.normalizeSalary('25.50', 'Hour');
testFramework.expect(result).toBe(53040); // 25.50 * 2080, rounded
});
});
/**
* Error Handling Tests for Malformed Data
*/
testFramework.describe('Error Handling for Malformed Data', function() {
this.it('should validate required fields and return errors', function() {
const dataManager = new DataManager();
const malformedRecord = {
// Missing required fields
};
const errors = dataManager.validateRecord(malformedRecord);
testFramework.expect(errors).toContain('Missing case number');
testFramework.expect(errors).toContain('Missing employer name');
testFramework.expect(errors).toContain('Missing job title');
testFramework.expect(errors).toContain('Missing case status');
});
this.it('should return no errors for valid record', function() {
const dataManager = new DataManager();
const validRecord = {
CASE_NUMBER: 'I-200-12345-123456',
EMPLOYER_NAME: 'Test Company',
JOB_TITLE: 'Software Engineer',
CASE_STATUS: 'Certified'
};
const errors = dataManager.validateRecord(validRecord);
testFramework.expect(errors).toHaveLength(0);
});
this.it('should handle partial validation errors', function() {
const dataManager = new DataManager();
const partialRecord = {
CASE_NUMBER: 'I-200-12345-123456',
EMPLOYER_NAME: 'Test Company'
// Missing JOB_TITLE and CASE_STATUS
};
const errors = dataManager.validateRecord(partialRecord);
testFramework.expect(errors).toHaveLength(2);
testFramework.expect(errors).toContain('Missing job title');
testFramework.expect(errors).toContain('Missing case status');
});
this.it('should process data with mixed valid and invalid records', function() {
const mixedData = [
{
CASE_NUMBER: 'I-200-12345-123456',
EMPLOYER_NAME: 'Valid Company',
JOB_TITLE: 'Software Engineer',
CASE_STATUS: 'Certified',
WAGE_RATE_OF_PAY_FROM: '75000',
WAGE_UNIT_OF_PAY: 'Year',
RECEIVED_DATE: '2023-01-15',
DECISION_DATE: '2023-03-20'
},
{
CASE_NUMBER: 'I-200-12345-123457',
EMPLOYER_NAME: 'Another Company',
JOB_TITLE: 'Data Scientist',
CASE_STATUS: 'Denied',
WAGE_RATE_OF_PAY_FROM: 'invalid-salary',
WAGE_UNIT_OF_PAY: 'Hour',
RECEIVED_DATE: 'invalid-date',
DECISION_DATE: '2023-04-15'
}
];
const dataManager = new DataManager(mixedData);
testFramework.expect(dataManager.processedData).toHaveLength(2);
// First record should have valid processed data
testFramework.expect(dataManager.processedData[0].ANNUAL_SALARY).toBe(75000);
testFramework.expect(dataManager.processedData[0].RECEIVED_DATE_PARSED).toBeInstanceOf(Date);
// Second record should handle invalid data gracefully
testFramework.expect(dataManager.processedData[1].ANNUAL_SALARY).toBe(0);
testFramework.expect(dataManager.processedData[1].RECEIVED_DATE_PARSED).toBeNull();
});
this.it('should handle empty or null input data', function() {
const dataManager1 = new DataManager([]);
testFramework.expect(dataManager1.processedData).toHaveLength(0);
const dataManager2 = new DataManager(null);
testFramework.expect(dataManager2.rawData).toBeNull();
const dataManager3 = new DataManager();
testFramework.expect(dataManager3.rawData).toHaveLength(0);
});
this.it('should handle records with null or undefined field values', function() {
const recordWithNulls = {
CASE_NUMBER: 'I-200-12345-123456',
EMPLOYER_NAME: 'Test Company',
JOB_TITLE: 'Software Engineer',
CASE_STATUS: 'Certified',
WAGE_RATE_OF_PAY_FROM: null,
WAGE_UNIT_OF_PAY: undefined,
RECEIVED_DATE: null,
DECISION_DATE: undefined
};
const dataManager = new DataManager([recordWithNulls]);
testFramework.expect(dataManager.processedData).toHaveLength(1);
const processed = dataManager.processedData[0];
testFramework.expect(processed.ANNUAL_SALARY).toBe(0);
testFramework.expect(processed.RECEIVED_DATE_PARSED).toBeNull();
testFramework.expect(processed.DECISION_DATE_PARSED).toBeNull();
});
});
/**
* Integration Tests for Data Processing
*/
testFramework.describe('Data Processing Integration', function() {
this.it('should process complete H1B record correctly', function() {
const sampleRecord = {
CASE_NUMBER: 'I-200-12345-123456',
EMPLOYER_NAME: 'Tech Corp',
JOB_TITLE: 'Senior Software Engineer',
CASE_STATUS: 'Certified',
WAGE_RATE_OF_PAY_FROM: '65',
WAGE_UNIT_OF_PAY: 'Hour',
RECEIVED_DATE: '2023-01-15',
DECISION_DATE: '2023-03-20',
WORKSITE_CITY: 'San Francisco',
WORKSITE_STATE: 'CA'
};
const dataManager = new DataManager([sampleRecord]);
testFramework.expect(dataManager.processedData).toHaveLength(1);
const processed = dataManager.processedData[0];
testFramework.expect(processed.ANNUAL_SALARY).toBe(135200); // 65 * 2080
testFramework.expect(processed.RECEIVED_DATE_PARSED).toBeInstanceOf(Date);
testFramework.expect(processed.DECISION_DATE_PARSED).toBeInstanceOf(Date);
});
this.it('should maintain original data while adding processed fields', function() {
const originalRecord = {
CASE_NUMBER: 'I-200-12345-123456',
EMPLOYER_NAME: 'Test Company',
JOB_TITLE: 'Software Engineer',
CASE_STATUS: 'Certified'
};
const dataManager = new DataManager([originalRecord]);
const processed = dataManager.processedData[0];
// Original fields should be preserved
testFramework.expect(processed.CASE_NUMBER).toBe('I-200-12345-123456');
testFramework.expect(processed.EMPLOYER_NAME).toBe('Test Company');
testFramework.expect(processed.JOB_TITLE).toBe('Software Engineer');
testFramework.expect(processed.CASE_STATUS).toBe('Certified');
// New processed fields should be added
testFramework.expect(processed.hasOwnProperty('ANNUAL_SALARY')).toBe(true);
testFramework.expect(processed.hasOwnProperty('RECEIVED_DATE_PARSED')).toBe(true);
});
});
/**
* Table Rendering Tests
*/
testFramework.describe('Table Header Generation with Sort Indicators', function() {
this.it('should create table headers with correct attributes', function() {
// Create a mock table structure
const mockTable = document.createElement('table');
const thead = document.createElement('thead');
const headerRow = document.createElement('tr');
thead.appendChild(headerRow);
mockTable.appendChild(thead);
document.body.appendChild(mockTable);
// Mock the table renderer (simplified version for testing)
const columns = [
{ key: 'CASE_NUMBER', label: 'Case Number', sortable: true, type: 'text' },
{ key: 'EMPLOYER_NAME', label: 'Employer', sortable: true, type: 'text' },
{ key: 'CASE_STATUS', label: 'Status', sortable: true, type: 'status' }
];
// Clear existing headers
headerRow.innerHTML = '';
// Create header cells (mimicking TableRenderer.renderTableHeader)
columns.forEach(column => {
const th = document.createElement('th');
th.textContent = column.label;
th.setAttribute('data-column', column.key);
if (column.sortable) {
th.classList.add('sortable');
th.setAttribute('role', 'columnheader');
th.setAttribute('tabindex', '0');
th.setAttribute('aria-sort', 'none');
}
headerRow.appendChild(th);
});
// Test header generation
const headers = mockTable.querySelectorAll('th');
testFramework.expect(headers).toHaveLength(3);
// Test first header
testFramework.expect(headers[0].textContent).toBe('Case Number');
testFramework.expect(headers[0].getAttribute('data-column')).toBe('CASE_NUMBER');
testFramework.expect(headers[0].classList.contains('sortable')).toBe(true);
testFramework.expect(headers[0].getAttribute('role')).toBe('columnheader');
testFramework.expect(headers[0].getAttribute('aria-sort')).toBe('none');
// Test second header
testFramework.expect(headers[1].textContent).toBe('Employer');
testFramework.expect(headers[1].getAttribute('data-column')).toBe('EMPLOYER_NAME');
// Test third header
testFramework.expect(headers[2].textContent).toBe('Status');
testFramework.expect(headers[2].getAttribute('data-column')).toBe('CASE_STATUS');
// Cleanup
document.body.removeChild(mockTable);
});
this.it('should update sort indicators correctly', function() {
// Create mock table structure
const mockTable = document.createElement('table');
const thead = document.createElement('thead');
const headerRow = document.createElement('tr');
thead.appendChild(headerRow);
mockTable.appendChild(thead);
document.body.appendChild(mockTable);
// Create sortable headers
const th1 = document.createElement('th');
th1.classList.add('sortable');
th1.setAttribute('data-column', 'CASE_NUMBER');
th1.textContent = 'Case Number';
headerRow.appendChild(th1);
const th2 = document.createElement('th');
th2.classList.add('sortable');
th2.setAttribute('data-column', 'EMPLOYER_NAME');
th2.textContent = 'Employer';
headerRow.appendChild(th2);
// Mock sort state
const currentSort = { column: 'CASE_NUMBER', direction: 'asc' };
// Update sort indicators (mimicking TableRenderer.updateSortIndicators)
const headers = mockTable.querySelectorAll('th.sortable');
headers.forEach(header => {
const column = header.getAttribute('data-column');
// Remove existing sort classes
header.classList.remove('sort-asc', 'sort-desc');
// Add appropriate sort class if this is the active column
if (column === currentSort.column) {
header.classList.add(`sort-${currentSort.direction}`);
}
});
// Test sort indicators
testFramework.expect(th1.classList.contains('sort-asc')).toBe(true);
testFramework.expect(th1.classList.contains('sort-desc')).toBe(false);
testFramework.expect(th2.classList.contains('sort-asc')).toBe(false);
testFramework.expect(th2.classList.contains('sort-desc')).toBe(false);
// Test descending sort
currentSort.direction = 'desc';
headers.forEach(header => {
const column = header.getAttribute('data-column');
header.classList.remove('sort-asc', 'sort-desc');
if (column === currentSort.column) {
header.classList.add(`sort-${currentSort.direction}`);
}
});
testFramework.expect(th1.classList.contains('sort-desc')).toBe(true);
testFramework.expect(th1.classList.contains('sort-asc')).toBe(false);
// Cleanup
document.body.removeChild(mockTable);
});
this.it('should handle sort direction toggle correctly', function() {
let currentSort = { column: null, direction: 'asc' };
// Mock handleSort function logic
function handleSort(column) {
if (currentSort.column === column) {
currentSort.direction = currentSort.direction === 'asc' ? 'desc' : 'asc';
} else {
currentSort.column = column;
currentSort.direction = 'asc';
}
}
// Test initial sort
handleSort('CASE_NUMBER');
testFramework.expect(currentSort.column).toBe('CASE_NUMBER');
testFramework.expect(currentSort.direction).toBe('asc');
// Test toggle to descending
handleSort('CASE_NUMBER');
testFramework.expect(currentSort.column).toBe('CASE_NUMBER');
testFramework.expect(currentSort.direction).toBe('desc');
// Test toggle back to ascending
handleSort('CASE_NUMBER');
testFramework.expect(currentSort.column).toBe('CASE_NUMBER');
testFramework.expect(currentSort.direction).toBe('asc');
// Test new column sort
handleSort('EMPLOYER_NAME');
testFramework.expect(currentSort.column).toBe('EMPLOYER_NAME');
testFramework.expect(currentSort.direction).toBe('asc');
});
});
/**
* Pagination Controls Tests
*/
testFramework.describe('Pagination Controls with Different Data Sizes', function() {
this.it('should calculate pagination correctly for small datasets', function() {
const totalRecords = 25;
const pageSize = 50;
const totalPages = Math.ceil(totalRecords / pageSize);
testFramework.expect(totalPages).toBe(1);
// Test pagination info
const currentPage = 1;
const startRecord = (currentPage - 1) * pageSize + 1;
const endRecord = Math.min(currentPage * pageSize, totalRecords);
testFramework.expect(startRecord).toBe(1);
testFramework.expect(endRecord).toBe(25);
});
this.it('should calculate pagination correctly for large datasets', function() {
const totalRecords = 1250;
const pageSize = 50;
const totalPages = Math.ceil(totalRecords / pageSize);
testFramework.expect(totalPages).toBe(25);
// Test first page
let currentPage = 1;
let startRecord = (currentPage - 1) * pageSize + 1;
let endRecord = Math.min(currentPage * pageSize, totalRecords);
testFramework.expect(startRecord).toBe(1);
testFramework.expect(endRecord).toBe(50);
// Test middle page
currentPage = 10;
startRecord = (currentPage - 1) * pageSize + 1;
endRecord = Math.min(currentPage * pageSize, totalRecords);
testFramework.expect(startRecord).toBe(451);
testFramework.expect(endRecord).toBe(500);
// Test last page
currentPage = 25;
startRecord = (currentPage - 1) * pageSize + 1;
endRecord = Math.min(currentPage * pageSize, totalRecords);
testFramework.expect(startRecord).toBe(1201);
testFramework.expect(endRecord).toBe(1250);
});
this.it('should handle edge cases in pagination', function() {
// Test empty dataset
let totalRecords = 0;
let pageSize = 50;
let totalPages = Math.ceil(totalRecords / pageSize);
testFramework.expect(totalPages).toBe(0);
// Test single record
totalRecords = 1;
totalPages = Math.ceil(totalRecords / pageSize);
testFramework.expect(totalPages).toBe(1);
// Test exact page boundary
totalRecords = 100;
totalPages = Math.ceil(totalRecords / pageSize);
testFramework.expect(totalPages).toBe(2);
const currentPage = 2;
const startRecord = (currentPage - 1) * pageSize + 1;
const endRecord = Math.min(currentPage * pageSize, totalRecords);
testFramework.expect(startRecord).toBe(51);
testFramework.expect(endRecord).toBe(100);
});
this.it('should validate page navigation boundaries', function() {
const totalPages = 10;
let currentPage = 1;
// Mock handlePaginationClick logic
function handlePaginationClick(action, page) {
let newPage = currentPage;
switch (action) {
case 'first':
newPage = 1;
break;
case 'prev':
newPage = Math.max(1, currentPage - 1);
break;
case 'next':
newPage = Math.min(totalPages, currentPage + 1);
break;
case 'last':
newPage = totalPages;
break;
case 'page':
newPage = page;
break;
}
if (newPage !== currentPage && newPage >= 1 && newPage <= totalPages) {
currentPage = newPage;
return true;
}
return false;
}
// Test navigation from first page
testFramework.expect(handlePaginationClick('prev', null)).toBe(false); // Can't go before first
testFramework.expect(currentPage).toBe(1);
testFramework.expect(handlePaginationClick('next', null)).toBe(true);
testFramework.expect(currentPage).toBe(2);
// Test navigation to last page
testFramework.expect(handlePaginationClick('last', null)).toBe(true);
testFramework.expect(currentPage).toBe(10);
testFramework.expect(handlePaginationClick('next', null)).toBe(false); // Can't go past last
testFramework.expect(currentPage).toBe(10);
// Test direct page navigation
testFramework.expect(handlePaginationClick('page', 5)).toBe(true);
testFramework.expect(currentPage).toBe(5);
// Test invalid page navigation
testFramework.expect(handlePaginationClick('page', 0)).toBe(false);
testFramework.expect(handlePaginationClick('page', 11)).toBe(false);
testFramework.expect(currentPage).toBe(5); // Should remain unchanged
});
this.it('should create correct pagination info text', function() {
// Mock createPaginationInfo logic
function createPaginationInfo(currentPage, pageSize, totalRecords) {
const startRecord = (currentPage - 1) * pageSize + 1;