Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
fix(List): render children list only when required (#1472)
Browse files Browse the repository at this point in the history
* fix(List): remove class parent when items is empty

* fix(List): empty array parent--item (#1459)

* fix(List): rename variable isItemHasChildren to itemHasChildren
  • Loading branch information
samouss authored Aug 7, 2018
1 parent c5f189b commit 9eb2cbb
Show file tree
Hide file tree
Showing 3 changed files with 362 additions and 9 deletions.
18 changes: 9 additions & 9 deletions packages/react-instantsearch-dom/src/components/List.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,7 @@ class List extends Component {
};

renderItem = (item, resetQuery) => {
const items = item.items && (
<ul className={this.props.cx('list', 'list--child')}>
{item.items
.slice(0, this.getLimit())
.map(child => this.renderItem(child, item))}
</ul>
);
const itemHasChildren = item.items && Boolean(item.items.length);

return (
<li
Expand All @@ -76,11 +70,17 @@ class List extends Component {
'item',
item.isRefined && 'item--selected',
item.noRefinement && 'item--noRefinement',
items && 'item--parent'
itemHasChildren && 'item--parent'
)}
>
{this.props.renderItem(item, resetQuery)}
{items}
{itemHasChildren && (
<ul className={this.props.cx('list', 'list--child')}>
{item.items
.slice(0, this.getLimit())
.map(child => this.renderItem(child, item))}
</ul>
)}
</li>
);
};
Expand Down
147 changes: 147 additions & 0 deletions packages/react-instantsearch-dom/src/components/__tests__/List.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import React from 'react';
import Enzyme, { shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import List from '../List';

Enzyme.configure({ adapter: new Adapter() });

describe('List', () => {
const defaultProps = {
items: [],
canRefine: true,
renderItem: item => <span>{item.value}</span>,
cx: (...args) => args.filter(Boolean).join(' '),
};

const apple = {
label: 'Apple',
value: 'Apple',
count: 100,
isRefined: false,
};

const appleSubElements = [
{
label: 'iPhone',
value: 'iPhone',
count: 50,
isRefined: false,
},
{
label: 'iPad',
value: 'iPad',
count: 50,
isRefined: false,
},
];

const samsung = {
label: 'Samsung',
value: 'Samsung',
count: 50,
isRefined: false,
};

const samsungSubElements = [
{
label: 'S8',
value: 'S8',
count: 25,
isRefined: false,
},
{
label: 'Note 5',
value: 'Note 5',
count: 25,
isRefined: false,
},
];

const microsoft = {
label: 'Microsoft',
value: 'Microsoft',
count: 25,
isRefined: false,
};

const microsoftSubElements = [
{
label: 'Surface',
value: 'Surface',
count: 13,
isRefined: false,
},
{
label: 'Surface Pro',
value: 'Surface Pro',
count: 12,
isRefined: false,
},
];

it('expect to render a list of items', () => {
const props = {
...defaultProps,
items: [apple, samsung, microsoft],
};

const wrapper = shallow(<List {...props} />);

expect(wrapper.find('.item')).toHaveLength(3);

expect(wrapper).toMatchSnapshot();
});

it('expect to render a list of nested items', () => {
const props = {
...defaultProps,
items: [
{
...apple,
items: appleSubElements,
},
{
...samsung,
items: samsungSubElements,
},
{
...microsoft,
items: microsoftSubElements,
},
],
};

const wrapper = shallow(<List {...props} />);

expect(wrapper.find('.item')).toHaveLength(9); // 3 parents + (3 * 2 children)
expect(wrapper.find('.item--parent')).toHaveLength(3);
expect(wrapper.find('.list--child')).toHaveLength(3);

expect(wrapper).toMatchSnapshot();
});

it('expect to render a list of nested items with empty children', () => {
const props = {
...defaultProps,
items: [
{
...apple,
items: appleSubElements,
},
{
...samsung,
items: samsungSubElements,
},
microsoft,
],
};

const wrapper = shallow(<List {...props} />);

expect(wrapper.find('.item')).toHaveLength(7); // 3 parents + (2 * 2 children)
expect(wrapper.find('.item--parent')).toHaveLength(2);
expect(wrapper.find('.list--child')).toHaveLength(2);

expect(wrapper).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`List expect to render a list of items 1`] = `
<div
className=""
>
<ul
className="list"
>
<li
className="item"
key="Apple"
>
<span>
Apple
</span>
</li>
<li
className="item"
key="Samsung"
>
<span>
Samsung
</span>
</li>
<li
className="item"
key="Microsoft"
>
<span>
Microsoft
</span>
</li>
</ul>
</div>
`;

exports[`List expect to render a list of nested items 1`] = `
<div
className=""
>
<ul
className="list"
>
<li
className="item item--parent"
key="Apple"
>
<span>
Apple
</span>
<ul
className="list list--child"
>
<li
className="item"
key="iPhone"
>
<span>
iPhone
</span>
</li>
<li
className="item"
key="iPad"
>
<span>
iPad
</span>
</li>
</ul>
</li>
<li
className="item item--parent"
key="Samsung"
>
<span>
Samsung
</span>
<ul
className="list list--child"
>
<li
className="item"
key="S8"
>
<span>
S8
</span>
</li>
<li
className="item"
key="Note 5"
>
<span>
Note 5
</span>
</li>
</ul>
</li>
<li
className="item item--parent"
key="Microsoft"
>
<span>
Microsoft
</span>
<ul
className="list list--child"
>
<li
className="item"
key="Surface"
>
<span>
Surface
</span>
</li>
<li
className="item"
key="Surface Pro"
>
<span>
Surface Pro
</span>
</li>
</ul>
</li>
</ul>
</div>
`;

exports[`List expect to render a list of nested items with empty children 1`] = `
<div
className=""
>
<ul
className="list"
>
<li
className="item item--parent"
key="Apple"
>
<span>
Apple
</span>
<ul
className="list list--child"
>
<li
className="item"
key="iPhone"
>
<span>
iPhone
</span>
</li>
<li
className="item"
key="iPad"
>
<span>
iPad
</span>
</li>
</ul>
</li>
<li
className="item item--parent"
key="Samsung"
>
<span>
Samsung
</span>
<ul
className="list list--child"
>
<li
className="item"
key="S8"
>
<span>
S8
</span>
</li>
<li
className="item"
key="Note 5"
>
<span>
Note 5
</span>
</li>
</ul>
</li>
<li
className="item"
key="Microsoft"
>
<span>
Microsoft
</span>
</li>
</ul>
</div>
`;

0 comments on commit 9eb2cbb

Please sign in to comment.