Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

fix(chips): copy selectedChipIds before calling setState #645

Merged
merged 6 commits into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/chips/ChipSet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export default class ChipSet extends React.Component<ChipSetProps, ChipSetState>
return {
hasClass: (className: string) => this.classes.split(' ').includes(className),
setSelected: () => {
const selectedChipIds = this.state.foundation.getSelectedChipIds();
const selectedChipIds = this.state.foundation.getSelectedChipIds().slice();
this.setState({selectedChipIds}, () => {
this.props.handleSelect(selectedChipIds);
});
Expand Down
45 changes: 45 additions & 0 deletions test/unit/chips/ChipSet.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,51 @@ test('#handleSelect calls foundation.handleChipSelection with selectedChipId and
td.verify(handleChipSelection('1', false), {times: 1});
});

test('#handleSelect calls updates parent component with selectedChipIds correctly for filter chips', () => {
type HandleSelectMethod = (prevSelectedChipIds: string[], selectedChipId: string[]) => void;
type Props = {
handleSelect: HandleSelectMethod
};
type State = {selectedChipIds: string[]};

class TestChipParentComponent extends React.Component<Props, State> {
state = {selectedChipIds: []};
componentDidUpdate(_: Props, pState: State) {
if (pState.selectedChipIds !== this.state.selectedChipIds) {
this.props.handleSelect(pState.selectedChipIds, this.state.selectedChipIds);
}
}
handleSelect = (selectedChipIds: string[]) => {
this.setState({selectedChipIds});
}
render() {
const Chip1 = <Chip id='chip1'/>;
const Chip2 = <Chip id='chip2'/>;
return (
<ChipSet
filter
selectedChipIds={this.state.selectedChipIds}
handleSelect={this.handleSelect}
>
{Chip1}
{Chip2}
</ChipSet>
);
}
}

const handleChipSelection = coerceForTesting<HandleSelectMethod>(td.func());
const wrapper = mount<TestChipParentComponent>(
<TestChipParentComponent handleSelect={handleChipSelection}/>
);
const chipSet = wrapper.childAt(0);
chipSet.children().children().first().children().simulate('click');
td.verify(handleChipSelection([], ['chip1']), {times: 1});
chipSet.children().children().last().children().simulate('click');
td.verify(handleChipSelection(['chip1'], ['chip1', 'chip2']), {times: 1});
});


test('#handleInteraction calls #foundation.handleChipInteraction', () => {
const handleChipInteraction = td.func();
const foundation = {handleChipInteraction};
Expand Down