Skip to content

Commit

Permalink
Fix issue when adding a source a second time (#198)
Browse files Browse the repository at this point in the history
* Removes unwanted bind call and add a test

* Added changelog automatic support

* Update contributing guide for release
  • Loading branch information
HarelM authored Sep 29, 2024
1 parent 3f12441 commit 675d63b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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()) {
<mgl-geojson-source id="123"></mgl-geojson-source>
}
`,
standalone: true,
imports: [GeoJSONSourceComponent]
})
class GeoJSONSourceTestComponent {
private show = signal<boolean>(true);

public toggle() {
this.show.set(!this.show());
}
}

describe('GeoJSONSourceComponent', () => {
let mapServiceStub: jasmine.SpyObj<MapService>;
let component: GeoJSONSourceTestComponent;
let fixture: ComponentFixture<GeoJSONSourceTestComponent>;

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();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 675d63b

Please sign in to comment.