fix(aio): intercept all clicks on anchors
Previously we had the `LinkDirective` which intercepted clicks on anchors outside the doc viewer. Now we intercept "all" link clicks within the app.
This commit is contained in:

committed by
Chuck Jazdzewski

parent
3f7cfde476
commit
eaa04354d5
@ -5,10 +5,13 @@ import { AppModule } from './app.module';
|
||||
import { GaService } from 'app/shared/ga.service';
|
||||
import { SearchService } from 'app/search/search.service';
|
||||
import { MockSearchService } from 'testing/search.service';
|
||||
import { LocationService } from 'app/shared/location.service';
|
||||
import { MockLocationService } from 'testing/location.service';
|
||||
|
||||
describe('AppComponent', () => {
|
||||
let component: AppComponent;
|
||||
let fixture: ComponentFixture<AppComponent>;
|
||||
const initialUrl = 'a/b';
|
||||
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
@ -16,7 +19,8 @@ describe('AppComponent', () => {
|
||||
providers: [
|
||||
{ provide: APP_BASE_HREF, useValue: '/' },
|
||||
{ provide: SearchService, useClass: MockSearchService },
|
||||
{ provide: GaService, useClass: TestGaService }
|
||||
{ provide: GaService, useClass: TestGaService },
|
||||
{ provide: LocationService, useFactory: () => new MockLocationService(initialUrl) }
|
||||
]
|
||||
});
|
||||
TestBed.compileComponents();
|
||||
@ -33,11 +37,10 @@ describe('AppComponent', () => {
|
||||
|
||||
describe('google analytics', () => {
|
||||
it('should call gaService.locationChanged with initial URL', () => {
|
||||
const url = window.location.pathname.substr(1); // strip leading '/'
|
||||
const { locationChanged } = TestBed.get(GaService) as TestGaService;
|
||||
expect(locationChanged.calls.count()).toBe(1, 'gaService.locationChanged');
|
||||
const args = locationChanged.calls.first().args;
|
||||
expect(args[0]).toBe(url);
|
||||
expect(args[0]).toBe(initialUrl);
|
||||
});
|
||||
|
||||
// Todo: add test to confirm tracking URL when navigate.
|
||||
@ -70,6 +73,17 @@ describe('AppComponent', () => {
|
||||
expect(searchService.loadIndex).toHaveBeenCalled();
|
||||
}));
|
||||
});
|
||||
|
||||
describe('click intercepting', () => {
|
||||
it('should intercept clicks on anchors and call `location.handleAnchorClick()`',
|
||||
inject([LocationService], (location: LocationService) => {
|
||||
const anchorElement: HTMLAnchorElement = document.createElement('a');
|
||||
anchorElement.href = 'some/local/url';
|
||||
fixture.nativeElement.append(anchorElement);
|
||||
anchorElement.click();
|
||||
expect(location.handleAnchorClick).toHaveBeenCalledWith(anchorElement, 0, false, false);
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
class TestGaService {
|
||||
|
Reference in New Issue
Block a user