import { Component } from '@angular/core' ; import { inject , TestBed , ComponentFixtureAutoDetect , ComponentFixture } from '@angular/core/testing' ; import { LoaderService } from '../src/loader.service' ; import { AbmModule } from '../index' ; describe ( 'Service: LoaderService' , ( ) => { let fixture ; let el ; let htmlEl ; beforeEach ( ( ) => { TestBed . configureTestingModule ( { declarations : [ EmptyTestComponent ] , imports : [ AbmModule . forRoot ( { apiKey : '' } ) ] , providers : [ LoaderService , { provide : ComponentFixtureAutoDetect , useValue : true } ] , } ) ; fixture = TestBed . createComponent ( EmptyTestComponent ) ; el = fixture . nativeElement ; fixture . detectChanges ( ) ; htmlEl = findParent ( el , 'html' ) ; } ) ; it ( 'should create the default script URL' , inject ( [ LoaderService ] , ( loader ) => { loader . load ( ) ; let script ! ; const ls = htmlEl . querySelectorAll ( 'script' ) ; for ( let i = 0 ; i < ls . length ; i ++ ) { const node = ls [ i ] ; if ( node . src . indexOf ( 'api.map.baidu.com/api' ) !== - 1 ) { script = node ; } } expect ( script ) . not . toBeNull ( ) ; expect ( script . type ) . toEqual ( 'text/javascript' ) ; expect ( script . async ) . toEqual ( true ) ; expect ( script . defer ) . toEqual ( true ) ; expect ( script . src ) . toBeDefined ( ) ; } ) ) ; } ) ; function findParent ( el , selector ) { let retEl = null ; while ( el ) { if ( el . matches ( selector ) ) { retEl = el ; break ; } el = el . parentElement ; } return retEl ; } @ Component ( { template : '' } ) class EmptyTestComponent { }