|
7 | 7 | */
|
8 | 8 |
|
9 | 9 | import {CommonModule, Location, LocationStrategy, PathLocationStrategy, PlatformLocation} from '@angular/common';
|
10 |
| -import {MockPlatformLocation} from '@angular/common/testing'; |
| 10 | +import {MockLocationStrategy, MockPlatformLocation} from '@angular/common/testing'; |
11 | 11 | import {inject, TestBed} from '@angular/core/testing';
|
12 | 12 |
|
13 | 13 | const baseUrl = '/base';
|
@@ -84,7 +84,7 @@ describe('Location Class', () => {
|
84 | 84 | TestBed.configureTestingModule({
|
85 | 85 | imports: [CommonModule],
|
86 | 86 | providers: [
|
87 |
| - {provide: LocationStrategy, useClass: PathLocationStrategy}, |
| 87 | + {provide: LocationStrategy, useClass: MockLocationStrategy}, |
88 | 88 | {
|
89 | 89 | provide: PlatformLocation,
|
90 | 90 | useFactory: () => {
|
@@ -113,5 +113,30 @@ describe('Location Class', () => {
|
113 | 113 | expect((location as any)._urlChangeListeners.length).toBe(1);
|
114 | 114 | expect((location as any)._urlChangeListeners[0]).toEqual(changeListener);
|
115 | 115 | }));
|
| 116 | + |
| 117 | + it('should only notify listeners once when multiple listeners are registered', () => { |
| 118 | + const location = TestBed.inject(Location); |
| 119 | + const locationStrategy = TestBed.inject(LocationStrategy) as MockLocationStrategy; |
| 120 | + let notificationCount = 0; |
| 121 | + |
| 122 | + function incrementChangeListener(url: string, state: unknown) { |
| 123 | + notificationCount += 1; |
| 124 | + |
| 125 | + return undefined; |
| 126 | + } |
| 127 | + |
| 128 | + function noopChangeListener(url: string, state: unknown) { |
| 129 | + return undefined; |
| 130 | + } |
| 131 | + |
| 132 | + location.onUrlChange(incrementChangeListener); |
| 133 | + location.onUrlChange(noopChangeListener); |
| 134 | + |
| 135 | + expect(notificationCount).toBe(0); |
| 136 | + |
| 137 | + locationStrategy.simulatePopState('/test'); |
| 138 | + |
| 139 | + expect(notificationCount).toBe(1); |
| 140 | + }); |
116 | 141 | });
|
117 | 142 | });
|
0 commit comments