Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions projects/v3/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const routes: Routes = [
},
{
path: 'experiences',
title: 'Experiences - Practera',
loadChildren: () => import('./pages/experiences/experiences.module').then(m => m.ExperiencesPageModule),
canLoad: [AuthGuard],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ <h1 class="for-accessibility" i18n>Notifications</h1>
<ion-toolbar>
<ion-buttons slot="start">
@if (mode === 'modal') {
<ion-button (keydown)="dismiss($event)" (click)="dismiss()">
<ion-button (keydown)="dismiss($event)" (click)="dismiss()" aria-label="Close notifications" i18n-aria-label>
<ion-icon slot="icon-only" color="grey-75" name="arrow-back-outline"></ion-icon>
</ion-button>
}

@if (!mode) {
<ion-button (keydown)="goBack($event)" (click)="goBack()">
<ion-button (keydown)="goBack($event)" (click)="goBack()" aria-label="Go back" i18n-aria-label>
<ion-icon slot="icon-only" color="grey-75" name="arrow-back-outline"></ion-icon>
</ion-button>
}
Expand Down
125 changes: 116 additions & 9 deletions projects/v3/src/app/pages/notifications/notifications.page.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ComponentFixture, fakeAsync, flushMicrotasks, TestBed, waitForAsync } from '@angular/core/testing';
import { Router } from '@angular/router';
import { DefaultUrlSerializer, Router } from '@angular/router';
import { UtilsService } from '@v3/services/utils.service';
import { IonicModule, ModalController } from '@ionic/angular/lazy';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
Expand All @@ -12,6 +12,7 @@ import { NotificationsPage } from './notifications.page';
import { of } from 'rxjs';
import { MockRouter } from '@testingv3/mocked.service';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { Title } from '@angular/platform-browser';

