Angular Ad-Block Detection Service

At times users have browser plugins installed that can block network requests for resources that are either ads or blacklisted as such. Often times this can cause issues with other code that depends on these resources to load. This code references the PlatformService which was introduced here.

import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { PlatformService } from './platform.service'
import { Observable } from 'rxjs/Observable'

export interface IAdblockService {
  adBlockerIsActive$: Observable
}

@Injectable()
export class AdblockService implements IAdblockService {
  public adBlockerIsActive$ = this.platformService.isBrowser
    ? this.http.get('./ad-server.js')
      .switchMap(a => Observable.of(false))
      .catch(a => Observable.of(true))
    : Observable.of(false)

  constructor(private platformService: PlatformService, private http: HttpClient) { }
}

with the accompanying unit tests

import { IPlatformService, PlatformService } from './platform.service'
import { AdblockService, IAdblockService } from './adblock.service'
import { async, TestBed } from '@angular/core/testing'
import { HttpClientModule } from '@angular/common/http'
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'
import '../../../operators'
 
describe(AdblockService.name, () => {
  let service: IAdblockService
  let httpMock: HttpTestingController
 
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule, HttpClientModule],
      providers: [
        AdblockService,
        { provide: PlatformService, useValue: new MockPlatformService() }
      ]
    })
  }))
 
  beforeEach(() => {
    service = TestBed.get(AdblockService)
    httpMock = TestBed.get(HttpTestingController)
  })
 
  it('should detect adblock is present', async(() => {
    service.adBlockerIsActive$.subscribe(isPresent => {
      expect(isPresent).toBeTruthy()
    })
 
    const req = httpMock.expectOne(r => r.url === './ad-server.js')
    expect(req.request.method).toEqual('GET')
 
    req.flush('', { status: 404, statusText: 'not found'})
    httpMock.verify()
  }))
 
  it('should detect adblock is not present', async(() => {
    service.adBlockerIsActive$.subscribe(isPresent => {
      expect(isPresent).toBeFalsy()
    })
 
    const req = httpMock.expectOne(r => r.url === './ad-server.js')
    expect(req.request.method).toEqual('GET')
 
    req.flush('', { status: 200, statusText: 'not found'})
    httpMock.verify()
  }))
 
  it('should return false when not on platform browser', async(() => {
    const ps = TestBed.get(PlatformService) as MockPlatformService
    ps.isServer = true
    service.adBlockerIsActive$.subscribe(result => expect(result).toBe(false))
  }))
})
 
class MockPlatformService implements IPlatformService {
  public isBrowser = true
  public isServer = false
}