forked from fga-eps-mds/2023.2-UnB-TV-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request fga-eps-mds#17 from fga-eps-mds/16-notificacoes
16 notificacoes
- Loading branch information
Showing
15 changed files
with
647 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 63 additions & 86 deletions
149
src/app/components/background/background.component.spec.ts
100755 → 100644
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
Oops, something went wrong.