This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 827
/
Copy pathapp.spec.js
69 lines (64 loc) · 1.92 KB
/
app.spec.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
import { init, registerListeners, setCurrentFromURL } from '../src/js/app';
import { actions as actionsMock } from '../src/js/store';
import * as renderMock from '../src/js/mini/render';
jest.mock('../src/js/mini/render', () => {
const mock = {};
function reset() {
Object.assign(mock, {
render: jest.fn(),
reset
});
}
reset();
return mock;
});
jest.mock('../src/js/store', () => ({
actions: {
next: jest.fn(),
prev: jest.fn(),
setCurrent: jest.fn()
},
connect: component => component
}));
afterEach(() => {
renderMock.reset();
});
const setup = () => {
const container = document.createElement('div');
container.className = 'root';
document.body.append(container);
return { container };
};
describe('Tests for app', () => {
describe('init', () => {
it('renders the app into the root element', () => {
const { container } = setup();
init();
expect(renderMock.render).toHaveBeenCalledTimes(1);
const firstCall = renderMock.render.mock.calls[0];
const [firstArg, secondArg] = firstCall;
expect(firstArg).toMatchSnapshot();
expect(secondArg).toEqual(container);
});
});
describe('registerListeners', () => {
it('register a listener for keyup events', () => {
const leftArrowEvent = new Event('keyup');
const rightArrowEvent = new Event('keyup');
leftArrowEvent.key = 'ArrowLeft';
rightArrowEvent.key = 'ArrowRight';
registerListeners();
document.dispatchEvent(leftArrowEvent);
document.dispatchEvent(rightArrowEvent);
expect(actionsMock.prev).toHaveBeenCalled();
expect(actionsMock.next).toHaveBeenCalled();
});
});
describe('setCurrentFromURL', () => {
it('sets the current rebus based on the url query string', () => {
window.history.pushState({}, 'Test', '/?rebus=2');
setCurrentFromURL();
expect(actionsMock.setCurrent).toHaveBeenCalledWith(2);
});
});
});