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

Fix unnecessary update of ra-input-rich-text on edit #3099

Merged
merged 7 commits into from
Apr 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"jest": "^23.6.0",
"lerna": "~2.9.1",
"lolex": "~2.3.2",
"mutationobserver-shim": "^0.3.3",
"prettier": "~1.16.4",
"raf": "~3.4.0",
"ts-jest": "^23.10.5",
Expand Down
9 changes: 8 additions & 1 deletion packages/ra-input-rich-text/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { withStyles } from '@material-ui/core/styles';
import styles from './styles';

export class RichTextInput extends Component {
lastValueChange = null;

static propTypes = {
addLabel: PropTypes.bool.isRequired,
classes: PropTypes.object,
Expand Down Expand Up @@ -56,6 +58,10 @@ export class RichTextInput extends Component {
this.quill.on('text-change', debounce(this.onTextChange, 500));
}

shouldComponentUpdate(nextProps) {
return nextProps.input.value !== this.lastValueChange;
}

componentDidUpdate(prevProps) {
if (prevProps.input.value !== this.props.input.value) {
const selection = this.quill.getSelection();
Expand All @@ -76,6 +82,7 @@ export class RichTextInput extends Component {
onTextChange = () => {
const value =
this.editor.innerHTML == '<p><br></p>' ? '' : this.editor.innerHTML;
this.lastValueChange = value;
this.props.input.onChange(value);
};

Expand All @@ -91,7 +98,7 @@ export class RichTextInput extends Component {
fullWidth={this.props.fullWidth}
className="ra-rich-text-input"
>
<div ref={this.updateDivRef} />
<div data-testid="quill" ref={this.updateDivRef} />
{error && <FormHelperText error>{error}</FormHelperText>}
{helperText && <FormHelperText>{helperText}</FormHelperText>}
</FormControl>
Expand Down
52 changes: 52 additions & 0 deletions packages/ra-input-rich-text/src/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React from 'react';
import debounce from 'lodash/debounce';
import {render, fireEvent, waitForElement, cleanup, getByTestId} from 'react-testing-library'

import { RichTextInput } from './index';

let container;

jest.mock('lodash/debounce');
describe('RichTextInput', () => {
beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
document.getSelection = () => {
djhi marked this conversation as resolved.
Show resolved Hide resolved
return {
removeAllRanges: () => {},
getRangeAt: function() {},
};
};
jest.mock('lodash.debounce', (fn) => fn);
djhi marked this conversation as resolved.
Show resolved Hide resolved
});

afterEach(() => {
document.body.removeChild(container);
container = null;
});

it('should call text-change event only once when editing', async () => {
const mockFn = jest.fn();
Copy link
Member

Choose a reason for hiding this comment

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

call it handleChange

Copy link
Author

Choose a reason for hiding this comment

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

done

Copy link
Collaborator

Choose a reason for hiding this comment

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

Are you sure ? I still see mockFn

Copy link
Author

@roychoo roychoo Apr 19, 2019

Choose a reason for hiding this comment

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

Oh sorry I misunderstood, I thought you meant the test description. Will make the required change in another 3 hours.

Copy link
Author

Choose a reason for hiding this comment

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

@djhi done.

debounce.mockImplementation(fn => fn);
djhi marked this conversation as resolved.
Show resolved Hide resolved
const { getByTestId, rerender } = render(
<RichTextInput
input={{
value: '<p>test</p>',
onChange: mockFn
}}
meta={{error: null}} />);
const quillNode = await waitForElement(() => {
return getByTestId('quill')
});
quillNode.__quill.setText('test1');
djhi marked this conversation as resolved.
Show resolved Hide resolved
rerender(
<RichTextInput
input={{
value: '<p>test1</p>',
onChange: mockFn
}}
meta={{error: null}} />)

expect(mockFn).toHaveBeenCalledTimes(1);
});
})
1 change: 1 addition & 0 deletions test-setup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require('raf/polyfill');
require('mutationobserver-shim');
djhi marked this conversation as resolved.
Show resolved Hide resolved
var enzyme = require('enzyme');
var Adapter = require('enzyme-adapter-react-16');

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10181,6 +10181,11 @@ multimatch@^2.0.0:
arrify "^1.0.0"
minimatch "^3.0.0"

mutationobserver-shim@^0.3.3:
version "0.3.3"
resolved "https://registry.yarnpkg.com/mutationobserver-shim/-/mutationobserver-shim-0.3.3.tgz#65869630bc89d7bf8c9cd9cb82188cd955aacd2b"
integrity sha512-gciOLNN8Vsf7YzcqRjKzlAJ6y7e+B86u7i3KXes0xfxx/nfLmozlW1Vn+Sc9x3tPIePFgc1AeIFhtRgkqTjzDQ==

mute-stream@0.0.7:
version "0.0.7"
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.7.tgz#3075ce93bc21b8fab43e1bc4da7e8115ed1e7bab"
Expand Down