Skip to content

Commit

Permalink
Updated Unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ronitjadhav committed May 6, 2024
1 parent dbdf6b7 commit 5382b5e
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ComponentFixture, TestBed } from '@angular/core/testing'
import { AddLayerFromOgcApiComponent } from './add-layer-from-ogc-api.component'
import { MapFacade } from '../+state/map.facade'
import { TranslateModule } from '@ngx-translate/core'
import { NO_ERRORS_SCHEMA } from '@angular/core'
import { MapContextLayerTypeEnum } from '../map-context/map-context.model'
Expand All @@ -9,6 +8,9 @@ jest.mock('@camptocamp/ogc-client', () => ({
OgcApiEndpoint: class {
constructor(private url) {}
isReady() {
if (this.url === 'http://example.com/ogc') {
return Promise.resolve(this)
}
if (this.url.indexOf('error') > -1) {
return Promise.reject(new Error('Something went wrong'))
}
Expand All @@ -19,17 +21,57 @@ jest.mock('@camptocamp/ogc-client', () => ({
}
return Promise.resolve(this)
}
get featureCollections() {
get allCollections() {
if (this.url === 'http://example.com/ogc') {
return Promise.resolve([
{
name: 'NaturalEarth:physical:ne_10m_lakes_pluvial',
hasVectorTiles: true,
hasMapTiles: true,
},
{
name: 'NaturalEarth:physical:ne_10m_land_ocean_seams',
hasVectorTiles: true,
hasMapTiles: true,
},
])
}
if (this.url.includes('error')) {
return Promise.reject(new Error('Simulated loading error'))
}
return Promise.resolve(['layer1', 'layer2', 'layer3'])
return Promise.resolve([
{
name: 'NaturalEarth:physical:ne_10m_lakes_pluvial',
hasVectorTiles: true,
hasMapTiles: true,
},
{
name: 'NaturalEarth:physical:ne_10m_land_ocean_seams',
hasVectorTiles: true,
hasMapTiles: true,
},
])
}
getCollectionItemsUrl(collectionId) {
if (this.url === 'http://example.com/ogc') {
return Promise.resolve(
`http://example.com/collections/${collectionId}/items`
)
}
return Promise.resolve(
`http://example.com/collections/${collectionId}/items`
)
}
getVectorTilesetUrl(collectionId) {
return Promise.resolve(
`http://example.com/collections/${collectionId}/tiles/vector`
)
}
getMapTilesetUrl(collectionId) {
return Promise.resolve(
`http://example.com/collections/${collectionId}/tiles/map`
)
}
},
}))

Expand Down Expand Up @@ -68,7 +110,18 @@ describe('AddLayerFromOgcApiComponent', () => {
await component.loadLayers()
expect(component.errorMessage).toBeFalsy()
expect(component.loading).toBe(false)
expect(component.layers).toEqual(['layer1', 'layer2', 'layer3'])
expect(component.layers).toEqual([
{
name: 'NaturalEarth:physical:ne_10m_lakes_pluvial',
hasVectorTiles: true,
hasMapTiles: true,
},
{
name: 'NaturalEarth:physical:ne_10m_land_ocean_seams',
hasVectorTiles: true,
hasMapTiles: true,
},
])
})

it('should handle errors while loading layers', async () => {
Expand All @@ -79,4 +132,40 @@ describe('AddLayerFromOgcApiComponent', () => {
expect(component.layers.length).toBe(0)
})
})

describe('Add Collection', () => {
it('should add feature type collection to map', async () => {
const layerAddedSpy = jest.spyOn(component.layerAdded, 'emit')
await component.addLayer('layer1', 'features')
expect(layerAddedSpy).toHaveBeenCalledWith({
name: 'layer1',
url: 'http://example.com/collections/layer1/items',
type: MapContextLayerTypeEnum.OGCAPI,
layerType: 'features',
title: 'layer1',
})
})
it('should add vector tile collection to map', async () => {
const layerAddedSpy = jest.spyOn(component.layerAdded, 'emit')
await component.addLayer('layer1', 'vectorTiles')
expect(layerAddedSpy).toHaveBeenCalledWith({
name: 'layer1',
url: 'http://example.com/collections/layer1/tiles/vector',
type: MapContextLayerTypeEnum.OGCAPI,
layerType: 'vectorTiles',
title: 'layer1',
})
})
it('should add map tile collection to map', async () => {
const layerAddedSpy = jest.spyOn(component.layerAdded, 'emit')
await component.addLayer('layer1', 'mapTiles')
expect(layerAddedSpy).toHaveBeenCalledWith({
name: 'layer1',
url: 'http://example.com/collections/layer1/tiles/map',
type: MapContextLayerTypeEnum.OGCAPI,
layerType: 'mapTiles',
title: 'layer1',
})
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ export class AddLayerFromOgcApiComponent implements OnInit {

async addLayer(layer: string, layerType: any) {
try {
console.log(layerType)
const ogcEndpoint = await new OgcApiEndpoint(this.ogcUrl)
let layerUrl
let layerUrl: string

if (layerType === 'vectorTiles') {
layerUrl = await ogcEndpoint.getVectorTilesetUrl(layer)
Expand All @@ -121,7 +120,6 @@ export class AddLayerFromOgcApiComponent implements OnInit {
})
}

// const layerUrl = await ogcEndpoint.getCollectionItemsUrl(layer, {outputFormat: 'json'})
const layerToAdd: MapContextLayerModel = {
name: layer,
url: layerUrl,
Expand Down

0 comments on commit 5382b5e

Please sign in to comment.