Skip to content

fix(progress-bar): not taking current path after first initialization #13628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 19, 2018
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
23 changes: 21 additions & 2 deletions src/lib/progress-bar/progress-bar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ import {MatProgressBar} from './progress-bar';


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

function createComponent<T>(componentType: Type<T>,
imports?: Array<Type<{}>>): ComponentFixture<T> {
fakePath = '/fake-path';

TestBed.configureTestingModule({
imports: imports || [MatProgressBarModule],
declarations: [componentType],
providers: [{
provide: MAT_PROGRESS_BAR_LOCATION,
useValue: {pathname: fakePath}
useValue: {getPathname: () => fakePath}
}]
}).compileComponents();

Expand Down Expand Up @@ -122,6 +124,23 @@ describe('MatProgressBar', () => {
const svg = fixture.debugElement.query(By.css('svg')).nativeElement;
expect(svg.getAttribute('focusable')).toBe('false');
});

it('should use latest path when prefixing the SVG references', () => {
let fixture = createComponent(BasicProgressBar);
fixture.detectChanges();

let rect = fixture.debugElement.query(By.css('rect')).nativeElement;
expect(rect.getAttribute('fill')).toMatch(/^url\(['"]?\/fake-path#.*['"]?\)$/);

fixture.destroy();
fakePath = '/another-fake-path';

fixture = TestBed.createComponent(BasicProgressBar);
fixture.detectChanges();
rect = fixture.debugElement.query(By.css('rect')).nativeElement;

expect(rect.getAttribute('fill')).toMatch(/^url\(['"]?\/another-fake-path#.*['"]?\)$/);
});
});

describe('animation trigger on determinate setting', () => {
Expand Down
12 changes: 8 additions & 4 deletions src/lib/progress-bar/progress-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,18 @@ export const MAT_PROGRESS_BAR_LOCATION = new InjectionToken<MatProgressBarLocati
* @docs-private
*/
export interface MatProgressBarLocation {
pathname: string;
getPathname: () => string;
}

/** @docs-private */
export function MAT_PROGRESS_BAR_LOCATION_FACTORY(): MatProgressBarLocation {
const _document = inject(DOCUMENT);
const pathname = (_document && _document.location && _document.location.pathname) || '';
return {pathname};

return {
// Note that this needs to be a function, because Angular will only instantiate
// this provider once, but we want the current location on each call.
getPathname: () => (_document && _document.location && _document.location.pathname) || ''
};
}


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