describe('NotificationsPage', () => {
let component: NotificationsPage;
Expand Down Expand Up @@ -50,13 +51,6 @@ describe('NotificationsPage', () => {
provide: Router,
useClass: MockRouter
},
{
provide: ModalController,
useValue: jasmine.createSpyObj('ModalController', {
'dismiss': Promise.resolve(),
'getTop': Promise.resolve(true),
}),
},
{
provide: HomeService,
useValue: jasmine.createSpyObj('HomeService', ['getMilestones'], {
Expand All @@ -76,7 +70,14 @@ describe('NotificationsPage', () => {
component = fixture.componentInstance;
routerSpy = TestBed.inject(Router);
utilsSpy = TestBed.inject(UtilsService);
modalSpy = TestBed.inject(ModalController);
modalSpy = jasmine.createSpyObj('ModalController', {
dismiss: Promise.resolve(true),
getTop: Promise.resolve(true),
});
(component as any).modalController = modalSpy;
(routerSpy as any).lastSuccessfulNavigation = () => null;
(routerSpy as any).parseUrl = (url: string) => new DefaultUrlSerializer().parse(url);
(routerSpy as any).navigateByUrl = jasmine.createSpy('navigateByUrl').and.returnValue(Promise.resolve(true));
notificationSpy = TestBed.inject(NotificationsService);

// reconfigure getTop to return truthy (global test.ts override sets it to null)
Expand All @@ -90,6 +91,37 @@ describe('NotificationsPage', () => {
});

describe('ngOnInit()', () => {
for (const title of ['Local Test Experience', 'Events - Practera', 'Peer feedback']) {
it(`preserves ${title} when Notifications is a drawer`, () => {
const titleService = TestBed.inject(Title);
const originalTitle = titleService.getTitle();
(utilsSpy.setPageTitle as jasmine.Spy).and.callFake(value => titleService.setTitle(value));
titleService.setTitle(title);
component.mode = 'modal';
try {
component.ngOnInit();
expect(titleService.getTitle()).toBe(title);
component.ngOnDestroy();
expect(titleService.getTitle()).toBe(title);
} finally {
titleService.setTitle(originalTitle);
}
});
}

it('uses the Notifications title for a routed page', () => {
const titleService = TestBed.inject(Title);
const originalTitle = titleService.getTitle();
(utilsSpy.setPageTitle as jasmine.Spy).and.callFake(value => titleService.setTitle(value));
try {
titleService.setTitle('Events - Practera');
component.ngOnInit();
expect(titleService.getTitle()).toBe('Notifications - Practera');
} finally {
titleService.setTitle(originalTitle);
}
});

it('should initiate subscriptions', () => {
utilsSpy.isEmpty = jasmine.createSpy('isEmpty').and.returnValue(false);
component.ngOnInit();
Expand Down Expand Up @@ -244,11 +276,86 @@ describe('NotificationsPage', () => {

describe('goBack()', () => {
it('should dismiss notification modal', () => {
component.mode = 'modal';
utilsSpy.isMobile = jasmine.createSpy('isMobile').and.returnValue(false);
component.goBack();

expect(modalSpy.dismiss).toHaveBeenCalled();
});

for (const mobile of [false, true]) {
it(`returns a direct Notifications page to Home (mobile=${mobile})`, async () => {
utilsSpy.isMobile = jasmine.createSpy('isMobile').and.returnValue(mobile);
component.window = { history: { length: 1, back: jasmine.createSpy('back') } };
await component.goBack();
expect(routerSpy.navigate).toHaveBeenCalledWith(['v3', 'home'], { replaceUrl: true });
expect(modalSpy.dismiss).not.toHaveBeenCalled();
expect(component.window.history.back).not.toHaveBeenCalled();
});

it(`returns to the recorded in-app destination (mobile=${mobile})`, async () => {
utilsSpy.isMobile = jasmine.createSpy('isMobile').and.returnValue(mobile);
(routerSpy as any).lastSuccessfulNavigation = () => ({ previousNavigation: { id: 1 } });
component.window = { history: {
length: 2,
state: { notificationsReturnUrl: '/v3/activity-mobile/11?view=feedback#answer' },
back: jasmine.createSpy('back'),
} };
await component.goBack();
expect(routerSpy.navigateByUrl).toHaveBeenCalledWith('/v3/activity-mobile/11?view=feedback#answer', { replaceUrl: true });
expect(component.window.history.back).not.toHaveBeenCalled();
expect(modalSpy.dismiss).not.toHaveBeenCalled();
expect(routerSpy.navigate).not.toHaveBeenCalled();
});
}

it('falls back to Home if redirect navigation has no browser history entry', async () => {
(routerSpy as any).lastSuccessfulNavigation = () => ({ previousNavigation: { id: 1 } });
component.window = { history: { length: 1, back: jasmine.createSpy('back') } };
await component.goBack();
expect(routerSpy.navigate).toHaveBeenCalledWith(['v3', 'home'], { replaceUrl: true });
expect(component.window.history.back).not.toHaveBeenCalled();
});

it('does not return direct entry to unrelated browser history', async () => {
component.window = { history: { length: 3, back: jasmine.createSpy('back') } };
await component.goBack();
expect(routerSpy.navigate).toHaveBeenCalledWith(['v3', 'home'], { replaceUrl: true });
expect(component.window.history.back).not.toHaveBeenCalled();
});

it('returns a cached direct entry to Home even after Angular navigated elsewhere', async () => {
(routerSpy as any).lastSuccessfulNavigation = () => ({ previousNavigation: { id: 2 } });
component.window = { history: { length: 3, state: { navigationId: 3 }, back: jasmine.createSpy('back') } };
await component.goBack();
expect(routerSpy.navigate).toHaveBeenCalledWith(['v3', 'home'], { replaceUrl: true });
expect(component.window.history.back).not.toHaveBeenCalled();
});

for (const returnUrl of ['/topic-mobile/11/22', '/assessment-mobile/assessment/11/8/11', '/experiences']) {
it(`returns to ${returnUrl}`, async () => {
component.window = { history: { state: { notificationsReturnUrl: returnUrl } } };
await component.goBack();
expect(routerSpy.navigateByUrl).toHaveBeenCalledWith(returnUrl, { replaceUrl: true });
});
}

for (const returnUrl of ['https://example.com/', '//example.com/', '/v3/notifications?from=home', '/%invalid', '/auth/logout']) {
it(`falls back to Home for an invalid return destination: ${returnUrl}`, async () => {
component.window = { history: { state: { notificationsReturnUrl: returnUrl } } };
await component.goBack();
expect(routerSpy.navigate).toHaveBeenCalledWith(['v3', 'home'], { replaceUrl: true });
expect(routerSpy.navigateByUrl).not.toHaveBeenCalled();
});
}

it('dismisses a modal even in a mobile layout', async () => {
component.mode = 'modal';
utilsSpy.isMobile = jasmine.createSpy('isMobile').and.returnValue(true);
await component.goBack();
expect(modalSpy.dismiss).toHaveBeenCalled();
expect(routerSpy.navigate).not.toHaveBeenCalled();
});
});

describe('keyboardEvent', () => {
Expand Down
23 changes: 19 additions & 4 deletions projects/v3/src/app/pages/notifications/notifications.page.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ChangeDetectorRef, Component, Inject, Input, NgZone, OnDestroy, OnInit } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { Router } from '@angular/router';
import { PRIMARY_OUTLET, Router } from '@angular/router';
import { NotificationsService, TodoItem } from '@v3/app/services/notifications.service';
import { UtilsService } from '@v3/app/services/utils.service';
import { trigger, transition, useAnimation } from '@angular/animations';
Expand Down Expand Up @@ -49,7 +49,9 @@ export class NotificationsPage implements OnInit, OnDestroy {
}

ngOnInit() {
this.utils.setPageTitle('Notifications - Practera');
if (this.mode !== 'modal') {
this.utils.setPageTitle('Notifications - Practera');
}
this.subscriptions.push(this.homeService.milestones$.subscribe(async milestones => {
if (milestones === null) {
this.homeService.getMilestones();
Expand Down Expand Up @@ -217,9 +219,22 @@ export class NotificationsPage implements OnInit, OnDestroy {
return;
}

if (!this.isMobile) {
if (this.mode === 'modal') {
return this.dismiss();
}
return this.window.history.back();
const returnUrl = this.window.history.state?.notificationsReturnUrl;
if (typeof returnUrl === 'string' && returnUrl.startsWith('/') && !returnUrl.startsWith('//')) {
try {
const segments = this.router.parseUrl(returnUrl).root.children[PRIMARY_OUTLET]?.segments || [];
const root = segments[0]?.path;
const allowedRoot = ['v3', 'experiences', 'topic-mobile', 'assessment-mobile'].includes(root);
if (allowedRoot && !(root === 'v3' && segments[1]?.path === 'notifications')) {
return this.router.navigateByUrl(returnUrl, { replaceUrl: true });
}
} catch {
// Invalid or stale navigation state should still provide a way back to Home.
}
}
return this.router.navigate(['v3', 'home'], { replaceUrl: true });
}
}
Loading
Loading