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 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
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
60 changes: 60 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,60 @@
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);
// required as quilljs uses getSelection api
document.getSelection = () => {
djhi marked this conversation as resolved.
Show resolved Hide resolved
return {
removeAllRanges: () => {},
getRangeAt: function() {},
};
};
});

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

it('should call handleChange only once when editing', async () => {
jest.useFakeTimers();
const handleChange = jest.fn();
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: handleChange
}}
meta={{error: null}} />);
const quillNode = await waitForElement(() => {
return getByTestId('quill')
});
const node = quillNode.querySelector('.ql-editor');
fireEvent.input(node, {
target: { innerHTML: '<p>test1</p>' }
});

// ensuring the first 'text-change' event had been handled
jest.runOnlyPendingTimers();

rerender(
<RichTextInput
input={{
value: '<p>test1</p>',
onChange: handleChange
}}
meta={{error: null}} />)

expect(handleChange).toHaveBeenCalledTimes(1);
});
})
5 changes: 5 additions & 0 deletions test-setup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
require('raf/polyfill');
/**
* As jsDom do not support mutationobserver and
* quill requires mutationobserver, thus a shim is needed
* */
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