Skip to content

Commit

Permalink
Merge pull request fga-eps-mds#17 from fga-eps-mds/16-notificacoes
Browse files Browse the repository at this point in the history
16 notificacoes
  • Loading branch information
viniman27 authored Aug 30, 2024
2 parents 4aa83c1 + a245d25 commit 5e68b20
Show file tree
Hide file tree
Showing 15 changed files with 647 additions and 130 deletions.
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ module.exports = function (config) {
browsers: ['Chrome', 'ChromeHeadless', 'ChromeHeadlessCI'],
restartOnFileChange: true
});
};
};
35 changes: 15 additions & 20 deletions src/app/app-routing.module.ts
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@ import { PrivacyPolicyComponent } from './pages/privacy-policy/privacy-policy.co

import { HomeAdminComponent } from './pages/home-admin/home-admin.component';
import { AdminActivateComponent } from './pages/admin-activate/admin-activate.component';
import { SuperAdminActivateComponent } from './pages/super-admin-activate/super-admin-activate.component';
import { CategoryTableComponent } from './pages/category-table/category-table.component';
import { VideoViewsComponent } from './pages/video-views/video-views.component';
import { RecordComponent } from './pages/record/record.component';
import { DashboardCategoryComponent } from './pages/dashboard-category/dashboard-category.component';
import { ControleSuperAdminComponent } from './pages/controle-super-admin/controle-super-admin.component';
import { NotificationsComponent } from './pages/notifications/notifications.component';

import { WithTokenGuard } from './guard/with-token.guard';
import { TokenAdminGuard } from './guard/admin.guard';
import { TokenSuperAdminGuard } from './guard/super-admin.guard';


