forked from codelearncreate/c2lc-coding-environment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandPaletteCommand.test.js
More file actions
91 lines (79 loc) · 2.92 KB
/
CommandPaletteCommand.test.js
File metadata and controls
91 lines (79 loc) · 2.92 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
// @flow
import React from 'react';
import Adapter from 'enzyme-adapter-react-16';
import { shallow, configure } from 'enzyme';
import CommandBlock from './CommandBlock';
import { createIntl } from 'react-intl';
import CommandPaletteCommand from './CommandPaletteCommand';
configure({ adapter: new Adapter()});
function hasPressedClass(wrapper) {
return wrapper.find(CommandBlock).hasClass('command-block--pressed');
}
function getAriaPressedValue(wrapper) {
// $FlowFixMe: The flow-typed definitions for enzyme introduce a type-checking error here.
return wrapper.find(CommandBlock).getElement().props['aria-pressed'];
}
const intl = createIntl({
locale: 'en',
defaultLocale: 'en',
messages: {
'Command.forward1' : 'forward1'
}
});
test('Pressed state is false when selecedCommandName is null', () => {
const wrapper = shallow(
<CommandPaletteCommand.WrappedComponent
intl={intl}
commandName='forward1'
selectedActionName={null}
onSelect={() => {}}/>
);
expect(hasPressedClass(wrapper)).toBe(false);
expect(getAriaPressedValue(wrapper)).toBe(false);
});
test('Pressed state is false when selecedCommandName is another command', () => {
const wrapper = shallow(
<CommandPaletteCommand.WrappedComponent
intl={intl}
commandName='forward1'
selectedActionName='left45'
onSelect={() => {}}/>
);
expect(hasPressedClass(wrapper)).toBe(false);
expect(getAriaPressedValue(wrapper)).toBe(false);
});
test('Pressed state is true when selecedCommandName is this command', () => {
const wrapper = shallow(
<CommandPaletteCommand.WrappedComponent
intl={intl}
commandName='forward1'
selectedActionName='forward1'
onSelect={() => {}}/>
);
expect(hasPressedClass(wrapper)).toBe(true);
expect(getAriaPressedValue(wrapper)).toBe(true);
});
test('Clicking the button calls the callback onSelect with commandName', () => {
const mockSelectHandler = jest.fn();
const wrapper = shallow(
<CommandPaletteCommand.WrappedComponent
intl={intl}
commandName='forward1'
selectedActionName={null}
onSelect={mockSelectHandler}/>
);
const button = wrapper.find(CommandBlock);
// Initially the command is not selected
button.simulate('click');
// Verify that onSelect is called with the commandName
expect(mockSelectHandler.mock.calls.length).toBe(1);
expect(mockSelectHandler.mock.calls[0][0]).toBe('forward1');
// Update the selectedActionName
wrapper.setProps({selectedActionName: 'forward1'});
wrapper.update();
// Click again
button.simulate('click');
// And verify that onSelect is called again with the commandName
expect(mockSelectHandler.mock.calls.length).toBe(2);
expect(mockSelectHandler.mock.calls[1][0]).toBe('forward1');
});