Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1525984 - Add navigation keyboard shortcuts. (#1243)
- Loading branch information
Showing
2 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
frontend/src/modules/entitydetails/components/EntityNavigation.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import React from 'react'; | ||
import { mount, shallow } from 'enzyme'; | ||
import sinon from 'sinon'; | ||
|
||
import EntityNavigation from './EntityNavigation'; | ||
|
||
|
||
describe('<EntityNavigation>', () => { | ||
function getEntityNav({ create = shallow } = {}) { | ||
const nextMock = sinon.stub(); | ||
const prevMock = sinon.stub(); | ||
const wrapper = create(<EntityNavigation | ||
goToNextEntity={ nextMock } | ||
goToPreviousEntity={ prevMock } | ||
/>); | ||
|
||
return { | ||
wrapper, | ||
nextMock, | ||
prevMock, | ||
}; | ||
} | ||
|
||
it('goes to the next entity on click on the Next button', () => { | ||
const { wrapper, nextMock } = getEntityNav(); | ||
|
||
expect(nextMock.calledOnce).toBeFalsy(); | ||
wrapper.find('button.next').simulate('click'); | ||
expect(nextMock.calledOnce).toBeTruthy(); | ||
}); | ||
|
||
it('goes to the next entity on Alt + Down', () => { | ||
// Simulating the key presses on `document`. | ||
// See https://github.com/airbnb/enzyme/issues/426 | ||
const eventsMap = {}; | ||
document.addEventListener = sinon.spy((event, cb) => { | ||
eventsMap[event] = cb; | ||
}); | ||
|
||
const { nextMock } = getEntityNav(mount); | ||
|
||
expect(nextMock.calledOnce).toBeFalsy(); | ||
const event = { | ||
preventDefault: sinon.spy(), | ||
keyCode: 40, // Down | ||
altKey: true, | ||
ctrlKey: false, | ||
shiftKey: false, | ||
}; | ||
eventsMap.keydown(event); | ||
expect(nextMock.calledOnce).toBeTruthy(); | ||
}); | ||
|
||
it('goes to the previous entity on click on the Previous button', () => { | ||
const { wrapper, prevMock } = getEntityNav(); | ||
|
||
expect(prevMock.calledOnce).toBeFalsy(); | ||
wrapper.find('button.previous').simulate('click'); | ||
expect(prevMock.calledOnce).toBeTruthy(); | ||
}); | ||
|
||
it('goes to the previous entity on Alt + Up', () => { | ||
// Simulating the key presses on `document`. | ||
// See https://github.com/airbnb/enzyme/issues/426 | ||
const eventsMap = {}; | ||
document.addEventListener = sinon.spy((event, cb) => { | ||
eventsMap[event] = cb; | ||
}); | ||
|
||
const { prevMock } = getEntityNav(mount); | ||
|
||
expect(prevMock.calledOnce).toBeFalsy(); | ||
const event = { | ||
preventDefault: sinon.spy(), | ||
keyCode: 38, // Up | ||
altKey: true, | ||
ctrlKey: false, | ||
shiftKey: false, | ||
}; | ||
eventsMap.keydown(event); | ||
expect(prevMock.calledOnce).toBeTruthy(); | ||
}); | ||
}); |