Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MMT-3906: Error deleting Drafts in MMT #1311

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
59 changes: 36 additions & 23 deletions static/src/js/components/DraftPreview/DraftPreview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,37 +58,50 @@ const DraftPreview = () => {
// Validate ummMetadata
const { errors: validationErrors } = validator.validateFormData(ummMetadata, schema)

const { errors } = ummMetadata

// Pull the formSections out of the formConfigurations
const formSections = formConfigurations[derivedConceptType]

return (
<Container id="metadata-form" fluid className="px-0">
<Row>
<Col md={12}>
{
errors ? (
<Row>
<Col className="mb-5">
<h3 className="sr-only">Metadata Fields</h3>
<PreviewProgress
draftJson={ummMetadata}
schema={schema}
sections={formSections}
validationErrors={validationErrors}
/>
<Col md={12}>
This record does not exist in CMR, please contact [email protected]
if you believe this is an error.
</Col>
</Row>
</Col>
<Row>
<Col md={12} className="draft-preview__preview">
<h2 className="fw-bold fs-4">Preview</h2>
<ErrorBoundary>
<MetadataPreview
conceptId={conceptId}
conceptType={derivedConceptType}
/>
</ErrorBoundary>
</Col>
</Row>
</Row>
) : (
<Row>
<Col md={12}>
<Row>
<Col className="mb-5">
<h3 className="sr-only">Metadata Fields</h3>
<PreviewProgress
draftJson={ummMetadata}
schema={schema}
sections={formSections}
validationErrors={validationErrors}
/>
</Col>
</Row>
</Col>
<Row>
<Col md={12} className="draft-preview__preview">
<h2 className="fw-bold fs-4">Preview</h2>
<ErrorBoundary>
<MetadataPreview
conceptId={conceptId}
conceptType={derivedConceptType}
/>
</ErrorBoundary>
</Col>
</Row>
</Row>
)
}
</Container>
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import React, { Suspense } from 'react'
import { render, waitFor } from '@testing-library/react'
import {
render,
screen,
waitFor
} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { MockedProvider } from '@apollo/client/testing'
import {
Expand Down Expand Up @@ -92,6 +96,59 @@ const mockDraft = {
__typename: 'Draft'
}

const mockErrorDraft = {
conceptId: 'TD1000000-MMT',
conceptType: 'tool-draft',
deleted: false,
name: null,
nativeId: 'MMT_2331e312-cbbc-4e56-9d6f-fe217464be2c',
providerId: 'MMT_2',
revisionDate: '2023-12-08T16:14:28.177Z',
revisionId: '2',
ummMetadata: {
errors: 'concept not found in DB'
},
previewMetadata: {
accessConstraints: null,
ancillaryKeywords: null,
associationDetails: null,
conceptId: 'TD1000000-MMT',
contactGroups: null,
contactPersons: null,
description: null,
doi: null,
nativeId: 'MMT_2331e312-cbbc-4e56-9d6f-fe217464be2c',
lastUpdatedDate: null,
longName: 'Long Name',
metadataSpecification: {
url: 'https://cdn.earthdata.nasa.gov/umm/tool/v1.1',
name: 'UMM-T',
version: '1.1'
},
name: null,
organizations: null,
pageTitle: null,
potentialAction: null,
quality: null,
relatedUrls: null,
revisionId: '2',
searchAction: null,
supportedBrowsers: null,
supportedInputFormats: null,
supportedOperatingSystems: null,
supportedOutputFormats: null,
supportedSoftwareLanguages: null,
toolKeywords: null,
type: null,
url: null,
useConstraints: null,
version: null,
versionDescription: null,
__typename: 'Tool'
},
__typename: 'Draft'
}

const setup = ({
additionalMocks = [],
overrideMocks = false,
Expand Down Expand Up @@ -244,4 +301,29 @@ describe('DraftPreview', () => {
}, {})
})
})

describe('when cmr takes longer than react to populate', () => {
test('renders a refresh page banner', async () => {
setup({
overrideMocks: [{
request: {
query: conceptTypeDraftQueries.Tool,
variables: {
params: {
conceptId: 'TD1000000-MMT',
conceptType: 'Tool'
}
}
},
result: {
data: {
draft: mockErrorDraft
}
}
}]
})

expect(await screen.findByText('This record does not exist in CMR, please contact [email protected] if you believe this is an error.')).toBeVisible()
})
})
})
5 changes: 5 additions & 0 deletions static/src/js/pages/DraftPage/DraftPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ const DraftPageHeader = () => {
// Validate ummMetadata
const { errors: validationErrors } = validator.validateFormData(ummMetadata, schema)

// Check to see if special CMR lag use case is true
const { errors } = ummMetadata

const handlePublish = () => {
publishMutation(derivedConceptType, nativeId)
}
Expand Down Expand Up @@ -219,6 +222,7 @@ const DraftPageHeader = () => {
variant: 'success'
},
{
disabled: !!errors,
icon: FaTrash,
iconTitle: 'A trash icon',
onClick: () => toggleShowDeleteModal(true),
Expand All @@ -228,6 +232,7 @@ const DraftPageHeader = () => {
...[(
derivedConceptType === conceptTypes.Collection
? {
disabled: !!errors,
icon: FaCopy,
iconTitle: 'A copy icon',
onClick: handleTemplate,
Expand Down
104 changes: 100 additions & 4 deletions static/src/js/pages/DraftPage/__tests__/DraftPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const ummMetadata = {
const mockDraft = {
__typename: 'Draft',
conceptId: 'TD1000000-MMT',
conceptType: 'topol-draft',
conceptType: 'tool-draft',
deleted: false,
name: 'mock name',
nativeId: 'MMT_2331e312-cbbc-4e56-9d6f-fe217464be2c',
Expand Down Expand Up @@ -111,11 +111,64 @@ const mockDraft = {
ummMetadata
}

const mockCollectionDraft = {
__typename: 'Draft',
conceptId: 'CD1000000-MMT',
conceptType: 'collection-draft',
deleted: false,
name: 'mock name',
nativeId: 'MMT_2331e312-cbbc-4e56-9d6f-fe217464be2c',
previewMetadata: {
__typename: 'Collection',
accessConstraints: null,
ancillaryKeywords: null,
associationDetails: null,
conceptId: 'CD1000000-MMT',
contactGroups: null,
contactPersons: null,
description: null,
doi: null,
lastUpdatedDate: null,
longName: 'Long Name',
metadataSpecification: {
name: 'UMM-C',
url: 'https://cdn.earthdata.nasa.gov/umm/collection/v1.1',
version: '1.1'
},
name: null,
nativeId: 'MMT_2331e312-cbbc-4e56-9d6f-fe217464be2c',
organizations: null,
pageTitle: null,
potentialAction: null,
quality: null,
relatedUrls: null,
revisionId: '2',
searchAction: null,
supportedBrowsers: null,
supportedInputFormats: null,
supportedOperatingSystems: null,
supportedOutputFormats: null,
supportedSoftwareLanguages: null,
toolKeywords: null,
type: null,
url: null,
useConstraints: null,
version: null,
versionDescription: null
},
providerId: 'MMT_2',
revisionDate: '2023-12-08T16:14:28.177Z',
revisionId: '2',
ummMetadata
}

const setup = ({
additionalMocks = [],
overrideMocks = false,
pageUrl = '/drafts/tools/TD1000000-MMT',
path = '/drafts/tools'
overridePageUrl = false,
path = '/drafts/tools',
overridePath = false
}) => {
const mocks = [{
request: {
Expand All @@ -141,10 +194,10 @@ const setup = ({
<MockedProvider
mocks={overrideMocks || mocks}
>
<MemoryRouter initialEntries={[pageUrl]}>
<MemoryRouter initialEntries={[overridePageUrl || pageUrl]}>
<Routes>
<Route
path={path}
path={overridePath || path}
>
<Route
path=":conceptId"
Expand Down Expand Up @@ -341,6 +394,49 @@ describe('DraftPage', () => {
expect(button).toHaveAttribute('disabled')
})

test('buttons are disabled if draft does not exist', async () => {
const navigateSpy = vi.fn()
vi.spyOn(router, 'useNavigate').mockImplementation(() => navigateSpy)

// Overriding ummMetadata and setting it to have errors
const invalidMockDraft = {
...mockCollectionDraft,
ummMetadata: {
errors: 'concept not found in DB'
}
}

setup({
overrideMocks: [{
request: {
query: conceptTypeDraftQueries.Collection,
variables: {
params: {
conceptId: 'CD1000000-MMT',
conceptType: 'Collection'
}
}
},
result: {
data: {
draft: invalidMockDraft
}
}
}],
overridePageUrl: '/drafts/collections/CD1000000-MMT',
overridePath: '/drafts/collections'
})

const publishButton = await screen.findByRole('button', { name: /Publish/ })
expect(publishButton).toHaveAttribute('disabled')

const deleteButton = await screen.findByRole('button', { name: /Delete/ })
expect(deleteButton).toHaveAttribute('disabled')

const templateButton = await screen.findByRole('button', { name: /Save as Template/ })
expect(templateButton).toHaveAttribute('disabled')
})

test('calls the publish mutation and if unsuccesfull shows error', async () => {
const navigateSpy = vi.fn()
vi.spyOn(router, 'useNavigate').mockImplementation(() => navigateSpy)
Expand Down