diff --git a/projects/ngx-maplibre-gl/src/lib/source/geojson/geojson-source.component.spec.ts b/projects/ngx-maplibre-gl/src/lib/source/geojson/geojson-source.component.spec.ts new file mode 100644 index 00000000..74d7c888 --- /dev/null +++ b/projects/ngx-maplibre-gl/src/lib/source/geojson/geojson-source.component.spec.ts @@ -0,0 +1,80 @@ +import { Component, signal } from "@angular/core"; +import { ComponentFixture, TestBed, waitForAsync } from "@angular/core/testing"; +import { of } from "rxjs"; +import { MapService } from "../../map/map.service"; +import { GeoJSONSourceComponent } from "./geojson-source.component"; + +const getMapServiceStub = () => + jasmine.createSpyObj( + [ + 'addSource', + 'removeSource' + ], + { + mapLoaded$: of(true), + mapInstance: new (class { + on() {} + off() {} + getLayer() {} + })(), + } + ); + +@Component({ + template: ` + @if (show()) { + + } + `, + standalone: true, + imports: [GeoJSONSourceComponent] +}) +class GeoJSONSourceTestComponent { + private show = signal(true); + + public toggle() { + this.show.set(!this.show()); + } +} + +describe('GeoJSONSourceComponent', () => { + let mapServiceStub: jasmine.SpyObj; + let component: GeoJSONSourceTestComponent; + let fixture: ComponentFixture; + + beforeEach(waitForAsync(() => { + mapServiceStub = getMapServiceStub(); + + TestBed.configureTestingModule({ + imports: [GeoJSONSourceTestComponent], + providers: [{ provide: MapService, useValue: mapServiceStub }], + }) + .overrideComponent(GeoJSONSourceTestComponent, { + set: { + providers: [{ provide: MapService, useValue: mapServiceStub }], + }, + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(GeoJSONSourceTestComponent); + component = fixture.componentInstance; + }); + + describe('Init/Destroy tests', () => { + beforeEach(() => { + fixture.detectChanges(); + }); + + it('should call add source when init', () => { + expect(mapServiceStub.addSource).toHaveBeenCalled(); + }); + + it('should remove source on destroy', () => { + component.toggle(); + fixture.detectChanges(); + expect(mapServiceStub.removeSource).toHaveBeenCalled(); + }); + }); +}); \ No newline at end of file diff --git a/projects/ngx-maplibre-gl/src/lib/source/source.directive.ts b/projects/ngx-maplibre-gl/src/lib/source/source.directive.ts index 9ff237a9..8297bc86 100644 --- a/projects/ngx-maplibre-gl/src/lib/source/source.directive.ts +++ b/projects/ngx-maplibre-gl/src/lib/source/source.directive.ts @@ -32,7 +32,7 @@ export class SourceDirective implements OnInit { readonly loadSource$ = this.loadSourceSubject.asObservable(); constructor() { - this.destroyRef.onDestroy(() => this.removeSource.bind(this)); + this.destroyRef.onDestroy(() => this.removeSource()); } ngOnInit() {