const routes: Routes = [
{ path: '', component: LoginComponent, canActivate: [WithTokenGuard] },
Expand Down Expand Up @@ -95,26 +94,22 @@ const routes: Routes = [
canActivate: [AdminGuard],
},
{ path: 'privacy', component: PrivacyPolicyComponent },
{
path: 'homeAdmin',

{ path: 'homeAdmin',
component: HomeAdminComponent,
canActivate: [TokenAdminGuard],
},
{
path: 'adminActivate',
component: AdminActivateComponent,
},
{
path: 'superAdminActivate',
component: SuperAdminActivateComponent,
component: AdminActivateComponent
},
{
{
path: 'category-views',
component: CategoryTableComponent,
canActivate: [TokenAdminGuard],
},
{
path: 'video-views',
{
path: 'video-views',
component: VideoViewsComponent,
canActivate: [TokenAdminGuard],
},
Expand All @@ -124,19 +119,19 @@ const routes: Routes = [
canActivate: [TokenAdminGuard],
},
{
path: 'record',
component: RecordComponent,
canActivate: [AuthGuard],
path: 'record',
component: RecordComponent,
canActivate: [AuthGuard]
},
{
path: 'controleSuperAdmin',
component: ControleSuperAdminComponent,
canActivate: [TokenSuperAdminGuard],
path: 'notifications',
component: NotificationsComponent,
canActivate: [AuthGuard]
},
];

@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule {}
export class AppRoutingModule { }
10 changes: 8 additions & 2 deletions src/app/app.component.spec.ts
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing'; // Import necessário
import { AppComponent } from './app.component';
import { BackgroundComponent } from './components/background/background.component';
import { ToastModule } from 'primeng/toast';
import { ConfirmationService, MessageService } from 'primeng/api';
import { ConfirmDialogModule } from 'primeng/confirmdialog';
import { MenuModule } from 'primeng/menu';


describe('AppComponent', () => {
let component: AppComponent;
let fixture: ComponentFixture<AppComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [RouterTestingModule, ToastModule, ConfirmDialogModule, MenuModule],
imports: [
RouterTestingModule,
HttpClientTestingModule,
ToastModule,
ConfirmDialogModule,
MenuModule
],
declarations: [AppComponent, BackgroundComponent],
providers: [MessageService, ConfirmationService],
}).compileComponents();
Expand Down
9 changes: 4 additions & 5 deletions src/app/app.module.ts
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import { ButtonModule } from 'primeng/button';
import { SocialLoginModule, SocialAuthServiceConfig, GoogleLoginProvider, FacebookLoginProvider } from '@abacritt/angularx-social-login';

// Declaration
import {CommonModule} from '@angular/common';
import { NgModule, isDevMode } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AppComponent } from './app.component';
import { LoginComponent } from './pages/login/login.component';
import { RegisterComponent } from './pages/register/register.component';
Expand Down Expand Up @@ -47,13 +47,12 @@ import { PrivacyPolicyComponent } from './pages/privacy-policy/privacy-policy.co
import { ServiceWorkerModule } from '@angular/service-worker';
import { NgChartsModule } from 'ng2-charts';
import { HomeAdminComponent } from './pages/home-admin/home-admin.component';
import {ControleSuperAdminComponent} from './pages/controle-super-admin/controle-super-admin.component'


import { CategoryTableComponent } from './pages/category-table/category-table.component';
import { VideoViewsComponent } from './pages/video-views/video-views.component';
import { DashboardCategoryComponent } from './pages/dashboard-category/dashboard-category.component';
import { RecordComponent } from './pages/record/record.component';
import { NotificationsComponent } from './pages/notifications/notifications.component';

@NgModule({
imports: [
Expand Down Expand Up @@ -112,7 +111,7 @@ import { RecordComponent } from './pages/record/record.component';
VideoViewsComponent,
DashboardCategoryComponent,
RecordComponent,
ControleSuperAdminComponent,
NotificationsComponent,
],

providers: [
Expand Down Expand Up @@ -150,4 +149,4 @@ import { RecordComponent } from './pages/record/record.component';
],
bootstrap: [AppComponent],
})
export class AppModule { }
export class AppModule { }
22 changes: 22 additions & 0 deletions src/app/components/background/background.component.css
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,25 @@
border: none;
box-shadow: none;
}

:host ::ng-deep .notification-badge-wrapper {
pointer-events: none;
position: relative;
top: -1em;
left: -1.8em;
display: flex;
align-items: center;
justify-content: center;
}

:host ::ng-deep .notification-badge {
background-color: red;
color: white;
border-radius: 50%;
padding: 0.7em 0.5em;
font-size: 0.7em;
margin-left: 0.5em;
display: inline-block;
min-width: 1.5em;
text-align: center;
}
6 changes: 5 additions & 1 deletion src/app/components/background/background.component.html
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="w-full flex h-screen flex-col min-h-screen justify-between">
<div class="w-full flex flex-col min-h-screen justify-between">
<nav class="w-full fixed top-0 left-0 z-50 bg-white">
<div class="pt-10 flex justify-center items-center">
<img
Expand Down Expand Up @@ -65,7 +65,11 @@
type="button"
(click)="menu.toggle($event)"
label="&#9776;"
class="menu-button"
></button>
<div class="notification-badge-wrapper" *ngIf="hasNotifications">
<div class="notification-badge"></div> <!-- Badge sem o número -->
</div>
</div>
</div>
</div>
Expand Down
149 changes: 63 additions & 86 deletions src/app/components/background/background.component.spec.ts
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,118 +1,95 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing';
import { BackgroundComponent } from './background.component';
import { RouterTestingModule } from '@angular/router/testing';
import { MenuModule } from 'primeng/menu';
import { NotificationService } from 'src/app/services/notification.service';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { of } from 'rxjs';

describe('BackgroundComponent', () => {
let component: BackgroundComponent;
let fixture: ComponentFixture<BackgroundComponent>;
let notificationService: NotificationService;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [RouterTestingModule, MenuModule, HttpClientTestingModule],
declarations: [BackgroundComponent],
imports: [RouterTestingModule, MenuModule],
providers: [
{
provide: NotificationService,
useValue: {
fetchRecommendedVideosCount: jasmine.createSpy('fetchRecommendedVideosCount').and.returnValue(of({ recommend_videos: [{}, {}, {}] })),
recommendedVideosCount$: of(5),
isAuthenticated: jasmine.createSpy('isAuthenticated').and.returnValue(true),
setUserIdFromToken: jasmine.createSpy('setUserIdFromToken'),
userId: 'mockUserId',
updateRecommendedVideosCount: jasmine.createSpy('updateRecommendedVideosCount')
}
}
],
}).compileComponents();

fixture = TestBed.createComponent(BackgroundComponent);
component = fixture.componentInstance;
notificationService = TestBed.inject(NotificationService);

fixture.detectChanges();
});

afterEach(() => {
component.ngOnDestroy(); // Certifique-se de limpar após cada teste
});

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

