File size: 9,071 Bytes
1e92f2d |
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
import React from 'react';
import renderer from 'react-test-renderer';
import Enzyme, { mount } from 'enzyme';
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
import Link from '../Link';
import Menu from '../Menu';
Enzyme.configure({ adapter: new Adapter() });
jest.mock('../../widgets/Highlight', () => () => null);
describe('Menu', () => {
it('default menu', () => {
const tree = renderer
.create(
<Menu
refine={() => null}
searchForItems={() => null}
createURL={() => '#'}
items={[
{ label: 'white', value: 'white', count: 10, isRefined: false },
{ label: 'black', value: 'black', count: 20, isRefined: false },
{ label: 'blue', value: 'blue', count: 30, isRefined: false },
{ label: 'green', value: 'green', count: 30, isRefined: false },
{ label: 'red', value: 'red', count: 30, isRefined: false },
]}
limit={2}
showMoreLimit={4}
showMore={true}
isFromSearch={false}
canRefine={true}
/>
)
.toJSON();
expect(tree).toMatchSnapshot();
});
it('default menu with custom className', () => {
const tree = renderer
.create(
<Menu
className="MyCustomMenu"
refine={() => null}
searchForItems={() => null}
createURL={() => '#'}
items={[
{ label: 'white', value: 'white', count: 10, isRefined: false },
{ label: 'black', value: 'black', count: 20, isRefined: false },
{ label: 'blue', value: 'blue', count: 30, isRefined: false },
{ label: 'green', value: 'green', count: 30, isRefined: false },
{ label: 'red', value: 'red', count: 30, isRefined: false },
]}
limit={2}
showMoreLimit={4}
showMore={true}
isFromSearch={false}
canRefine={true}
/>
)
.toJSON();
expect(tree).toMatchSnapshot();
});
it('Menu with search inside items but no search results', () => {
const tree = renderer
.create(
<Menu
refine={() => null}
searchForItems={() => null}
searchable
createURL={() => '#'}
items={[
{ label: 'white', value: 'white', count: 10, isRefined: true },
{ label: 'black', value: 'black', count: 20, isRefined: false },
{ label: 'blue', value: 'blue', count: 30, isRefined: false },
]}
isFromSearch={false}
canRefine={true}
/>
)
.toJSON();
expect(tree).toMatchSnapshot();
});
it('Menu with search inside items with search results', () => {
const tree = renderer
.create(
<Menu
refine={() => null}
searchForItems={() => null}
searchable
createURL={() => '#'}
items={[
{
label: 'white',
value: 'white',
count: 10,
isRefined: true,
_highlightResult: { label: 'white' },
},
]}
isFromSearch={true}
canRefine={true}
/>
)
.toJSON();
expect(tree).toMatchSnapshot();
});
it('applies translations', () => {
const tree = renderer
.create(
<Menu
refine={() => null}
createURL={() => '#'}
searchForItems={() => null}
searchable
items={[
{ label: 'white', value: 'white', count: 10, isRefined: false },
{ label: 'black', value: 'black', count: 20, isRefined: false },
{ label: 'blue', value: 'blue', count: 30, isRefined: false },
{ label: 'green', value: 'green', count: 30, isRefined: false },
{ label: 'red', value: 'red', count: 30, isRefined: false },
]}
limit={2}
showMoreLimit={4}
showMore={true}
translations={{
showMore: ' display more',
placeholder: 'placeholder',
}}
isFromSearch={false}
canRefine={true}
/>
)
.toJSON();
expect(tree).toMatchSnapshot();
});
it('refines its value on change', () => {
const refine = jest.fn();
const wrapper = mount(
<Menu
refine={refine}
searchForItems={() => null}
createURL={() => '#'}
items={[
{ label: 'white', value: 'white', count: 10, isRefined: false },
{ label: 'black', value: 'black', count: 20, isRefined: false },
{ label: 'blue', value: 'blue', count: 30, isRefined: false },
]}
isFromSearch={false}
canRefine={true}
/>
);
const items = wrapper.find('li');
expect(items).toHaveLength(3);
const firstItem = items.first().find(Link);
firstItem.simulate('click');
expect(refine.mock.calls).toHaveLength(1);
expect(refine.mock.calls[0][0]).toEqual('white');
wrapper.unmount();
});
it('should respect defined limits', () => {
const refine = jest.fn();
const wrapper = mount(
<Menu
refine={refine}
createURL={() => '#'}
items={[
{ label: 'white', value: 'white', count: 10, isRefined: false },
{ label: 'black', value: 'black', count: 20, isRefined: false },
{ label: 'blue', value: 'blue', count: 30, isRefined: false },
{ label: 'green', value: 'green', count: 30, isRefined: false },
{ label: 'red', value: 'red', count: 30, isRefined: false },
]}
limit={2}
showMoreLimit={4}
showMore={true}
isFromSearch={false}
searchForItems={() => null}
canRefine={true}
/>
);
const items = wrapper.find('li');
expect(items).toHaveLength(2);
wrapper.find('button').simulate('click');
expect(wrapper.find('li')).toHaveLength(4);
wrapper.unmount();
});
it('should disabled show more when no more item to display', () => {
const refine = jest.fn();
const wrapper = mount(
<Menu
refine={refine}
createURL={() => '#'}
items={[
{ label: 'white', value: 'white', count: 10, isRefined: false },
{ label: 'black', value: 'black', count: 20, isRefined: false },
]}
limit={2}
showMoreLimit={4}
showMore={true}
isFromSearch={false}
searchForItems={() => null}
canRefine={true}
/>
);
const items = wrapper.find('li');
expect(items).toHaveLength(2);
expect(wrapper.find('button[disabled]')).toBeDefined();
wrapper.unmount();
});
describe('search for facets value', () => {
const refine = jest.fn();
const searchForItems = jest.fn();
const menu = (
<Menu
refine={refine}
searchable
searchForItems={searchForItems}
createURL={() => '#'}
items={[
{
label: 'white',
value: 'white',
count: 10,
isRefined: false,
_highlightResult: { label: { value: 'white' } },
},
{
label: 'black',
value: 'black',
count: 20,
isRefined: false,
_highlightResult: { label: { value: 'black' } },
},
]}
isFromSearch={true}
canRefine={true}
/>
);
it('a searchbox should be displayed if the feature is activated', () => {
const wrapper = mount(menu);
const searchBox = wrapper.find('.searchBox');
expect(searchBox).toBeDefined();
wrapper.unmount();
});
it('searching for a value should call searchForItems', () => {
const wrapper = mount(menu);
wrapper.find('input').simulate('change', { target: { value: 'query' } });
expect(searchForItems.mock.calls).toHaveLength(1);
expect(searchForItems.mock.calls[0][0]).toBe('query');
wrapper.unmount();
});
it('should refine the selected value and display selected refinement back', () => {
const wrapper = mount(menu);
const firstItem = wrapper.find('li').first().find(Link);
firstItem.simulate('click');
expect(refine.mock.calls).toHaveLength(1);
expect(refine.mock.calls[0][0]).toEqual('white');
expect(wrapper.find('input').props().value).toBe('');
const selectedRefinements = wrapper.find('li');
expect(selectedRefinements).toHaveLength(2);
wrapper.unmount();
});
it('hit enter on the search values results list should refine the first facet value', () => {
refine.mockClear();
const wrapper = mount(menu);
wrapper.find('form').simulate('submit');
expect(refine.mock.calls).toHaveLength(1);
expect(refine.mock.calls[0][0]).toEqual('white');
expect(wrapper.find('input').props().value).toBe('');
const selectedRefinements = wrapper.find('li');
expect(selectedRefinements).toHaveLength(2);
wrapper.unmount();
});
});
});
|