forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
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 DSpace#3337 from atmire/live-region-main
Live Region
- Loading branch information
Showing
15 changed files
with
472 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div class="live-region" [ngClass]="{'visually-hidden': !isVisible }" aria-live="assertive" role="log" aria-relevant="additions" aria-atomic="true"> | ||
<div class="live-region-message" *ngFor="let message of (messages$ | async)">{{ message }}</div> | ||
</div> |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
.live-region { | ||
position: fixed; | ||
bottom: 0; | ||
left: 0; | ||
right: 0; | ||
padding-left: 60px; | ||
height: 90px; | ||
line-height: 18px; | ||
color: var(--bs-white); | ||
background-color: var(--bs-dark); | ||
opacity: 0.94; | ||
z-index: var(--ds-live-region-z-index); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { | ||
ComponentFixture, | ||
TestBed, | ||
waitForAsync, | ||
} from '@angular/core/testing'; | ||
import { By } from '@angular/platform-browser'; | ||
import { TranslateModule } from '@ngx-translate/core'; | ||
import { of } from 'rxjs'; | ||
|
||
import { LiveRegionComponent } from './live-region.component'; | ||
import { LiveRegionService } from './live-region.service'; | ||
|
||
describe('liveRegionComponent', () => { | ||
let fixture: ComponentFixture<LiveRegionComponent>; | ||
let liveRegionService: LiveRegionService; | ||
|
||
beforeEach(waitForAsync(() => { | ||
liveRegionService = jasmine.createSpyObj('liveRegionService', { | ||
getMessages$: of(['message1', 'message2']), | ||
getLiveRegionVisibility: false, | ||
setLiveRegionVisibility: undefined, | ||
}); | ||
|
||
void TestBed.configureTestingModule({ | ||
imports: [ | ||
TranslateModule.forRoot(), | ||
LiveRegionComponent, | ||
], | ||
providers: [ | ||
{ provide: LiveRegionService, useValue: liveRegionService }, | ||
], | ||
}).compileComponents(); | ||
})); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(LiveRegionComponent); | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should contain the current live region messages', () => { | ||
const messages = fixture.debugElement.queryAll(By.css('.live-region-message')); | ||
|
||
expect(messages.length).toEqual(2); | ||
expect(messages[0].nativeElement.textContent).toEqual('message1'); | ||
expect(messages[1].nativeElement.textContent).toEqual('message2'); | ||
}); | ||
|
||
it('should respect the live region visibility', () => { | ||
const liveRegion = fixture.debugElement.query(By.css('.live-region')); | ||
expect(liveRegion).toBeDefined(); | ||
|
||
const liveRegionHidden = fixture.debugElement.query(By.css('.visually-hidden')); | ||
expect(liveRegionHidden).toBeDefined(); | ||
|
||
liveRegionService.getLiveRegionVisibility = jasmine.createSpy('getLiveRegionVisibility').and.returnValue(true); | ||
fixture = TestBed.createComponent(LiveRegionComponent); | ||
fixture.detectChanges(); | ||
|
||
const liveRegionVisible = fixture.debugElement.query(By.css('.visually-hidden')); | ||
expect(liveRegionVisible).toBeNull(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { | ||
AsyncPipe, | ||
NgClass, | ||
NgFor, | ||
} from '@angular/common'; | ||
import { | ||
Component, | ||
OnInit, | ||
} from '@angular/core'; | ||
import { Observable } from 'rxjs'; | ||
|
||
import { LiveRegionService } from './live-region.service'; | ||
|
||
/** | ||
* The Live Region Component is an accessibility tool for screenreaders. When a change occurs on a page when the changed | ||
* section is not in focus, a message should be displayed by this component so it can be announced by a screen reader. | ||
* | ||
* This component should not be used directly. Use the {@link LiveRegionService} to add messages. | ||
*/ | ||
@Component({ | ||
selector: `ds-live-region`, | ||
templateUrl: './live-region.component.html', | ||
styleUrls: ['./live-region.component.scss'], | ||
standalone: true, | ||
imports: [NgClass, NgFor, AsyncPipe], | ||
}) | ||
export class LiveRegionComponent implements OnInit { | ||
|
||
protected isVisible: boolean; | ||
|
||
protected messages$: Observable<string[]>; | ||
|
||
constructor( | ||
protected liveRegionService: LiveRegionService, | ||
) { | ||
} | ||
|
||
ngOnInit() { | ||
this.isVisible = this.liveRegionService.getLiveRegionVisibility(); | ||
this.messages$ = this.liveRegionService.getMessages$(); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Config } from '../../../config/config.interface'; | ||
|
||
/** | ||
* Configuration interface used by the LiveRegionService | ||
*/ | ||
export class LiveRegionConfig implements Config { | ||
messageTimeOutDurationMs: number; | ||
isVisible: boolean; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import { | ||
fakeAsync, | ||
flush, | ||
tick, | ||
} from '@angular/core/testing'; | ||
|
||
import { UUIDService } from '../../core/shared/uuid.service'; | ||
import { LiveRegionService } from './live-region.service'; | ||
|
||
describe('liveRegionService', () => { | ||
let service: LiveRegionService; | ||
|
||
beforeEach(() => { | ||
service = new LiveRegionService( | ||
new UUIDService(), | ||
); | ||
}); | ||
|
||
describe('addMessage', () => { | ||
it('should correctly add messages', () => { | ||
expect(service.getMessages().length).toEqual(0); | ||
|
||
service.addMessage('Message One'); | ||
expect(service.getMessages().length).toEqual(1); | ||
expect(service.getMessages()[0]).toEqual('Message One'); | ||
|
||
service.addMessage('Message Two'); | ||
expect(service.getMessages().length).toEqual(2); | ||
expect(service.getMessages()[1]).toEqual('Message Two'); | ||
}); | ||
}); | ||
|
||
describe('clearMessages', () => { | ||
it('should clear the messages', () => { | ||
expect(service.getMessages().length).toEqual(0); | ||
|
||
service.addMessage('Message One'); | ||
service.addMessage('Message Two'); | ||
expect(service.getMessages().length).toEqual(2); | ||
|
||
service.clear(); | ||
expect(service.getMessages().length).toEqual(0); | ||
}); | ||
}); | ||
|
||
describe('messages$', () => { | ||
it('should emit when a message is added and when a message is removed after the timeOut', fakeAsync(() => { | ||
const results: string[][] = []; | ||
|
||
service.getMessages$().subscribe((messages) => { | ||
results.push(messages); | ||
}); | ||
|
||
expect(results.length).toEqual(1); | ||
expect(results[0]).toEqual([]); | ||
|
||
service.addMessage('message'); | ||
|
||
tick(); | ||
|
||
expect(results.length).toEqual(2); | ||
expect(results[1]).toEqual(['message']); | ||
|
||
tick(service.getMessageTimeOutMs()); | ||
|
||
expect(results.length).toEqual(3); | ||
expect(results[2]).toEqual([]); | ||
})); | ||
|
||
it('should only emit once when the messages are cleared', fakeAsync(() => { | ||
const results: string[][] = []; | ||
|
||
service.getMessages$().subscribe((messages) => { | ||
results.push(messages); | ||
}); | ||
|
||
expect(results.length).toEqual(1); | ||
expect(results[0]).toEqual([]); | ||
|
||
service.addMessage('Message One'); | ||
service.addMessage('Message Two'); | ||
|
||
tick(); | ||
|
||
expect(results.length).toEqual(3); | ||
expect(results[2]).toEqual(['Message One', 'Message Two']); | ||
|
||
service.clear(); | ||
flush(); | ||
|
||
expect(results.length).toEqual(4); | ||
expect(results[3]).toEqual([]); | ||
})); | ||
|
||
it('should not pop messages added after clearing within timeOut period', fakeAsync(() => { | ||
const results: string[][] = []; | ||
|
||
service.getMessages$().subscribe((messages) => { | ||
results.push(messages); | ||
}); | ||
|
||
expect(results.length).toEqual(1); | ||
expect(results[0]).toEqual([]); | ||
|
||
service.addMessage('Message One'); | ||
tick(10000); | ||
service.clear(); | ||
tick(15000); | ||
service.addMessage('Message Two'); | ||
|
||
// Message Two should not be cleared after 5 more seconds | ||
tick(5000); | ||
|
||
expect(results.length).toEqual(4); | ||
expect(results[3]).toEqual(['Message Two']); | ||
|
||
// But should be cleared 30 seconds after it was added | ||
tick(25000); | ||
expect(results.length).toEqual(5); | ||
expect(results[4]).toEqual([]); | ||
})); | ||
|
||
it('should respect configured timeOut', fakeAsync(() => { | ||
const results: string[][] = []; | ||
|
||
service.getMessages$().subscribe((messages) => { | ||
results.push(messages); | ||
}); | ||
|
||
expect(results.length).toEqual(1); | ||
expect(results[0]).toEqual([]); | ||
|
||
const timeOutMs = 500; | ||
service.setMessageTimeOutMs(timeOutMs); | ||
|
||
service.addMessage('Message One'); | ||
tick(timeOutMs - 1); | ||
|
||
expect(results.length).toEqual(2); | ||
expect(results[1]).toEqual(['Message One']); | ||
|
||
tick(1); | ||
|
||
expect(results.length).toEqual(3); | ||
expect(results[2]).toEqual([]); | ||
|
||
const timeOutMsTwo = 50000; | ||
service.setMessageTimeOutMs(timeOutMsTwo); | ||
|
||
service.addMessage('Message Two'); | ||
tick(timeOutMsTwo - 1); | ||
|
||
expect(results.length).toEqual(4); | ||
expect(results[3]).toEqual(['Message Two']); | ||
|
||
tick(1); | ||
|
||
expect(results.length).toEqual(5); | ||
expect(results[4]).toEqual([]); | ||
})); | ||
}); | ||
|
||
describe('liveRegionVisibility', () => { | ||
it('should be false by default', () => { | ||
expect(service.getLiveRegionVisibility()).toBeFalse(); | ||
}); | ||
|
||
it('should correctly update', () => { | ||
service.setLiveRegionVisibility(true); | ||
expect(service.getLiveRegionVisibility()).toBeTrue(); | ||
service.setLiveRegionVisibility(false); | ||
expect(service.getLiveRegionVisibility()).toBeFalse(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.