-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathLoopIterationsInput.test.js
More file actions
120 lines (96 loc) · 4.97 KB
/
LoopIterationsInput.test.js
File metadata and controls
120 lines (96 loc) · 4.97 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// @flow
import React from 'react';
import Adapter from 'enzyme-adapter-react-16';
import { configure, mount } from 'enzyme';
import LoopIterationsInput from './LoopIterationsInput';
configure({ adapter: new Adapter() });
const defaultLoopIterationsInputProps = {
loopIterationsAriaLabel: 'Number of iterations for loop A',
loopIterationsStr: '2',
loopLabel: 'A',
stepNumber: 2,
runningState: 'stopped',
keyboardInputSchemeName: 'controlalt'
};
function createMountLoopIterationsInput(props) {
const mockOnChangeLoopIterations = jest.fn();
const wrapper = mount(
React.createElement(
LoopIterationsInput,
Object.assign(
{},
defaultLoopIterationsInputProps,
{ onChangeLoopIterations: mockOnChangeLoopIterations },
props
)
)
);
return { wrapper, mockOnChangeLoopIterations };
}
function getLoopIterationsInput(wrapper) {
return wrapper.find('.command-block-loop-iterations');
}
test('Number of iterations should be rendered', () => {
expect.assertions(1);
const {wrapper} = createMountLoopIterationsInput();
const loopIterationsInput = getLoopIterationsInput(wrapper);
expect(((loopIterationsInput.getDOMNode(): any): HTMLInputElement).value).toBe('2');
});
test('Blur should call the registered callback', () => {
expect.assertions(4);
const {wrapper, mockOnChangeLoopIterations} = createMountLoopIterationsInput();
const loopIterationsInput = getLoopIterationsInput(wrapper);
expect(((loopIterationsInput.getDOMNode(): any): HTMLInputElement).value).toBe('2');
// Change the value and check that the callback is called on blur
((loopIterationsInput.getDOMNode(): any): HTMLInputElement).value = '3';
loopIterationsInput.simulate('change');
expect(wrapper.instance().state.loopIterationsStr).toBe('3');
loopIterationsInput.simulate('blur');
expect(mockOnChangeLoopIterations.mock.calls.length).toBe(1);
expect(mockOnChangeLoopIterations.mock.calls[0][2]).toBe(3);
});
test('Pressing Enter should call the registered callback', () => {
expect.assertions(4);
const {wrapper, mockOnChangeLoopIterations} = createMountLoopIterationsInput();
const loopIterationsInput = getLoopIterationsInput(wrapper);
expect(((loopIterationsInput.getDOMNode(): any): HTMLInputElement).value).toBe('2');
// Change the value and check that the callback is called on press Enter
((loopIterationsInput.getDOMNode(): any): HTMLInputElement).value = '3';
loopIterationsInput.simulate('change');
expect(wrapper.instance().state.loopIterationsStr).toBe('3');
loopIterationsInput.getDOMNode().dispatchEvent(new KeyboardEvent('keydown', {key: 'Enter'}));
expect(mockOnChangeLoopIterations.mock.calls.length).toBe(1);
expect(mockOnChangeLoopIterations.mock.calls[0][2]).toBe(3);
});
test.each(([
new KeyboardEvent('keydown', {key: 'a', ctrlKey: true, altKey: true}), // addCommand
new KeyboardEvent('keydown', {key: 'b', ctrlKey: true, altKey: true}), // addCommandToBeginning
new KeyboardEvent('keydown', {key: ']', ctrlKey: true, altKey: true}), // moveToNextStep
new KeyboardEvent('keydown', {key: '[', ctrlKey: true, altKey: true}), // moveToPreviousStep
new KeyboardEvent('keydown', {key: 'p', ctrlKey: true, altKey: true}) // playPauseProgram
]))('Shortcuts that cause an update should call the registered callback', (keyboardEvent) => {
expect.assertions(4);
const {wrapper, mockOnChangeLoopIterations} = createMountLoopIterationsInput();
const loopIterationsInput = getLoopIterationsInput(wrapper);
expect(((loopIterationsInput.getDOMNode(): any): HTMLInputElement).value).toBe('2');
// Change the value, send the keyboard event, and check that the callback is called
((loopIterationsInput.getDOMNode(): any): HTMLInputElement).value = '3';
loopIterationsInput.simulate('change');
expect(wrapper.instance().state.loopIterationsStr).toBe('3');
loopIterationsInput.getDOMNode().dispatchEvent(keyboardEvent);
expect(mockOnChangeLoopIterations.mock.calls.length).toBe(1);
expect(mockOnChangeLoopIterations.mock.calls[0][2]).toBe(3);
});
test('Other shortcuts should not cause the registered callback to be called', () => {
expect.assertions(3);
const {wrapper, mockOnChangeLoopIterations} = createMountLoopIterationsInput();
const loopIterationsInput = getLoopIterationsInput(wrapper);
expect(((loopIterationsInput.getDOMNode(): any): HTMLInputElement).value).toBe('2');
// Change the value, send the keyboard event, and check that the callback is not called
((loopIterationsInput.getDOMNode(): any): HTMLInputElement).value = '3';
loopIterationsInput.simulate('change');
expect(wrapper.instance().state.loopIterationsStr).toBe('3');
loopIterationsInput.getDOMNode().dispatchEvent(new KeyboardEvent('keydown',
{key: 'x', ctrlKey: true, altKey: true}));
expect(mockOnChangeLoopIterations.mock.calls.length).toBe(0);
});