Skip to content

Commit

Permalink
Merge a2dd887 into 51c49c7
Browse files Browse the repository at this point in the history
  • Loading branch information
createthis committed Aug 29, 2019
2 parents 51c49c7 + a2dd887 commit 782202f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ typings/

# compiled
dist

# I don't see this committed, so putting in ignore
package-lock.json

# vim swap files
**/*.swp
9 changes: 0 additions & 9 deletions license

This file was deleted.

9 changes: 9 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class EllipisWithTooltip extends React.Component {
this.state = {
hasOverflowingChildren: false,
text: undefined,
prevPropsChildren: props.children,
};
this.updateOverflow = this.updateOverflow.bind(this);
}
Expand All @@ -35,6 +36,14 @@ class EllipisWithTooltip extends React.Component {
}
}

static getDerivedStateFromProps(props, state) {
if (props.children === state.prevPropsChildren) return null;
return {
hasOverflowingChildren: false,
prevPropsChildren: props.children,
};
}

render() {
const { hasOverflowingChildren, text } = this.state;
const {
Expand Down
37 changes: 37 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,41 @@ describe('Ellipsis', () => {

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

it('should update text when props change', () => {
const wrapper = setup('this is some long text');
let domElement = wrapper.find('#a div').getDOMNode();

expect(wrapper.state().hasOverflowingChildren).toEqual(false);
Object.defineProperty(domElement, 'clientWidth', {
get() {
return 300;
},
});
Object.defineProperty(domElement, 'scrollWidth', {
get() {
return 400;
},
});
wrapper.find('#a div').simulate('mouseEnter');
expect(wrapper.state().hasOverflowingChildren).toEqual(true);

expect(wrapper.state().text).toEqual('this is some long text');
wrapper.setProps({ children: 'this changed' });
expect(wrapper.state().hasOverflowingChildren).toEqual(false);
domElement = wrapper.find('#a div').getDOMNode();
Object.defineProperty(domElement, 'clientWidth', {
get() {
return 300;
},
});
Object.defineProperty(domElement, 'scrollWidth', {
get() {
return 400;
},
});
wrapper.find('#a div').simulate('mouseEnter');
expect(wrapper.state().hasOverflowingChildren).toEqual(true);
expect(wrapper.state().text).toEqual('this changed');
});
});

0 comments on commit 782202f

Please sign in to comment.