Skip to content

Commit c1fd941

Browse files
createthisamirfefer
authored andcommitted
fix: update state.text if props.children change
1 parent 51c49c7 commit c1fd941

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,9 @@ typings/
5959

6060
# compiled
6161
dist
62+
63+
# I don't see this committed, so putting in ignore
64+
package-lock.json
65+
66+
# vim swap files
67+
**/*.swp

src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ class EllipisWithTooltip extends React.Component {
3535
}
3636
}
3737

38+
componentWillReceiveProps(nextProps) {
39+
if (nextProps.children === this.props.children) return;
40+
this.setState({ hasOverflowingChildren: false });
41+
}
42+
3843
render() {
3944
const { hasOverflowingChildren, text } = this.state;
4045
const {

src/index.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,25 @@ describe('Ellipsis', () => {
109109

110110
expect(toJson(wrapper)).toMatchSnapshot();
111111
});
112+
113+
it('should update text when props change', () => {
114+
const wrapper = setup('this is some long text');
115+
let domElement = wrapper.find('#a div').getDOMNode();
116+
117+
expect(wrapper.state().hasOverflowingChildren).toEqual(false);
118+
Object.defineProperty(domElement, 'clientWidth', { get() { return 300; } });
119+
Object.defineProperty(domElement, 'scrollWidth', { get() { return 400; } });
120+
wrapper.find('#a div').simulate('mouseEnter');
121+
expect(wrapper.state().hasOverflowingChildren).toEqual(true);
122+
123+
expect(wrapper.state().text).toEqual('this is some long text');
124+
wrapper.setProps({children: 'this changed'});
125+
expect(wrapper.state().hasOverflowingChildren).toEqual(false);
126+
domElement = wrapper.find('#a div').getDOMNode();
127+
Object.defineProperty(domElement, 'clientWidth', { get() { return 300; } });
128+
Object.defineProperty(domElement, 'scrollWidth', { get() { return 400; } });
129+
wrapper.find('#a div').simulate('mouseEnter');
130+
expect(wrapper.state().hasOverflowingChildren).toEqual(true);
131+
expect(wrapper.state().text).toEqual('this changed');
132+
});
112133
});

0 commit comments

Comments
 (0)