Skip to content

Commit

Permalink
Merge pull request #182 from theImmortalCoders/dev
Browse files Browse the repository at this point in the history
Release 1.9
  • Loading branch information
pablitoo1 authored Nov 28, 2024
2 parents ffeb7d7 + 1aa6c0d commit 0aac561
Show file tree
Hide file tree
Showing 24 changed files with 1,864 additions and 1,039 deletions.
27 changes: 21 additions & 6 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { RouterOutlet } from '@angular/router';
import { NavbarComponent } from './shared/components/navbar/navbar.component';
import { FooterComponent } from './shared/components/footer/footer.component';
import { NotificationComponent } from './shared/components/common/notification.component';
import { CookieConsentComponent } from './shared/components/common/cookie-consent.component';
import { CommonModule } from '@angular/common';

@Component({
selector: 'app-root',
Expand All @@ -12,16 +14,29 @@ import { NotificationComponent } from './shared/components/common/notification.c
NavbarComponent,
FooterComponent,
NotificationComponent,
CookieConsentComponent,
CommonModule,
],
template: `
<app-notification></app-notification>
<app-navbar />
<main class="max-w-full min-h-all overflow-x-hidden relative z-40">
<router-outlet />
</main>
<app-footer />
@if (!isCookiesAccepted) {
<app-cookie-consent />
}
<div
[ngClass]="{ 'filter blur-sm pointer-events-none': !isCookiesAccepted }">
<app-notification></app-notification>
<app-navbar />
<main class="max-w-full min-h-all overflow-x-hidden relative z-40">
<router-outlet />
</main>
<app-footer />
</div>
`,
})
export class AppComponent {
public title = 'RUT-AI GAMES 2';
public isCookiesAccepted = false;

public constructor() {
this.isCookiesAccepted = localStorage.getItem('cookiesAccepted') === 'true';
}
}
Original file line number Diff line number Diff line change
@@ -1,136 +1,144 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ReactiveFormsModule } from '@angular/forms';
import { of, throwError } from 'rxjs';
import { AdminSettingsComponent } from './admin-settings.component';
import { AdministrationEndpointsService } from '@endpoints/administration-endpoints.service';
import { NotificationService } from 'app/shared/services/notification.service';
import { of, throwError } from 'rxjs';
import { IUserResponse } from 'app/shared/models/user.models';
import { TRole } from 'app/shared/models/role.enum';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { TRole } from 'app/shared/models/role.enum';

