Skip to content

Commit

Permalink
chore: fix broken tests for layer actions
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbasgaard committed Oct 9, 2023
1 parent b6e924a commit 7c60cdf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
36 changes: 21 additions & 15 deletions src/store/map/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,27 +239,33 @@ export const actions = {
}
},

loadLayerCollection(
async loadLayerCollection(
{ commit },
{ collectionUrl, setCollectionCommit, addLayerCommit, datasetId }
) {
// Retrieve a layer collection and it's underlaying collection
getCatalog(collectionUrl).then(dataset => {
dataset.layers = []
// Retrieve a layer collection and it's underlaying collectiongit
await getCatalog(collectionUrl).then(async dataset => {
const itemLinks = _.get(dataset, 'links')
const items = itemLinks.filter(child => child.rel === 'item')
const layers = []
items.forEach((item, index) => {
getCatalog(item.href)
.then(decorateLayerStyling)
.then(layerData => {
layers.push(layerData)
if (index === items.length - 1) {
dataset.layers = layers
commit(setCollectionCommit, { id: datasetId, data: dataset })
}
})
})

await Promise.all(
items.map(async item =>
getCatalog(item.href)
.then(decorateLayerStyling)
.then(layerData => {
layers.push(layerData)
})
)
)

if (layers.length > 0) {
dataset.layers = layers
commit(setCollectionCommit, {
id: datasetId,
data: dataset
})
}
})
},

Expand Down
13 changes: 11 additions & 2 deletions tests/unit/store/map/index/actions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { actions } from '@/store/map/index.js'
import getCatalog from '@/lib/request/get-catalog'

jest.mock('@/lib/request/get-catalog')
jest.mock('@/lib/mapbox/styling/index.js', () => ({
decorateLayerStyling: jest
.fn()
.mockImplementation(input => Promise.resolve(input))
}))

describe('loadDatasets', () => {
test('fetches datasets and stores them', async () => {
Expand Down Expand Up @@ -218,12 +223,16 @@ describe('loadLayerCollection', () => {
getCatalog.mockResolvedValueOnce(apiResult)
getCatalog.mockResolvedValue('bar')

const childs = await actions.loadLayerCollection(
// Call the async function and await its resolution
await actions.loadLayerCollection(
{ commit },
{ collectionUrl: 'url', setCollectionCommit: 'foo', datasetId: 'par1' }
)
await childs

// Modify apiResult after the async operation is done
apiResult.layers = ['bar', 'bar']

// Expect commit to be called with the modified data
expect(commit.mock.calls[0]).toEqual([
'foo',
{ id: 'par1', data: apiResult }
Expand Down

0 comments on commit 7c60cdf

Please sign in to comment.