|
1 |
| -describe.skip('MapContainerComponent', () => { |
2 |
| - // empty |
| 1 | +import { |
| 2 | + ComponentFixture, |
| 3 | + discardPeriodicTasks, |
| 4 | + fakeAsync, |
| 5 | + TestBed, |
| 6 | + tick, |
| 7 | +} from '@angular/core/testing' |
| 8 | +import { MockBuilder } from 'ng-mocks' |
| 9 | +import { |
| 10 | + mapCtxFixture, |
| 11 | + mapCtxLayerWmsFixture, |
| 12 | + mapCtxLayerXyzFixture, |
| 13 | +} from '@geonetwork-ui/common/fixtures' |
| 14 | +import { applyContextDiffToMap } from '@geospatial-sdk/openlayers' |
| 15 | +import { MapContainerComponent } from './map-container.component' |
| 16 | +import { computeMapContextDiff } from '@geospatial-sdk/core' |
| 17 | + |
| 18 | +jest.mock('@geospatial-sdk/core', () => ({ |
| 19 | + computeMapContextDiff: jest.fn(() => ({ |
| 20 | + 'this is': 'a diff', |
| 21 | + })), |
| 22 | +})) |
| 23 | + |
| 24 | +jest.mock('@geospatial-sdk/openlayers', () => ({ |
| 25 | + applyContextDiffToMap: jest.fn(), |
| 26 | + createMapFromContext: jest.fn(() => Promise.resolve(new OpenLayersMapMock())), |
| 27 | + listen: jest.fn(), |
| 28 | +})) |
| 29 | + |
| 30 | +let mapmutedCallback |
| 31 | +let movestartCallback |
| 32 | +let singleclickCallback |
| 33 | +class OpenLayersMapMock { |
| 34 | + _size = undefined |
| 35 | + setTarget = jest.fn() |
| 36 | + updateSize() { |
| 37 | + this._size = [100, 100] |
| 38 | + } |
| 39 | + getSize() { |
| 40 | + return this._size |
| 41 | + } |
| 42 | + on(type, callback) { |
| 43 | + if (type === 'mapmuted') { |
| 44 | + mapmutedCallback = callback |
| 45 | + } |
| 46 | + if (type === 'movestart') { |
| 47 | + movestartCallback = callback |
| 48 | + } |
| 49 | + if (type === 'singleclick') { |
| 50 | + singleclickCallback = callback |
| 51 | + } |
| 52 | + } |
| 53 | + off() { |
| 54 | + // do nothing! |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +const defaultBaseMap = { |
| 59 | + attributions: |
| 60 | + '<span>© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, © <a href="https://carto.com/">Carto</a></span>', |
| 61 | + type: 'xyz', |
| 62 | + url: 'https://{a-c}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}.png', |
| 63 | +} |
| 64 | + |
| 65 | +describe('MapContainerComponent', () => { |
| 66 | + let component: MapContainerComponent |
| 67 | + let fixture: ComponentFixture<MapContainerComponent> |
| 68 | + |
| 69 | + beforeEach(() => { |
| 70 | + jest.clearAllMocks() |
| 71 | + }) |
| 72 | + |
| 73 | + beforeEach(() => { |
| 74 | + return MockBuilder(MapContainerComponent) |
| 75 | + }) |
| 76 | + |
| 77 | + beforeEach(async () => { |
| 78 | + await TestBed.configureTestingModule({}).compileComponents() |
| 79 | + }) |
| 80 | + |
| 81 | + beforeEach(() => { |
| 82 | + fixture = TestBed.createComponent(MapContainerComponent) |
| 83 | + component = fixture.componentInstance |
| 84 | + fixture.detectChanges() |
| 85 | + }) |
| 86 | + |
| 87 | + it('creates', () => { |
| 88 | + expect(component).toBeTruthy() |
| 89 | + }) |
| 90 | + |
| 91 | + describe('#processContext', () => { |
| 92 | + it('returns a default context if null provided', () => { |
| 93 | + expect(component.processContext(null)).toEqual({ |
| 94 | + layers: [defaultBaseMap], |
| 95 | + view: { |
| 96 | + center: [0, 15], |
| 97 | + zoom: 2, |
| 98 | + }, |
| 99 | + }) |
| 100 | + }) |
| 101 | + it('adds base layers to context', () => { |
| 102 | + const context = { |
| 103 | + layers: [mapCtxLayerWmsFixture()], |
| 104 | + view: null, |
| 105 | + } |
| 106 | + expect(component.processContext(context)).toEqual({ |
| 107 | + layers: [defaultBaseMap, mapCtxLayerWmsFixture()], |
| 108 | + view: { |
| 109 | + center: [0, 15], |
| 110 | + zoom: 2, |
| 111 | + }, |
| 112 | + }) |
| 113 | + }) |
| 114 | + it('uses provided basemaps if any', () => { |
| 115 | + component['basemapLayers'] = [mapCtxLayerXyzFixture()] |
| 116 | + const context = { layers: [], view: null } |
| 117 | + expect(component.processContext(context)).toEqual({ |
| 118 | + layers: [defaultBaseMap, mapCtxLayerXyzFixture()], |
| 119 | + view: { |
| 120 | + center: [0, 15], |
| 121 | + zoom: 2, |
| 122 | + }, |
| 123 | + }) |
| 124 | + }) |
| 125 | + it('does not use the default base layer if specified', () => { |
| 126 | + component['doNotUseDefaultBasemap'] = true |
| 127 | + const context = { layers: [mapCtxLayerXyzFixture()], view: null } |
| 128 | + expect(component.processContext(context)).toEqual({ |
| 129 | + layers: [mapCtxLayerXyzFixture()], |
| 130 | + view: { |
| 131 | + center: [0, 15], |
| 132 | + zoom: 2, |
| 133 | + }, |
| 134 | + }) |
| 135 | + }) |
| 136 | + it('applies map constraints if any', () => { |
| 137 | + component['mapViewConstraints'] = { |
| 138 | + maxZoom: 18, |
| 139 | + maxExtent: [10, 20, 30, 40], |
| 140 | + } |
| 141 | + const context = { layers: [mapCtxLayerXyzFixture()], view: null } |
| 142 | + expect(component.processContext(context)).toEqual({ |
| 143 | + layers: [defaultBaseMap, mapCtxLayerXyzFixture()], |
| 144 | + view: { |
| 145 | + center: [0, 15], |
| 146 | + zoom: 2, |
| 147 | + maxExtent: [10, 20, 30, 40], |
| 148 | + maxZoom: 18, |
| 149 | + }, |
| 150 | + }) |
| 151 | + }) |
| 152 | + }) |
| 153 | + |
| 154 | + describe('#afterViewInit', () => { |
| 155 | + beforeEach(async () => { |
| 156 | + await component.ngAfterViewInit() |
| 157 | + }) |
| 158 | + it('creates a map', () => { |
| 159 | + expect(component.olMap).toBeInstanceOf(OpenLayersMapMock) |
| 160 | + }) |
| 161 | + describe('display message that map navigation has been muted', () => { |
| 162 | + let messageDisplayed |
| 163 | + beforeEach(() => { |
| 164 | + messageDisplayed = null |
| 165 | + component.displayMessage$.subscribe( |
| 166 | + (value) => (messageDisplayed = value) |
| 167 | + ) |
| 168 | + }) |
| 169 | + it('mapmuted event displays message after 300ms (delay for eventually hiding message)', fakeAsync(() => { |
| 170 | + mapmutedCallback() |
| 171 | + tick(400) |
| 172 | + expect(messageDisplayed).toEqual(true) |
| 173 | + discardPeriodicTasks() |
| 174 | + })) |
| 175 | + it('message goes away after 2s', fakeAsync(() => { |
| 176 | + mapmutedCallback() |
| 177 | + tick(2500) |
| 178 | + expect(messageDisplayed).toEqual(false) |
| 179 | + discardPeriodicTasks() |
| 180 | + })) |
| 181 | + it('message does not display if map fires movestart event', fakeAsync(() => { |
| 182 | + movestartCallback() |
| 183 | + tick(300) |
| 184 | + expect(messageDisplayed).toEqual(false) |
| 185 | + discardPeriodicTasks() |
| 186 | + })) |
| 187 | + it('message does not display if map fires singleclick event', fakeAsync(() => { |
| 188 | + singleclickCallback() |
| 189 | + tick(300) |
| 190 | + expect(messageDisplayed).toEqual(false) |
| 191 | + discardPeriodicTasks() |
| 192 | + })) |
| 193 | + }) |
| 194 | + }) |
| 195 | + |
| 196 | + describe('#ngOnChanges', () => { |
| 197 | + beforeEach(async () => { |
| 198 | + await component.ngAfterViewInit() |
| 199 | + }) |
| 200 | + it('updates the map with the new context', async () => { |
| 201 | + const newContext = { |
| 202 | + ...mapCtxFixture(), |
| 203 | + layers: [mapCtxLayerWmsFixture()], |
| 204 | + } |
| 205 | + await component.ngOnChanges({ |
| 206 | + context: { |
| 207 | + currentValue: mapCtxFixture(), |
| 208 | + previousValue: newContext, |
| 209 | + firstChange: false, |
| 210 | + isFirstChange: () => false, |
| 211 | + }, |
| 212 | + }) |
| 213 | + expect(computeMapContextDiff).toHaveBeenCalledWith( |
| 214 | + { |
| 215 | + layers: [defaultBaseMap, ...mapCtxFixture().layers], |
| 216 | + view: mapCtxFixture().view, |
| 217 | + }, |
| 218 | + { |
| 219 | + layers: [defaultBaseMap, mapCtxLayerWmsFixture()], |
| 220 | + view: mapCtxFixture().view, |
| 221 | + } |
| 222 | + ) |
| 223 | + expect(applyContextDiffToMap).toHaveBeenCalledWith(component.olMap, { |
| 224 | + 'this is': 'a diff', |
| 225 | + }) |
| 226 | + }) |
| 227 | + }) |
3 | 228 | })
|
0 commit comments