/
EntityNavigation.test.js
83 lines (69 loc) · 2.49 KB
/
EntityNavigation.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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();
});
});