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-3553: As a user, I want to clone a publish record #1115

Merged
merged 13 commits into from
Feb 6, 2024
26 changes: 26 additions & 0 deletions static/src/js/components/PublishPreview/PublishPreview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import MetadataPreview from '../MetadataPreview/MetadataPreview'
import Page from '../Page/Page'
import useAppContext from '../../hooks/useAppContext'
import useIngestDraftMutation from '../../hooks/useIngestDraftMutation'
import removeMetadataKeys from '../../utils/removeMetadataKeys'

/**
* Renders a PublishPreview component
Expand Down Expand Up @@ -112,6 +113,15 @@ const PublishPreview = () => {
ingestMutation(derivedConceptType, ummMetadata, nativeId, providerId)
}

// Calls ingestDraft mutation with a new nativeId
const handleClone = () => {
const cloneNativeId = `MMT_${crypto.randomUUID()}`
// Removes the value from the metadata that has to be unique
removeMetadataKeys(ummMetadata, ['Name', 'LongName', 'ShortName'])
dmistry1 marked this conversation as resolved.
Show resolved Hide resolved

ingestMutation(derivedConceptType, ummMetadata, cloneNativeId, providerId)
}

// Handles the user selecting delete from the delete model
const handleDelete = () => {
deleteMutation({
Expand Down Expand Up @@ -186,6 +196,22 @@ const PublishPreview = () => {
{' '}
Record
</Button>
<Button
className="btn btn-link"
type="button"
variant="link"
onClick={
() => {
handleClone()
}
}
>
Clone
{' '}
{derivedConceptType}
{' '}
Record
</Button>
<Button
type="button"
variant="outline-danger"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ jest.mock('react-router-dom', () => ({
useNavigate: () => mockedUsedNavigate
}))

Object.defineProperty(globalThis, 'crypto', {
value: {
randomUUID: () => 'mock-uuid'
}
})

const mock = {
accessConstraints: null,
ancillaryKeywords: null,
Expand Down Expand Up @@ -443,4 +449,44 @@ describe('PublishPreview', () => {
expect(errorLogger).toHaveBeenCalledWith(new Error('An error occurred'), 'PublishPreview ingestDraftMutation Query')
})
})

describe('when clicking on Clone Tool Record button', () => {
test('calls ingestDraft Mutation and navigates to /drafts/tool/conceptId page', async () => {
const navigateSpy = jest.fn()
jest.spyOn(router, 'useNavigate').mockImplementation(() => navigateSpy)

const { user } = setup({
additionalMocks: [{
request: {
query: INGEST_DRAFT,
variables: {
conceptType: 'Tool',
metadata: {},
nativeId: 'MMT_mock-uuid',
providerId: 'MMT_2',
ummVersion: '1.2.0'
}
},
result: {
data: {
ingestDraft: {
conceptId: 'TD1000000-MMT',
revisionId: '3'
}
}
}
}]
})

await waitForResponse()

const editButton = screen.getByRole('button', { name: 'Clone Tool Record' })
await user.click(editButton)

await waitForResponse()

expect(navigateSpy).toHaveBeenCalledTimes(1)
expect(navigateSpy).toHaveBeenCalledWith('/drafts/tools/TD1000000-MMT')
})
})
})
16 changes: 16 additions & 0 deletions static/src/js/utils/__tests__/removeMetadataKeys.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import removeMetadataKeys from '../removeMetadataKeys'

describe('removeMetadataKeys', () => {
test('should remove Name and LongName from the metadata', () => {
const metadata = {
Name: 'test name',
LongName: 'test long name',
Description: 'test description'
}
const keys = ['Name', 'LongName']

const removeKeys = removeMetadataKeys(metadata, keys)

expect(removeKeys).toMatchObject({ Description: 'test description' })
})
})
19 changes: 19 additions & 0 deletions static/src/js/utils/removeMetadataKeys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Given a ummMetadata object, removes all the keys that are given in the
* keys array.
* @param {Object} metadata An object containing the keys and value
* @param {Array} keys An array that has all the keys that needs to be removed
*/
const removeMetadataKeys = (metadata, keys) => {
const modifiedMetadata = metadata

Object.keys(metadata).forEach((key) => {
if (keys.includes(key)) {
delete modifiedMetadata[key]
}
})

return modifiedMetadata
}

export default removeMetadataKeys
Loading