Skip to content

Commit

Permalink
Moved clear selection logic to viewer-app component
Browse files Browse the repository at this point in the history
  • Loading branch information
ctcuff committed Jan 5, 2021
1 parent 385bce2 commit 479f2c5
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import NavButton from 'src/scripts/viewer/components/inline-nav-button'

jest.mock('src/scripts/viewer/util/nav-util', () => ({
goNext: jest.fn(),
goPrev: jest.fn(),
clearSelection: jest.fn()
goPrev: jest.fn()
}))

const getProps = (type, disabled = false, onClick = null) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,62 @@ describe('ViewerApp', () => {
})
})

test('clearSelectionIfNavTargetChanging clears current selection', done => {
expect.assertions(2)
mocksForMount()

const mockRemoveAllRanges = jest.fn()
const mockEmpty = jest.fn()
const component = mount(<ViewerApp />)

// Need to make sure removeAllRanges gets called by default
// while empty gets called if removeAllRanges isn't available
const windowSpy = jest
.spyOn(window, 'getSelection')
.mockReturnValueOnce({
removeAllRanges: mockRemoveAllRanges,
empty: null
})
.mockReturnValueOnce({
removeAllRanges: null,
empty: mockEmpty
})
.mockReturnValueOnce({
removeAllRanges: null,
empty: null
})

setTimeout(() => {
component.update()

const isNavTargetChangingSpy = jest
.spyOn(component.instance(), 'isNavTargetChanging')
.mockReturnValueOnce(false)
.mockReturnValue(true)

// window.getSelection() shouldn't be called
component.instance().clearSelectionIfNavTargetChanging({})

// window.getSelection().removeAllRanges() should be called
component.instance().clearSelectionIfNavTargetChanging({})

// window.getSelection().empty() should be called
component.instance().clearSelectionIfNavTargetChanging({})

// window.getSelection() doesn't have removeAllRanges or empty,
// no function should be called
component.instance().clearSelectionIfNavTargetChanging({})

expect(mockRemoveAllRanges).toHaveBeenCalledTimes(1)
expect(mockEmpty).toHaveBeenCalledTimes(1)

isNavTargetChangingSpy.mockRestore()
windowSpy.mockRestore()

done()
})
})

test('onMouseDown calls clearFadeEffect', done => {
expect.assertions(2)
mocksForMount()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,33 +517,4 @@ describe('NavUtil', () => {
NavUtil.resetContext()
expect(Common.flux.Dispatcher.trigger).toHaveBeenCalledWith('nav:resetContext')
})

test('clearSelection clears the current selection', () => {
const mockRemoveAllRanges = jest.fn()
const mockEmpty = jest.fn()

// Need to make sure removeAllRanges gets called by default
// while empty gets called if removeAllRanges isn't available
window.getSelection = jest
.fn()
.mockReturnValueOnce({
removeAllRanges: mockRemoveAllRanges,
empty: null
})
.mockReturnValueOnce({
removeAllRanges: null,
empty: mockEmpty
})
.mockReturnValueOnce({
removeAllRanges: null,
empty: null
})

NavUtil.clearSelection()
NavUtil.clearSelection()
NavUtil.clearSelection()

expect(mockRemoveAllRanges).toHaveBeenCalledTimes(1)
expect(mockEmpty).toHaveBeenCalledTimes(1)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ export default class InlineNavButton extends React.Component {
}

onClick(event) {
// Make sure any highlighted text gets reset
// when navigating to different pages
NavUtil.clearSelection()

if (this.props.onClick) {
this.props.onClick()
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export default class ViewerApp extends React.Component {
this.unlockNavigation = this.unlockNavigation.bind(this)
this.clearPreviewScores = this.clearPreviewScores.bind(this)
this.onDelayResize = this.onDelayResize.bind(this)
this.clearSelectionIfNavTargetChanging = this.clearSelectionIfNavTargetChanging.bind(this)
}

componentDidMount() {
Expand Down Expand Up @@ -214,6 +215,20 @@ export default class ViewerApp extends React.Component {
}
}

clearSelectionIfNavTargetChanging(prevState) {
if (!this.isNavTargetChanging(prevState)) {
return
}

const selection = window.getSelection()

if (selection.removeAllRanges) {
selection.removeAllRanges()
} else if (selection.empty) {
selection.empty()
}
}

focusOnContentIfNavTargetChanging(prevState) {
const focussedItem = FocusUtil.getFocussedItem(this.state.focusState)

Expand Down Expand Up @@ -241,6 +256,7 @@ export default class ViewerApp extends React.Component {

this.focusOnContentIfNavTargetChanging(prevState)
this.scrollToTopIfNavTargetChanging(prevState)
this.clearSelectionIfNavTargetChanging(prevState)

// use Focus Store values to update DOM Focus
this.updateDOMFocus()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,6 @@ const NavUtil = {

isNavOpen(state) {
return state.open
},

clearSelection() {
const selection = window.getSelection()

if (selection.removeAllRanges) {
selection.removeAllRanges()
} else if (selection.empty) {
selection.empty()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ exports[`Footer renders 1`] = `
<span
id="copyright-date"
>
2020
2021
</span>
<a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ exports[`PageError renders when given props 1`] = `
<span
id="copyright-date"
>
2020
2021
</span>
<a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ exports[`PageHomepage renders when given props 1`] = `
<span
id="copyright-date"
>
2020
2021
</span>
<a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ exports[`PageLogin renders when given props 1`] = `
<span
id="copyright-date"
>
2020
2021
</span>
<a
Expand Down

0 comments on commit 479f2c5

Please sign in to comment.