Skip to content

Commit

Permalink
Refactor CloseResourceModal to use state for clearing resource
Browse files Browse the repository at this point in the history
  • Loading branch information
jermnelson committed May 13, 2020
1 parent 3da51d8 commit 1aa0ea2
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,14 @@ describe('<CloseResourceModal />', () => {
})

it('clicking closes closes and invokes closeResource()', async () => {
const mockCloseResource = jest.fn()
const { getByText } = renderWithRedux(
<RDFModal closeResource={mockCloseResource}/>,
<RDFModal />,
store,
)
fireEvent.click(getByText('Close'))

await wait(() => {
expect(modalType(store.getState())).toBe(undefined)
})
expect(mockCloseResource).toBeCalled()
})
})
2 changes: 1 addition & 1 deletion __tests__/integration/multipleResources.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('Loading multiple resources', () => {
await wait(() => expect(queryByText('Abbreviated Title', { selector: 'button' })).not.toBeInTheDocument())

// Close again
fireEvent.click(queryAllByText('Close')[0])
fireEvent.click(queryAllByText('Close')[3])

// Back to Resource Template page
expect(await findByText('BIBFRAME Instance')).toBeInTheDocument()
Expand Down
2 changes: 1 addition & 1 deletion src/components/editor/actions/CloseButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const CloseButton = (props) => {

return (
<React.Fragment>
<CloseResourceModal closeResource={closeResource} />
<CloseResourceModal />
<button type="button"
className={buttonClasses}
aria-label="Close"
Expand Down
12 changes: 5 additions & 7 deletions src/components/editor/actions/CloseResourceModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@
import React from 'react'
import { hideModal } from 'actions/modals'
import { useDispatch, useSelector } from 'react-redux'
import PropTypes from 'prop-types'
import { clearResource } from 'actions/index'
import { currentResourceKey } from 'selectors/resourceSelectors'
import ModalWrapper, { useDisplayStyle, useModalCss } from '../../ModalWrapper'
import { modalType } from 'selectors/modalSelectors'

const CloseResourceModal = (props) => {
const CloseResourceModal = () => {
const dispatch = useDispatch()

const show = useSelector((state) => modalType(state) === 'CloseResourceModal')
const resourceKey = useSelector((state) => currentResourceKey(state))

const handleClose = (event) => {
dispatch(hideModal())
event.preventDefault()
}

const handleCloseResource = (event) => {
props.closeResource()
dispatch(clearResource(resourceKey))
dispatch(hideModal())
event.preventDefault()
}
Expand Down Expand Up @@ -55,8 +57,4 @@ const CloseResourceModal = (props) => {
return (<ModalWrapper modal={modal} />)
}

CloseResourceModal.propTypes = {
closeResource: PropTypes.func,
}

export default CloseResourceModal

0 comments on commit 1aa0ea2

Please sign in to comment.