-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support YMapLayer component (#249)
- Loading branch information
Showing
4 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...ngular-yandex-maps-v3/src/lib/components/layers/y-map-layer/y-map-layer.directive.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { Component, ViewChild } from '@angular/core'; | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { YMapLayerProps } from '@yandex/ymaps3-types'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
|
||
import { | ||
mockYMapInstance, | ||
mockYMapLayerConstructor, | ||
mockYMapLayerInstance, | ||
} from '../../../../test-utils'; | ||
import { YReadyEvent } from '../../../types/y-ready-event'; | ||
import { YMapComponent } from '../../common/y-map/y-map.component'; | ||
import { YMapLayerDirective } from './y-map-layer.directive'; | ||
|
||
@Component({ | ||
standalone: true, | ||
imports: [YMapLayerDirective], | ||
template: '<y-map-layer [props]="props" />', | ||
}) | ||
class MockHostComponent { | ||
@ViewChild(YMapLayerDirective, { static: true }) | ||
layer!: YMapLayerDirective; | ||
|
||
props: YMapLayerProps = { | ||
type: 'markers', | ||
}; | ||
} | ||
|
||
describe('YMapLayerDirective', () => { | ||
let component: YMapLayerDirective; | ||
let mockComponent: MockHostComponent; | ||
let fixture: ComponentFixture<MockHostComponent>; | ||
|
||
let mapInstance: ReturnType<typeof mockYMapInstance>; | ||
let layerInstance: ReturnType<typeof mockYMapLayerInstance>; | ||
let layerConstructorMock: jest.Mock; | ||
|
||
beforeEach(async () => { | ||
mapInstance = mockYMapInstance(); | ||
|
||
await TestBed.configureTestingModule({ | ||
imports: [MockHostComponent], | ||
providers: [ | ||
{ | ||
provide: YMapComponent, | ||
useValue: { | ||
map$: new BehaviorSubject(mapInstance), | ||
}, | ||
}, | ||
], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(MockHostComponent); | ||
mockComponent = fixture.componentInstance; | ||
component = mockComponent.layer; | ||
|
||
layerInstance = mockYMapLayerInstance(); | ||
layerConstructorMock = mockYMapLayerConstructor(layerInstance); | ||
}); | ||
|
||
afterEach(() => { | ||
(window as any).ymaps3 = undefined; | ||
}); | ||
|
||
it('should create entity', () => { | ||
fixture.detectChanges(); | ||
|
||
expect(layerConstructorMock).toHaveBeenCalledWith(mockComponent.props); | ||
expect(mapInstance.addChild).toHaveBeenCalledWith(layerInstance); | ||
}); | ||
|
||
it('should emit ready on load', () => { | ||
jest.spyOn(component.ready, 'emit'); | ||
fixture.detectChanges(); | ||
|
||
const readyEvent: YReadyEvent = { | ||
ymaps3: (window as any).ymaps3, | ||
entity: layerInstance, | ||
}; | ||
|
||
expect(component.ready.emit).toHaveBeenCalledWith(readyEvent); | ||
}); | ||
|
||
it('should pass inputs to constructor', () => { | ||
const props: YMapLayerProps = { | ||
type: 'ground', | ||
}; | ||
|
||
mockComponent.props = props; | ||
|
||
fixture.detectChanges(); | ||
|
||
expect(layerConstructorMock).toHaveBeenCalledWith(props); | ||
}); | ||
|
||
it('should update props input after init', () => { | ||
fixture.detectChanges(); | ||
|
||
const props: YMapLayerProps = { | ||
type: 'features', | ||
zIndex: 10, | ||
}; | ||
|
||
mockComponent.props = props; | ||
|
||
fixture.detectChanges(); | ||
|
||
expect(layerInstance.update).toHaveBeenCalledWith(props); | ||
}); | ||
}); |
84 changes: 84 additions & 0 deletions
84
libs/angular-yandex-maps-v3/src/lib/components/layers/y-map-layer/y-map-layer.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { | ||
Directive, | ||
EventEmitter, | ||
Input, | ||
OnChanges, | ||
OnDestroy, | ||
OnInit, | ||
Output, | ||
SimpleChanges, | ||
} from '@angular/core'; | ||
import { YMapLayer, YMapLayerProps } from '@yandex/ymaps3-types'; | ||
import { Subject, takeUntil } from 'rxjs'; | ||
import { filter } from 'rxjs/operators'; | ||
|
||
import { YReadyEvent } from '../../../types/y-ready-event'; | ||
import { YMapComponent } from '../../common/y-map/y-map.component'; | ||
|
||
/** | ||
* This component wraps the [ymaps3.YMapLayer](https://yandex.ru/dev/jsapi30/doc/ru/ref/#class-ymaplayer) class from the Yandex.Maps API. | ||
* All component inputs are named the same as the API class constructor arguments. | ||
* | ||
* ```html | ||
* <y-map | ||
* [props]="{ | ||
* location: { | ||
* center: [-0.127696, 51.507351], | ||
* zoom: 10, | ||
* }, | ||
* }" | ||
* > | ||
* <y-map-default-scheme-layer /> | ||
* | ||
* <y-map-layer | ||
* [props]="{ | ||
* type: 'markers', | ||
* zIndex: 1800, | ||
* }" | ||
* /> | ||
* </y-map> | ||
* ``` | ||
*/ | ||
@Directive({ | ||
selector: 'y-map-layer', | ||
standalone: true, | ||
}) | ||
export class YMapLayerDirective implements OnInit, OnDestroy, OnChanges { | ||
private readonly destroy$ = new Subject<void>(); | ||
|
||
private layer?: YMapLayer; | ||
|
||
/** | ||
* See the API entity documentation for detailed information. Supports ngOnChanges. | ||
* {@link https://yandex.ru/dev/jsapi30/doc/ru/ref/#YMapLayerProps} | ||
*/ | ||
@Input({ required: true }) props!: YMapLayerProps; | ||
|
||
/** | ||
* The entity instance is created. This event runs outside an Angular zone. | ||
*/ | ||
@Output() ready: EventEmitter<YReadyEvent<YMapLayer>> = new EventEmitter< | ||
YReadyEvent<YMapLayer> | ||
>(); | ||
|
||
constructor(private readonly yMapComponent: YMapComponent) {} | ||
|
||
ngOnInit() { | ||
this.yMapComponent.map$.pipe(filter(Boolean), takeUntil(this.destroy$)).subscribe((map) => { | ||
this.layer = new ymaps3.YMapLayer(this.props); | ||
map.addChild(this.layer); | ||
this.ready.emit({ ymaps3, entity: this.layer }); | ||
}); | ||
} | ||
|
||
ngOnChanges(changes: SimpleChanges) { | ||
if (this.layer) { | ||
this.layer.update(changes['props'].currentValue); | ||
} | ||
} | ||
|
||
ngOnDestroy() { | ||
this.destroy$.next(); | ||
this.destroy$.complete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters