Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShowMoreCard should expand if updated content changes #3057

Merged
merged 1 commit into from
Sep 4, 2017
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
9 changes: 3 additions & 6 deletions src/ui/components/ShowMoreCard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import classNames from 'classnames';
import React from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
import { compose } from 'redux';

import translate from 'core/i18n/translate';
Expand All @@ -29,15 +28,13 @@ export class ShowMoreCardBase extends React.Component {
}

componentWillReceiveProps() {
this.truncateToMaxHeight(ReactDOM.findDOMNode(this.contents));
this.setState({ expanded: true }, () => {
this.truncateToMaxHeight(this.contents);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will work in production but I don't think it will work for our test suite. The problem is that React does not guarantee that the callback to setState() will be invoked immediately. Thus, the test you wrote will probably pass most of the time but not all of the time. If the setState() callback is delayed then your test will fail.

There are a few ways to solve this. We could try to wedge promises in here so that the test can use a promise chain. I don't see an easy way to do that.

Alternatively, we could keep the process synchronous which would make the test you already wrote pass 100% of the time. I think restoring componentWillReceiveProps() to how it was and adding this else block would accomplish that:

truncateToMaxHeight = (contents) => {
  // If the contents are short enough they don't need a "show more" link; the
  // contents are expanded by default.
  if (contents.clientHeight > MAX_HEIGHT) {
    this.setState({ expanded: false });
  } else {
    this.setState({ expanded: true });
  }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it's possible that Enzyme has some trickery to ensure all batched state changes are applied but they don't document this. I'm skeptical since this kind of state change is triggered from within the component (not from an Enzyme method).

Copy link
Member Author

@willdurand willdurand Sep 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What we could do (maybe) is reusing the callback from Enzyme setState() (see: enzymejs/enzyme#617) and ensure the test case asserts things once the setState() has been triggered (and use done() to ensure the test does not silently fail). WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh, yeah, we really can't rely on setState() being synchronous anywhere. enzymejs/enzyme#509 (comment)

It would be nice to use promises instead of done() but your approach will work. Creating a promise wrapper for everywhere we rely on wrapper.setState() in the tests is probably a better long term solution.

});
}

onClick = (event) => {
event.preventDefault();
this.expandText();
}

expandText() {
this.setState({ expanded: true });
}

Expand Down
29 changes: 24 additions & 5 deletions tests/unit/ui/components/TestShowMoreCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function render(props) {
);
}

describe('<ShowMoreCard />', () => {
describe(__filename, () => {
it('reveals more text when clicking "show more" link', () => {
const root = render({ children: 'Hello I am description' });
const rootNode = findDOMNode(root);
Expand All @@ -22,26 +22,26 @@ describe('<ShowMoreCard />', () => {
// don't have a clientHeight in the tests.
root.setState({ expanded: false });
expect(rootNode.className).not.toContain('.ShowMoreCard--expanded');
expect(root.state.expanded).toBe(false);
expect(root.state.expanded).toEqual(false);
expect(rootNode.querySelector('.Card-footer-link').textContent).toEqual('Expand to Read more');

Simulate.click(rootNode.querySelector('.Card-footer-link a'));

expect(rootNode.className).toContain('ShowMoreCard--expanded');
expect(root.state.expanded).toBe(true);
expect(root.state.expanded).toEqual(true);

expect(rootNode.querySelector('.Card-footer-link')).toEqual(null);
});

it('is expanded by default', () => {
const root = render({ children: 'Hello I am description' });
expect(root.state.expanded).toBe(true);
expect(root.state.expanded).toEqual(true);
});

it('truncates the contents if they are too long', () => {
const root = render({ children: 'Hello I am description' });
root.truncateToMaxHeight({ clientHeight: 101 });
expect(root.state.expanded).toBe(false);
expect(root.state.expanded).toEqual(false);
});

it('renders className', () => {
Expand Down Expand Up @@ -69,4 +69,23 @@ describe('<ShowMoreCard />', () => {

sinon.assert.calledWith(truncateToMaxHeight, contentNode);
});

it('expands if new child content is smaller', () => {
const root = mount(
<ShowMoreCardBase i18n={getFakeI18nInst()}>
This would be very long content, but we cannot get `clientHeight` in
the tests, so this will be forced below (via setState()).
</ShowMoreCardBase>
);

// We have to manually set the expanded flag to false because we don't have
// a `clientHeight` in the tests.
root.setState({ expanded: false });

expect(root.state('expanded')).toEqual(false);
// This will call `componentWillReceiveProps()`, the content of `children`
// is for example purpose.
root.setProps({ children: 'short content' });
expect(root.state('expanded')).toEqual(true);
});
});