describe('AdminSettingsComponent', () => {
let component: AdminSettingsComponent;
let fixture: ComponentFixture<AdminSettingsComponent>;
let adminServiceSpy: jasmine.SpyObj<AdministrationEndpointsService>;

const mockUser: IUserResponse = {
id: 1,
name: 'John Doe',
email: 'john.doe@example.com',
role: 'Student' as TRole,
studyCycleYearA: 1,
studyCycleYearB: 2,
banned: false,
lastPlayed: '',
course: { id: 1, name: '' },
group: 'l1',
};
let mockAdminEndpointsService: jasmine.SpyObj<AdministrationEndpointsService>;

beforeEach(async () => {
const adminSpy = jasmine.createSpyObj('AdministrationEndpointsService', [
'getUsers',
'banStatus',
'changeRole',
]);
const notificationSpy = jasmine.createSpyObj('NotificationService', [
'addNotification',
]);
mockAdminEndpointsService = jasmine.createSpyObj(
'AdministrationEndpointsService',
['getUsers']
);

await TestBed.configureTestingModule({
imports: [AdminSettingsComponent, HttpClientTestingModule],
imports: [
AdminSettingsComponent,
ReactiveFormsModule,
HttpClientTestingModule,
],
providers: [
{ provide: AdministrationEndpointsService, useValue: adminSpy },
{ provide: NotificationService, useValue: notificationSpy },
{
provide: AdministrationEndpointsService,
useValue: mockAdminEndpointsService,
},
],
}).compileComponents();

fixture = TestBed.createComponent(AdminSettingsComponent);
component = fixture.componentInstance;

adminServiceSpy = TestBed.inject(
AdministrationEndpointsService
) as jasmine.SpyObj<AdministrationEndpointsService>;

fixture.detectChanges();
// Mock the subscription response
mockAdminEndpointsService.getUsers.and.returnValue(of([]));
});

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

it('should load users list when banUnbanUserModal is called', () => {
adminServiceSpy.getUsers.and.returnValue(of([mockUser]));
component.banUnbanUserModal();
expect(adminServiceSpy.getUsers).toHaveBeenCalled();
expect(component.modalVisibility).toBe('banUnbanUser');
expect(component.modalTitle).toBe('Changing ban status of user');
expect(component.modalButtonText).toBe('Set ban status');
expect(component.usersList).toEqual([mockUser]);
});
describe('ngOnInit', () => {
it('should initialize with users fetched', () => {
const mockUsers: IUserResponse[] = [
{ id: 1, email: 'test@example.com' } as IUserResponse,
];
mockAdminEndpointsService.getUsers.and.returnValue(of(mockUsers));

it('should update ban status and show notification', () => {
component.selectedUserData = mockUser;
component.isBanned = true;
adminServiceSpy.banStatus.and.returnValue(of());
component.ngOnInit();

component.banUnbanUserFunction();
expect(adminServiceSpy.banStatus).toHaveBeenCalledWith(mockUser.id, true);
expect(component.modalVisibility).toBeNull();
});
expect(mockAdminEndpointsService.getUsers).toHaveBeenCalled();
expect(component.filteredUsers).toEqual(mockUsers);
expect(component.errorMessage).toBeNull();
});

it('should display an error message if changing ban status fails', () => {
adminServiceSpy.banStatus.and.returnValue(
throwError('Error changing status')
);
component.selectedUserData = mockUser;
component.isBanned = true;
it('should handle error when fetching users', () => {
const errorMessage = 'Error fetching users';
mockAdminEndpointsService.getUsers.and.returnValue(
throwError(() => errorMessage)
);

component.banUnbanUserFunction();
expect(component.errorMessage).toBe('Error changing status');
component.ngOnInit();

expect(mockAdminEndpointsService.getUsers).toHaveBeenCalled();
expect(component.filteredUsers).toBeNull();
expect(component.errorMessage).toEqual(errorMessage);
});
});

it('should update user role and show notification', () => {
component.selectedUserData = mockUser;
component.newUserRole = TRole.Admin;
adminServiceSpy.changeRole.and.returnValue(of());
describe('showOptions', () => {
it('should toggle isOptionsVisible and emit the event if visible', () => {
spyOn(component.optionsVisibleEmitter, 'emit');

component.changeUserRoleFunction();
expect(adminServiceSpy.changeRole).toHaveBeenCalledWith(
mockUser.id,
TRole.Admin
);
expect(component.modalVisibility).toBeNull();
});
component.showOptions();
expect(component.isOptionsVisible).toBeTrue();
expect(component.optionsVisibleEmitter.emit).toHaveBeenCalledWith(
'admin'
);

it('should set error message if role change fails', () => {
adminServiceSpy.changeRole.and.returnValue(
throwError('Error changing role')
);
component.selectedUserData = mockUser;
component.newUserRole = TRole.Admin;
component.showOptions();
expect(component.isOptionsVisible).toBeFalse();
});
});

component.changeUserRoleFunction();
expect(component.errorMessage).toBe('Error changing role');
describe('applyFilters', () => {
it('should apply filters and fetch filtered users', () => {
const mockUsers: IUserResponse[] = [
{ id: 2, email: 'filtered@example.com' } as IUserResponse,
];
mockAdminEndpointsService.getUsers.and.returnValue(of(mockUsers));

component.filterForm.setValue({
role: 'Student',
email: 'test@example.com',
studyCycleYearA: null,
studyCycleYearB: null,
group: '',
courseName: '',
});

component.applyFilters();

expect(mockAdminEndpointsService.getUsers).toHaveBeenCalledWith(
'Student' as TRole,
'test@example.com',
undefined,
undefined,
'',
'',
'Asc',
'Email'
);
expect(component.filteredUsers).toEqual(mockUsers);
expect(component.errorMessage).toBeNull();
});

it('should handle error when applying filters', () => {
const errorMessage = 'Error applying filters';
mockAdminEndpointsService.getUsers.and.returnValue(
throwError(() => errorMessage)
);

component.applyFilters();

expect(mockAdminEndpointsService.getUsers).toHaveBeenCalled();
expect(component.filteredUsers).toBeNull();
expect(component.errorMessage).toEqual(errorMessage);
});
});

it('should hide modal and reset selected user data when hideModal is called', () => {
component.modalVisibility = 'banUnbanUser';
component.selectedUserData = mockUser;
describe('ngOnDestroy', () => {
it('should unsubscribe from getUsersSubscription', () => {
spyOn(component['_getUsersSubscription'], 'unsubscribe');

component.hideModal();
expect(component.modalVisibility).toBeNull();
expect(component.selectedUserData).toBeNull();
});
component.ngOnDestroy();

it('should unsubscribe from all subscriptions on component destroy', () => {
const subscriptionSpy = jasmine.createSpyObj('Subscription', [
'unsubscribe',
]);
component['_getUsersSubscription'] = subscriptionSpy;
component['_getUserStatsSubscription'] = subscriptionSpy;
component['_changeBanStatusSubscription'] = subscriptionSpy;
component['_changeRoleSubscription'] = subscriptionSpy;

component.ngOnDestroy();
expect(subscriptionSpy.unsubscribe).toHaveBeenCalledTimes(4);
expect(component['_getUsersSubscription'].unsubscribe).toHaveBeenCalled();
});
});
});
Loading

0 comments on commit 0aac561

Please sign in to comment.