Skip to content

Commit

Permalink
chore: add client tests for the event settings component
Browse files Browse the repository at this point in the history
  • Loading branch information
kaancayli committed Nov 18, 2024
1 parent ba6c6fa commit 48903b8
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export class IrisEventSettingsUpdateComponent {
parentEventSettings = input<IrisEventSettings>();

isAdmin = signal(false);
inheritDisabled = computed(() => (this.parentEventSettings() !== undefined ? !this.parentEventSettings()!.enabled : false));
isSettingsSwitchDisabled = computed(() => (!this.isAdmin() && this.settingsType() !== IrisSettingsType.EXERCISE) || this.inheritDisabled());

// Computed properties
inheritDisabled = computed(() => (this.parentEventSettings() !== undefined ? !this.parentEventSettings()!.enabled : false));
isSettingsSwitchDisabled = computed(() => !this.isAdmin() || this.settingsType() === IrisSettingsType.EXERCISE || this.inheritDisabled() || this.proactivityDisabled());
enabled = computed(() => this.eventSettings().enabled);

// Constants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ describe('IrisCourseSettingsUpdateComponent Component', () => {
jest.restoreAllMocks();
});

it('should create IrisCourseSettingsUpdateComponent', () => {
expect(comp).toBeDefined();
});

it('Setup works correctly', () => {
fixture.detectChanges();
expect(paramsSpy).toHaveBeenCalledOnce();
Expand Down Expand Up @@ -96,5 +100,6 @@ describe('IrisCourseSettingsUpdateComponent Component', () => {
expect(comp.settingsUpdateComponent!.irisSettings.irisChatSettings).toBeTruthy();
expect(comp.settingsUpdateComponent!.irisSettings.irisLectureIngestionSettings).toBeTruthy();
expect(comp.settingsUpdateComponent!.irisSettings.irisCompetencyGenerationSettings).toBeTruthy();
expect(comp.settingsUpdateComponent!.irisSettings.irisProactivitySettings).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { IrisEventSettingsUpdateComponent } from '../../../../../../main/webapp/app/iris/settings/iris-settings-update/iris-event-settings-update/iris-event-settings-update.component';
import { AccountService } from '../../../../../../main/webapp/app/core/auth/account.service';
import { IrisEventSettings } from '../../../../../../main/webapp/app/entities/iris/settings/iris-event-settings.model';
import { IrisSettingsType } from '../../../../../../main/webapp/app/entities/iris/settings/iris-settings.model';
import { ArtemisTestModule } from '../../../test.module';
import { mockEvents } from './mock-settings';
import { ComponentRef } from '@angular/core';

describe('IrisEventSettingsUpdateComponent', () => {
let component: IrisEventSettingsUpdateComponent;
let fixture: ComponentFixture<IrisEventSettingsUpdateComponent>;
let componentRef: ComponentRef<IrisEventSettingsUpdateComponent>;
let accountServiceStub: Partial<AccountService>;

beforeEach(async () => {
accountServiceStub = {
isAdmin: jest.fn().mockReturnValue(true),
};

await TestBed.configureTestingModule({
imports: [ArtemisTestModule, IrisEventSettingsUpdateComponent],
declarations: [],
providers: [{ provide: AccountService, useValue: accountServiceStub }],
}).compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(IrisEventSettingsUpdateComponent);
component = fixture.componentInstance;
componentRef = fixture.componentRef;
const mockEventSettings = mockEvents();
componentRef.setInput('eventSettings', mockEventSettings[0]);
componentRef.setInput('proactivityDisabled', false);
componentRef.setInput('settingsType', IrisSettingsType.COURSE);
const parentEventSettings = { ...mockEventSettings[0] } as IrisEventSettings;
componentRef.setInput('parentEventSettings', parentEventSettings);
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});

it('should initialize isAdmin correctly', () => {
expect(component.isAdmin()).toBeTruthy();
});

it('should compute inheritDisabled correctly', () => {
let parentEventSettings = mockEvents()[0];
parentEventSettings.enabled = true;
componentRef.setInput('parentEventSettings', parentEventSettings);
fixture.detectChanges();

expect(component.inheritDisabled()).toBeFalsy();

parentEventSettings = mockEvents()[0];
parentEventSettings.enabled = false;
componentRef.setInput('parentEventSettings', parentEventSettings);
fixture.detectChanges();

expect(component.inheritDisabled()).toBeTruthy();
});

it('should compute isSettingsSwitchDisabled correctly', () => {
expect(component.isSettingsSwitchDisabled()).toBeFalsy();
// If the parent event settings are disabled, the switch should be
componentRef.setInput('proactivityDisabled', true);
fixture.detectChanges();
expect(component.isSettingsSwitchDisabled()).toBeTruthy();

componentRef.setInput('proactivityDisabled', false);
fixture.detectChanges();
expect(component.isSettingsSwitchDisabled()).toBeFalsy();

// If the settings type is exercise, the switch should be disabled
componentRef.setInput('settingsType', IrisSettingsType.EXERCISE);
fixture.detectChanges();
expect(component.isSettingsSwitchDisabled()).toBeTruthy();
});

it('should update event settings correctly', () => {
const newEventSettings = { ...mockEvents()[0] } as IrisEventSettings;
newEventSettings.enabled = false;
component.eventSettings.set({ ...newEventSettings } as IrisEventSettings);
fixture.detectChanges();
component.updateSetting('enabled', true);
fixture.detectChanges();
expect(component.eventSettings().enabled).toBeTruthy();
});

it('should render the correct button states based on enabled property', () => {
component.eventSettings.set({ enabled: true } as IrisEventSettings);
fixture.detectChanges();
const activeButton = fixture.debugElement.query(By.css('.btn-success.selected'));
const inactiveButton = fixture.debugElement.query(By.css('.btn-danger.selected'));
expect(activeButton).toBeTruthy();
expect(inactiveButton).toBeFalsy();

component.eventSettings.set({ enabled: false } as IrisEventSettings);
fixture.detectChanges();
expect(fixture.debugElement.query(By.css('.btn-success.selected'))).toBeFalsy();
expect(fixture.debugElement.query(By.css('.btn-danger.selected'))).toBeTruthy();
});

it('should disable buttons when isSettingsSwitchDisabled is true', () => {
componentRef.setInput('proactivityDisabled', true);
fixture.detectChanges();
expect(component.isSettingsSwitchDisabled()).toBeTruthy();
fixture.detectChanges();
const buttons = fixture.debugElement.queryAll(By.css('.btn'));
buttons.forEach((button) => {
expect(button.nativeElement.classList).toContain('disabled');
});
});

it('should call updateSetting when buttons are clicked', () => {
jest.spyOn(component, 'updateSetting');
const activeButton = fixture.debugElement.query(By.css('.btn-success'));

activeButton.triggerEventHandler('click', null);
fixture.detectChanges();

expect(component.updateSetting).toHaveBeenCalledWith('enabled', false);

const inactiveButton = fixture.debugElement.query(By.css('.btn-danger'));
inactiveButton.triggerEventHandler('click', null);
fixture.detectChanges();

expect(component.updateSetting).toHaveBeenCalledWith('enabled', true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ describe('IrisGlobalSettingsUpdateComponent Component', () => {
declarations: [
IrisGlobalSettingsUpdateComponent,
IrisSettingsUpdateComponent,
MockComponent(IrisCommonSubSettingsUpdateComponent),
MockComponent(IrisEventSettingsUpdateComponent),
MockComponent(IrisCommonSubSettingsUpdateComponent),
MockComponent(ButtonComponent),
MockDirective(NgModel),
],
Expand All @@ -41,6 +41,7 @@ describe('IrisGlobalSettingsUpdateComponent Component', () => {
const irisSettings = mockSettings();
getSettingsSpy = jest.spyOn(irisSettingsService, 'getGlobalSettings').mockReturnValue(of(irisSettings));
});
TestBed.createComponent(IrisEventSettingsUpdateComponent);
fixture = TestBed.createComponent(IrisGlobalSettingsUpdateComponent);
comp = fixture.componentInstance;
});
Expand All @@ -49,6 +50,10 @@ describe('IrisGlobalSettingsUpdateComponent Component', () => {
jest.restoreAllMocks();
});

it('should create IrisGlobalSettingsUpdateComponent', () => {
expect(comp).toBeDefined();
});

it('Setup works correctly', () => {
fixture.detectChanges();
expect(comp.settingsUpdateComponent).toBeTruthy();
Expand All @@ -70,6 +75,7 @@ describe('IrisGlobalSettingsUpdateComponent Component', () => {
fixture.detectChanges();
const irisSettings = mockSettings();
irisSettings.id = undefined;
irisSettings.irisProactivitySettings!.eventSettings.forEach((eventSetting) => (eventSetting.id = undefined));
const irisSettingsSaved = mockSettings();
const setSettingsSpy = jest.spyOn(irisSettingsService, 'setGlobalSettings').mockReturnValue(of(new HttpResponse<IrisSettings>({ body: irisSettingsSaved })));
comp.settingsUpdateComponent!.irisSettings = irisSettings;
Expand Down
26 changes: 26 additions & 0 deletions src/test/javascript/spec/component/iris/settings/mock-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,29 @@ export function mockVariants() {
},
] as IrisVariant[];
}

export function mockEvents() {
return [
{
id: 1,
enabled: true,
selectedEventVariant: 'jol',
sessionType: 'COURSE',
type: 'jol',
} as IrisEventSettings,
{
id: 2,
enabled: true,
selectedEventVariant: 'progress_stalled',
sessionType: 'COURSE',
type: 'progress_stalled',
} as IrisEventSettings,
{
id: 3,
enabled: true,
selectedEventVariant: 'build_failed',
sessionType: 'COURSE',
type: 'build_failed',
} as IrisEventSettings,
];
}

0 comments on commit 48903b8

Please sign in to comment.