Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 90 additions & 0 deletions static/src/js/zustand/slices/__tests__/createGranulesSlice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,95 @@ describe('createGranulesSlice', () => {
title: 'Something went wrong fetching granule search results'
}))
})

describe('when the collectionId matches the granules collectionConceptId', () => {
describe('when the collection is not OpenSearch', () => {
test('does not fetch granules', async () => {
// This test will fail if a network request is attempted.

useEdscStore.setState((state) => {
state.collection.collectionId = 'collectionId'
state.granules.granules.collectionConceptId = 'collectionId'
state.errors.handleError = vi.fn()
})

const { granules } = useEdscStore.getState()
await granules.getGranules()

const { errors } = useEdscStore.getState()
expect(errors.handleError).toHaveBeenCalledTimes(0)
})
})

describe('when the collection is OpenSearch', () => {
test('fetches granules', async () => {
nock(/localhost/)
.post(/opensearch\/granules/)
.reply(200, `<feed>
<totalResults>1</totalResults>
<entry>
<mockGranuleData>goes here</mockGranuleData>
</entry>
</feed>`)

useEdscStore.setState((state) => {
state.collection.collectionId = 'collectionId'
state.collection.collectionMetadata = {
collectionId: {
links: [{
rel: '/search#',
href: 'https://example.com/opensearch'
}]
}
}

state.granules.granules.collectionConceptId = 'collectionId'
state.errors.handleError = vi.fn()
})

const { granules } = useEdscStore.getState()
await granules.getGranules()

const { errors } = useEdscStore.getState()
expect(errors.handleError).toHaveBeenCalledTimes(0)

const {
granules: updatedGranules
} = useEdscStore.getState()

expect(updatedGranules.granules).toEqual({
collectionConceptId: 'collectionId',
count: 1,
isLoaded: true,
isLoading: false,
items: [{
browseFlag: false,
collectionConceptId: 'collectionId',
isOpenSearch: true,
mockGranuleData: 'goes here',
spatial: null
}],
loadTime: expect.any(Number)
})
})
})
})

describe('when collectionId is not set', () => {
test('does not fetch granules', async () => {
// This test will fail if a network request is attempted.

useEdscStore.setState((state) => {
state.collection.collectionId = null
state.errors.handleError = vi.fn()
})

const { granules } = useEdscStore.getState()
await granules.getGranules()

const { errors } = useEdscStore.getState()
expect(errors.handleError).toHaveBeenCalledTimes(0)
})
})
})
})
5 changes: 3 additions & 2 deletions static/src/js/zustand/slices/createGranulesSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ const createGranulesSlice: ImmerStateCreator<GranulesSlice> = (set, get) => ({
// endpoints. This `if` ensures we don't double fetch CMR granules.
// TODO This is causing a double fetch for OpenSearch granules (both to the same OpenSearch endpoint)
// TODO when loaded from the search results. The first request gets cancelled, but it would be nice to avoid that
// When this double fetch is happening, it is possible the user has already moved to a new page and the `collectionId` no longer exists. When this happens we also need to return with no action.
if (
collectionId === zustandState.granules.granules.collectionConceptId
&& !isOpenSearch
!collectionId

@eudoroolivares2016 eudoroolivares2016 Aug 20, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we put this catch further up if (!collectionId) return on something like line 49? if the coll is empty do we need to execute anything else on this slice instead bail?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally had it further up but I was looking at what was triggering it, and the bug is because of the opensearch double trigger mentioned in the comments. So I moved it into this if statement. I meant to add details to that comment, I'll do that now

|| (collectionId === zustandState.granules.granules.collectionConceptId && !isOpenSearch)
) {
return
}
Expand Down
Loading