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

EDSC-2895: Fix download granule dropdown in table view #1241

Merged
merged 6 commits into from
Oct 9, 2020
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import { Dropdown } from 'react-bootstrap'
import { PropTypes } from 'prop-types'

Expand All @@ -12,14 +13,15 @@ import './GranuleResultsDataLinksButton.scss'
* @param {Object} props - The props passed into the component.
* @param {Function} props.onClick - The click callback.null
*/
class CustomDataLinksToggle extends Component {
export class CustomDataLinksToggle extends Component {
constructor(props, context) {
super(props, context)
this.handleClick = this.handleClick.bind(this)
}

handleClick(e) {
e.preventDefault()
e.stopPropagation()
const { onClick } = this.props
onClick(e)
}
Expand Down Expand Up @@ -60,31 +62,36 @@ export const GranuleResultsDataLinksButton = ({
return (
<Dropdown>
<Dropdown.Toggle as={CustomDataLinksToggle} />
<Dropdown.Menu>
{
dataLinks.map((dataLink, i) => {
const key = `data_link_${i}`
let dataLinkTitle = dataLink.title
{
ReactDOM.createPortal(
<Dropdown.Menu>
{
dataLinks.map((dataLink, i) => {
const key = `data_link_${i}`
let dataLinkTitle = dataLink.title

if (!dataLinkTitle) dataLinkTitle = getFilenameFromPath(dataLink.href)
if (!dataLinkTitle) dataLinkTitle = getFilenameFromPath(dataLink.href)

return (
<Dropdown.Item
key={key}
href={dataLink.href}
onClick={() => onMetricsDataAccess({
type: 'single_granule_download',
collections: [{
collectionId
}]
})}
>
{dataLinkTitle}
</Dropdown.Item>
)
})
}
</Dropdown.Menu>
return (
<Dropdown.Item
key={key}
href={dataLink.href}
onClick={() => onMetricsDataAccess({
type: 'single_granule_download',
collections: [{
collectionId
}]
})}
>
{dataLinkTitle}
</Dropdown.Item>
)
})
}
</Dropdown.Menu>,
document.querySelector('#root')
)
}
</Dropdown>
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react'
import ReactDOM from 'react-dom'
import Enzyme, { shallow } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import { Dropdown } from 'react-bootstrap'

import { GranuleResultsDataLinksButton } from '../GranuleResultsDataLinksButton'
import { GranuleResultsDataLinksButton, CustomDataLinksToggle } from '../GranuleResultsDataLinksButton'
import Button from '../../Button/Button'

Enzyme.configure({ adapter: new Adapter() })
Expand Down Expand Up @@ -76,6 +77,9 @@ describe('GranuleResultsDataLinksButton component', () => {

describe('with multiple granule links', () => {
test('renders the correct element', () => {
// Mocks createPortal method of ReactDOM (https://stackoverflow.com/a/60953708/8116576)
ReactDOM.createPortal = jest.fn(dropdown => dropdown)

const { enzymeWrapper } = setup({
dataLinks: [
{
Expand All @@ -93,3 +97,21 @@ describe('GranuleResultsDataLinksButton component', () => {
})
})
})

describe('CustomDataLinksToggle component', () => {
test('calls expected event methods on download click', () => {
const mockClickEvent = {
stopPropagation: jest.fn(),
preventDefault: jest.fn()
}

const mockClickCallback = jest.fn()

shallow(<CustomDataLinksToggle onClick={mockClickCallback} />)
.simulate('click', mockClickEvent)

expect(mockClickEvent.stopPropagation).toHaveBeenCalled()
expect(mockClickEvent.preventDefault).toHaveBeenCalled()
expect(mockClickCallback).toHaveBeenCalled()
})
})