From af3eddae0a0adc000d82ed8fc95f2bc34021c6a4 Mon Sep 17 00:00:00 2001 From: Hiroki Terashima Date: Fri, 22 Nov 2024 08:31:27 -0800 Subject: [PATCH] test(SelectPeriodComponent): Add unit tests for period selection functionality --- .../select-period.component.spec.ts | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 src/assets/wise5/classroomMonitor/classroomMonitorComponents/select-period/select-period.component.spec.ts diff --git a/src/assets/wise5/classroomMonitor/classroomMonitorComponents/select-period/select-period.component.spec.ts b/src/assets/wise5/classroomMonitor/classroomMonitorComponents/select-period/select-period.component.spec.ts new file mode 100644 index 00000000000..9549a0098c7 --- /dev/null +++ b/src/assets/wise5/classroomMonitor/classroomMonitorComponents/select-period/select-period.component.spec.ts @@ -0,0 +1,103 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { MatSelectHarness } from '@angular/material/select/testing'; +import { NoopAnimationsModule } from '@angular/platform-browser/animations'; +import { HarnessLoader } from '@angular/cdk/testing'; +import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed'; +import { SelectPeriodComponent } from './select-period.component'; +import { ConfigService } from '../../../services/configService'; +import { TeacherDataService } from '../../../services/teacherDataService'; +import { WorkgroupService } from '../../../../../app/services/workgroup.service'; +import { of } from 'rxjs'; +describe('SelectPeriodComponent', () => { + let component: SelectPeriodComponent; + let fixture: ComponentFixture; + let loader: HarnessLoader; + let configService: jasmine.SpyObj; + let dataService: jasmine.SpyObj; + let workgroupService: jasmine.SpyObj; + let workgroupsInPeriod: Map; + + const mockPeriods = [ + { periodId: -1, periodName: 'All Periods' }, + { periodId: 1, periodName: 'Period 1' }, + { periodId: 2, periodName: 'Period 2' } + ]; + + beforeEach(async () => { + const configServiceSpy = jasmine.createSpyObj('ConfigService', [ + 'configRetrieved$', + 'isClassroomMonitor' + ]); + const dataServiceSpy = jasmine.createSpyObj('TeacherDataService', [ + 'getCurrentPeriod', + 'getPeriods', + 'currentPeriodChanged$', + 'setCurrentPeriod' + ]); + const workgroupServiceSpy = jasmine.createSpyObj('WorkgroupService', ['getWorkgroupsInPeriod']); + + await TestBed.configureTestingModule({ + imports: [NoopAnimationsModule, SelectPeriodComponent], + providers: [ + { provide: ConfigService, useValue: configServiceSpy }, + { provide: TeacherDataService, useValue: dataServiceSpy }, + { provide: WorkgroupService, useValue: workgroupServiceSpy } + ] + }).compileComponents(); + + configService = TestBed.inject(ConfigService) as jasmine.SpyObj; + dataService = TestBed.inject(TeacherDataService) as jasmine.SpyObj; + workgroupService = TestBed.inject(WorkgroupService) as jasmine.SpyObj; + + configService.isClassroomMonitor.and.returnValue(true); + configService.configRetrieved$ = of({}); + dataService.getCurrentPeriod.and.returnValue({ periodId: 1, periodName: 'Period 1' }); + dataService.getPeriods.and.returnValue(mockPeriods); + dataService.currentPeriodChanged$ = of({ + currentPeriod: { periodId: 1, periodName: 'Period 1' } + }); + workgroupsInPeriod = new Map(); + workgroupsInPeriod.set(1, 1); + workgroupsInPeriod.set(2, 2); + workgroupsInPeriod.set(3, 3); + workgroupService.getWorkgroupsInPeriod.and.returnValue(workgroupsInPeriod); + + fixture = TestBed.createComponent(SelectPeriodComponent); + loader = TestbedHarnessEnvironment.loader(fixture); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); + + it('should initialize with correct period data', async () => { + const select = await loader.getHarness(MatSelectHarness); + expect(await select.getValueText()).toBe('Period: Period 1'); + }); + + it('should update period when selection changes', async () => { + const select = await loader.getHarness(MatSelectHarness); + await select.open(); + const options = await select.getOptions(); + await options[2].click(); // Select Period 2 + expect(dataService.setCurrentPeriod).toHaveBeenCalledWith(mockPeriods[2]); + }); + + it('should hide All Periods option when only one period exists', () => { + dataService.getPeriods.and.returnValue([ + { periodId: -1, periodName: 'All Periods' }, + { periodId: 1, periodName: 'Period 1' } + ]); + component.ngOnInit(); + expect(component['periods'].length).toBe(1); + expect(component['periods'][0].periodId).toBe(1); + }); + + it('should calculate correct number of workgroups per period', () => { + component.ngOnInit(); + expect(component['periods'][1].numWorkgroupsInPeriod).toBe(3); + expect(component['periods'][0].numWorkgroupsInPeriod).toBe(6); // Total for all periods + }); +});