Skip to content

Commit 0898827

Browse files
crisbetojosephperrott
authored andcommitted
fix(progress-bar): not taking current path after first initialization (#13628)
1 parent 5162e3b commit 0898827

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/lib/progress-bar/progress-bar.spec.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ import {MatProgressBar} from './progress-bar';
88

99

1010
describe('MatProgressBar', () => {
11-
let fakePath = '/fake-path';
11+
let fakePath: string;
1212

1313
function createComponent<T>(componentType: Type<T>,
1414
imports?: Array<Type<{}>>): ComponentFixture<T> {
15+
fakePath = '/fake-path';
16+
1517
TestBed.configureTestingModule({
1618
imports: imports || [MatProgressBarModule],
1719
declarations: [componentType],
1820
providers: [{
1921
provide: MAT_PROGRESS_BAR_LOCATION,
20-
useValue: {pathname: fakePath}
22+
useValue: {getPathname: () => fakePath}
2123
}]
2224
}).compileComponents();
2325

@@ -122,6 +124,23 @@ describe('MatProgressBar', () => {
122124
const svg = fixture.debugElement.query(By.css('svg')).nativeElement;
123125
expect(svg.getAttribute('focusable')).toBe('false');
124126
});
127+
128+
it('should use latest path when prefixing the SVG references', () => {
129+
let fixture = createComponent(BasicProgressBar);
130+
fixture.detectChanges();
131+
132+
let rect = fixture.debugElement.query(By.css('rect')).nativeElement;
133+
expect(rect.getAttribute('fill')).toMatch(/^url\(['"]?\/fake-path#.*['"]?\)$/);
134+
135+
fixture.destroy();
136+
fakePath = '/another-fake-path';
137+
138+
fixture = TestBed.createComponent(BasicProgressBar);
139+
fixture.detectChanges();
140+
rect = fixture.debugElement.query(By.css('rect')).nativeElement;
141+
142+
expect(rect.getAttribute('fill')).toMatch(/^url\(['"]?\/another-fake-path#.*['"]?\)$/);
143+
});
125144
});
126145

127146
describe('animation trigger on determinate setting', () => {

src/lib/progress-bar/progress-bar.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,18 @@ export const MAT_PROGRESS_BAR_LOCATION = new InjectionToken<MatProgressBarLocati
6060
* @docs-private
6161
*/
6262
export interface MatProgressBarLocation {
63-
pathname: string;
63+
getPathname: () => string;
6464
}
6565

6666
/** @docs-private */
6767
export function MAT_PROGRESS_BAR_LOCATION_FACTORY(): MatProgressBarLocation {
6868
const _document = inject(DOCUMENT);
69-
const pathname = (_document && _document.location && _document.location.pathname) || '';
70-
return {pathname};
69+
70+
return {
71+
// Note that this needs to be a function, because Angular will only instantiate
72+
// this provider once, but we want the current location on each call.
73+
getPathname: () => (_document && _document.location && _document.location.pathname) || ''
74+
};
7175
}
7276

7377

@@ -114,7 +118,7 @@ export class MatProgressBar extends _MatProgressBarMixinBase implements CanColor
114118
// we can't tell the difference between whether
115119
// the consumer is using the hash location strategy or not, because `Location` normalizes
116120
// both `/#/foo/bar` and `/foo/bar` to the same thing.
117-
const path = location && location.pathname ? location.pathname.split('#')[0] : '';
121+
const path = location ? location.getPathname().split('#')[0] : '';
118122
this._rectangleFillValue = `url('${path}#${this.progressbarId}')`;
119123
this._isNoopAnimation = _animationMode === 'NoopAnimations';
120124
}

0 commit comments

Comments
 (0)