-
Notifications
You must be signed in to change notification settings - Fork 181
/
select__popup.test.tsx
159 lines (118 loc) · 4.22 KB
/
select__popup.test.tsx
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import React from 'react';
import {Simulate} from 'react-dom/test-utils';
import {shallow, mount} from 'enzyme';
import getUID from '../global/get-uid';
import List from '../list/list';
import simulateCombo from '../../test-helpers/simulate-combo';
import {ListDataItem} from '../list/consts';
import SelectPopup, {SelectPopupAttrs} from './select__popup';
describe('SelectPopup', () => {
const factory = (props?: SelectPopupAttrs) => (
<SelectPopup
filter
onSelect={sandbox.spy()}
onFilter={sandbox.spy()}
onCloseAttempt={sandbox.spy()}
data={[]}
{...props}
/>
);
const shallowSelectPopup = (props?: SelectPopupAttrs) => shallow<SelectPopup>(factory(props));
const mountSelectPopup = (props?: SelectPopupAttrs) => mount<SelectPopup>(factory(props));
describe('hidden', () => {
describe('filter', () => {
it('should disable shortcuts', () => {
const wrapper = shallowSelectPopup();
const instance = wrapper.instance();
sandbox.spy(instance, 'tabPress');
simulateCombo('tab');
instance.tabPress.should.not.be.called;
});
});
});
describe('visible', () => {
function createListItemMock(): ListDataItem {
const key = getUID('popup-test-');
const label = getUID('popup-label-');
return {
key,
label,
rgItemType: List.ListProps.Type.ITEM
};
}
let testData: ListDataItem[];
beforeEach(() => {
testData = [
createListItemMock(),
createListItemMock()
];
});
it('should initialize', () => {
shallowSelectPopup().should.exist;
});
it('should call close handler when user press tab', () => {
const wrapper = mountSelectPopup({data: testData});
wrapper.setProps({hidden: false});
wrapper.instance().list!.state.activeItem = {};
simulateCombo('tab');
wrapper.prop('onCloseAttempt').should.be.called;
});
describe('popup without data', () => {
it('should not throw error when user press tab but we do not have the list', () => {
const wrapper = mountSelectPopup();
wrapper.setProps({hidden: false});
(() => {
simulateCombo('tab');
}).should.not.throw();
});
});
describe('navigation', () => {
it('should highlight first item', () => {
const wrapper = mountSelectPopup({data: testData});
wrapper.setProps({hidden: false});
const firstItem = testData[0];
wrapper.instance().list!.getSelected()!.should.be.equal(firstItem);
});
it('should highlight last item', () => {
const wrapper = mountSelectPopup({data: testData});
wrapper.setProps({hidden: false});
const lastItem = testData[testData.length - 1];
simulateCombo('up');
wrapper.instance().list!.getSelected()!.should.be.equal(lastItem);
});
it('should select item', () => {
const wrapper = mountSelectPopup({data: testData});
wrapper.setProps({hidden: false});
const firstItem = testData[0];
simulateCombo('enter');
wrapper.prop('onSelect').should.be.calledWith(firstItem);
});
});
describe('filter', () => {
function expectPopupFilterShortcutsDisabled(fn: (...args: never[]) => void, value: boolean) {
fn.should.be.calledWith({
popupFilterShortcutsOptions: {
modal: true,
disabled: value
}
});
}
it('should enable shortcuts on focus', () => {
const wrapper = mountSelectPopup({data: testData});
wrapper.setProps({hidden: false});
const instance = wrapper.instance();
sandbox.spy(instance, 'setState');
Simulate.focus(instance.filter as HTMLInputElement);
expectPopupFilterShortcutsDisabled(instance.setState, false);
});
it('should disable shortcuts on blur', () => {
const wrapper = mountSelectPopup({data: testData});
wrapper.setProps({hidden: false});
const instance = wrapper.instance();
sandbox.spy(instance, 'setState');
Simulate.blur(instance.filter as HTMLInputElement);
expectPopupFilterShortcutsDisabled(instance.setState, true);
});
});
});
});