describe('Identifies User Device', () => {
it('should identify a mobile device - Android', () => {
// mock userAgent for Android
Object.defineProperty(navigator, 'userAgent', {
value:
'Mozilla/5.0 (Linux; Android 10; Pixel 3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Mobile Safari/537.36',
configurable: true,
writable: true,
});

component.identifiesUserDevice();
expect(component.mobileDevide).toBeTruthy();
});
it('should initialize items and start fetching notifications', fakeAsync(() => {
expect(notificationService.fetchRecommendedVideosCount).toHaveBeenCalled();
expect(notificationService.setUserIdFromToken).toHaveBeenCalled();
expect(component.hasNotifications).toBeTrue(); // Verifica se as notificações foram atualizadas
}));

it('should identify a mobile device - iPhone', () => {
// mock userAgent for iPhone
Object.defineProperty(navigator, 'userAgent', {
value:
'Mozilla/5.0 (iPhone; CPU iPhone OS 15_7_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6.3 Mobile/15E148 Safari/604.1',
configurable: true,
writable: true,
});

component.identifiesUserDevice();
expect(component.mobileDevide).toBeTruthy();
});
it('should correctly update the notification count', fakeAsync(() => {
const response = { recommend_videos: [{}, {}, {}] };
component.updateNotificationCount(response);

it('should identify a mobile device - iPad', () => {
// mock userAgent for iPad
Object.defineProperty(navigator, 'userAgent', {
value:
'Mozilla/5.0 (iPad; CPU OS 14_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1 Mobile/15E148 Safari/604.1',
configurable: true,
writable: true,
});

component.identifiesUserDevice();
expect(component.mobileDevide).toBeTruthy();
});
tick(); // Avança o tempo para garantir a execução da lógica

it('should identify a mobile device - iPod', () => {
// mock userAgent for iPod
Object.defineProperty(navigator, 'userAgent', {
value:
'Mozilla/5.0 (iPod; CPU iPhone OS 14_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1 Mobile/15E148 Safari/604.1',
configurable: true,
writable: true,
});

component.identifiesUserDevice();
expect(component.mobileDevide).toBeTruthy();
});
expect(notificationService.updateRecommendedVideosCount).toHaveBeenCalledWith(3);
expect(component.hasNotifications).toBeTrue();
}));

it('should identify a mobile device - Windows Phone', () => {
// mock userAgent for Windows Phone
Object.defineProperty(navigator, 'userAgent', {
value:
'Mozilla/5.0 (Windows Phone 10.0; Android 6.0.1; Microsoft; Lumia 950 XL) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Mobile Safari/537.36 Edge/13.10586',
configurable: true,
writable: true,
});

component.identifiesUserDevice();
expect(component.mobileDevide).toBeTruthy();
});
it('should correctly identify the user device as mobile or not', () => {
const originalUserAgent = navigator.userAgent;
const mobileUserAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1';

it('should identify a mobile device - BlackBerry', () => {
// mock userAgent for BlackBerry
Object.defineProperty(navigator, 'userAgent', {
value:
'Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+',
configurable: true,
writable: true,
});

component.identifiesUserDevice();
expect(component.mobileDevide).toBeTruthy();
Object.defineProperty(navigator, 'userAgent', {
value: mobileUserAgent,
writable: true,
configurable: true
});

it('should identify a non-mobile device', () => {
// mock userAgent
Object.defineProperty(navigator, 'userAgent', {
value:
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 OPR/104.0.0.0 (Edition std-1)',
configurable: true,
writable: true,
});

component.identifiesUserDevice();
expect(component.mobileDevide).toBeFalsy();
component.identifiesUserDevice();
expect(component.mobileDevide).toBeTrue(); // Agora deve passar

Object.defineProperty(navigator, 'userAgent', {
value: originalUserAgent
});
});

it('should update the notification label', () => {
component.updateNotificationLabel();
fixture.detectChanges();

const notificationItem = component.items.find(item => item.routerLink === '/notifications');
expect(notificationItem?.label).toContain('Notificações <span class="notification-badge"');
});

it('should clear interval subscription on destroy', () => {
const unsubscribeSpy = spyOn(component['intervalSubscription'] as any, 'unsubscribe');
component.ngOnDestroy();
expect(unsubscribeSpy).toHaveBeenCalled();
});
});
Loading

0 comments on commit 5e68b20

Please sign in to comment.