Skip to content

Commit

Permalink
MMT-3922: Created new file for testing purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
mandyparson committed Oct 18, 2024
1 parent 6e38c5f commit b1e6ef9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 52 deletions.
11 changes: 2 additions & 9 deletions static/src/js/components/MetadataForm/MetadataForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import useNotificationsContext from '@/js/hooks/useNotificationsContext'

import { INGEST_DRAFT } from '@/js/operations/mutations/ingestDraft'

import checkForCMRFetchDraftLag from '@/js/utils/checkForCMRFetchDraftLag'
import errorLogger from '@/js/utils/errorLogger'
import getConceptTypeByDraftConceptId from '@/js/utils/getConceptTypeByDraftConceptId'
import getFormSchema from '@/js/utils/getFormSchema'
Expand Down Expand Up @@ -142,15 +143,7 @@ const MetadataForm = () => {
const { draft: fetchedDraft } = data || {}
const { revisionId: cmrRetrievedRevisionId } = fetchedDraft || ''

if (cmrRetrievedRevisionId && (cmrRetrievedRevisionId !== appContextRevisionId)) {
addNotification({
message: 'Delay Detected',
variant: 'danger'
})

// Send the error to the errorLogger
errorLogger('A delay has been detected in CMR', 'MetadataForm: retrieveDraftUseEffect')
}
checkForCMRFetchDraftLag(cmrRetrievedRevisionId, appContextRevisionId)

setOriginalDraft(fetchedDraft)
setDraft(fetchedDraft)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -654,49 +654,6 @@ describe('MetadataForm', () => {
})
})

describe('when the saveType is save and continue and there is a lag in retreiving the data', () => {
test('throws an error', async () => {
const navigateSpy = vi.fn()
vi.spyOn(router, 'useNavigate').mockImplementation(() => navigateSpy)

const { user } = setup({
additionalMocks: [{
request: {
query: INGEST_DRAFT,
variables: {
conceptType: 'Tool',
metadata: {
LongName: 'Long Name',
MetadataSpecification: {
URL: 'https://cdn.earthdata.nasa.gov/umm/tool/v1.1',
Name: 'UMM-T',
Version: '1.1'
}
},
nativeId: 'MMT_2331e312-cbbc-4e56-9d6f-fe217464be2c',
providerId: 'MMT_2',
ummVersion: '1.0.0'
}
},
result: {
data: {
ingestDraft: {
conceptId: 'TD1000000-MMT',
revisionId: '3'
}
}
}
}]
})

const button = await screen.findByRole('button', { name: 'Save & Continue' })
await user.click(button)
expect(navigateSpy).toHaveBeenCalledTimes(1)

expect(errorLogger).toHaveBeenCalledTimes(1)
})
})

describe('when the saveType is save and preview', () => {
test('navigates to the current form and calls scrolls to the top', async () => {
const navigateSpy = vi.fn()
Expand Down
15 changes: 15 additions & 0 deletions static/src/js/utils/__tests__/checkForCMRFetchDraftLag.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import checkForCMRFetchDraftLag from '../checkForCMRFetchDraftLag'

describe('checkForCMRFetchDraftLag', () => {
describe('when the fetched revision Id and expected revision Id are undefined', () => {
test('returns undefined', () => {
expect(checkForCMRFetchDraftLag(`${undefined}`, `${undefined}`)).toEqual(undefined)
})
})

describe('when the fetched revision Id does not match expected revision Id', () => {
test('throws error', () => {
expect(() => checkForCMRFetchDraftLag('1', '2')).toThrow('Delay in CMR has detected. Refresh the page in order to see latest revision')
})
})
})
13 changes: 13 additions & 0 deletions static/src/js/utils/checkForCMRFetchDraftLag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Compares the revision Ids to ensure the draft retched is the most current
* @param {String} fetchedRevisionId The revision Id that comes back from CMR
* @param {String} expectedRevisionId The revision Id that was set on ingest (expected revision)
*/

const checkForCMRFetchDraftLag = (fetchedRevisionId, expectedRevisionId) => {
if ((fetchedRevisionId && expectedRevisionId) && (fetchedRevisionId !== expectedRevisionId)) {
throw new Error('Delay in CMR has detected. Refresh the page in order to see latest revision')
}
}

export default checkForCMRFetchDraftLag

0 comments on commit b1e6ef9

Please sign in to comment.