-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Editor: PostTextEditor: Fix case where empty state value defers to prop #9513
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
packages/editor/src/components/post-text-editor/test/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { create } from 'react-test-renderer'; | ||
import Textarea from 'react-autosize-textarea'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { PostTextEditor } from '../'; | ||
|
||
// "Downgrade" ReactAutosizeTextarea to a regular textarea. Assumes aligned | ||
// props interface. | ||
jest.mock( 'react-autosize-textarea', () => ( props ) => <textarea { ...props } /> ); | ||
|
||
describe( 'PostTextEditor', () => { | ||
it( 'should render via the prop value', () => { | ||
const wrapper = create( <PostTextEditor value="Hello World" /> ); | ||
|
||
const textarea = wrapper.root.findByType( Textarea ); | ||
expect( textarea.props.value ).toBe( 'Hello World' ); | ||
} ); | ||
|
||
it( 'should render via the state value when edits made', () => { | ||
const onChange = jest.fn(); | ||
const wrapper = create( | ||
<PostTextEditor | ||
value="Hello World" | ||
onChange={ onChange } | ||
/> | ||
); | ||
|
||
const textarea = wrapper.root.findByType( Textarea ); | ||
textarea.props.onChange( { target: { value: 'Hello Chicken' } } ); | ||
|
||
expect( textarea.props.value ).toBe( 'Hello Chicken' ); | ||
expect( onChange ).toHaveBeenCalledWith( 'Hello Chicken' ); | ||
} ); | ||
|
||
it( 'should render via the state value when edits made, even if prop value changes', () => { | ||
const onChange = jest.fn(); | ||
const wrapper = create( | ||
<PostTextEditor | ||
value="Hello World" | ||
onChange={ onChange } | ||
/> | ||
); | ||
|
||
const textarea = wrapper.root.findByType( Textarea ); | ||
textarea.props.onChange( { target: { value: 'Hello Chicken' } } ); | ||
|
||
wrapper.update( | ||
<PostTextEditor | ||
value="Goodbye World" | ||
onChange={ onChange } | ||
/> | ||
); | ||
|
||
expect( textarea.props.value ).toBe( 'Hello Chicken' ); | ||
expect( onChange ).toHaveBeenCalledWith( 'Hello Chicken' ); | ||
} ); | ||
|
||
it( 'should render via the state value when edits made, even if prop value changes and state value empty', () => { | ||
const onChange = jest.fn(); | ||
const wrapper = create( | ||
<PostTextEditor | ||
value="Hello World" | ||
onChange={ onChange } | ||
/> | ||
); | ||
|
||
const textarea = wrapper.root.findByType( Textarea ); | ||
textarea.props.onChange( { target: { value: '' } } ); | ||
|
||
wrapper.update( | ||
<PostTextEditor | ||
value="Goodbye World" | ||
onChange={ onChange } | ||
/> | ||
); | ||
|
||
expect( textarea.props.value ).toBe( '' ); | ||
expect( onChange ).toHaveBeenCalledWith( '' ); | ||
} ); | ||
|
||
it( 'calls onPersist after changes made and user stops editing', () => { | ||
const onPersist = jest.fn(); | ||
const wrapper = create( | ||
<PostTextEditor | ||
value="Hello World" | ||
onChange={ () => {} } | ||
onPersist={ onPersist } | ||
/> | ||
); | ||
|
||
const textarea = wrapper.root.findByType( Textarea ); | ||
textarea.props.onChange( { target: { value: '' } } ); | ||
textarea.props.onBlur(); | ||
|
||
expect( onPersist ).toHaveBeenCalledWith( '' ); | ||
} ); | ||
|
||
it( 'does not call onPersist after user stops editing without changes', () => { | ||
const onPersist = jest.fn(); | ||
const wrapper = create( | ||
<PostTextEditor | ||
value="Hello World" | ||
onChange={ () => {} } | ||
onPersist={ onPersist } | ||
/> | ||
); | ||
|
||
const textarea = wrapper.root.findByType( Textarea ); | ||
textarea.props.onBlur(); | ||
|
||
expect( onPersist ).not.toHaveBeenCalled(); | ||
} ); | ||
|
||
it( 'resets to prop value after user stops editing', () => { | ||
// This isn't the most realistic case, since typically we'd assume the | ||
// parent renderer to pass the value as it had received onPersist. The | ||
// test here is more an edge case to stress that it's intentionally | ||
// differentiating between state and prop values. | ||
const wrapper = create( | ||
<PostTextEditor | ||
value="Hello World" | ||
onChange={ () => {} } | ||
onPersist={ () => {} } | ||
/> | ||
); | ||
|
||
const textarea = wrapper.root.findByType( Textarea ); | ||
textarea.props.onChange( { target: { value: '' } } ); | ||
|
||
wrapper.update( | ||
<PostTextEditor | ||
value="Goodbye World" | ||
onChange={ () => {} } | ||
onPersist={ () => {} } | ||
/> | ||
); | ||
|
||
textarea.props.onBlur(); | ||
|
||
expect( textarea.props.value ).toBe( 'Goodbye World' ); | ||
} ); | ||
} ); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this is a bit weird :). I suppose this is only needed to trigger the post dirtiness, right? I wonder if a specific action is not better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Granted it's not being changed in this PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you elaborate on what's weird here?
onChange
is called to surface the edit to the editor store immediately (so as to mark for dirty detection).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we filling the
content
in theedits
? For me theblocks
is theedits
of thecontent
property. And we're already filling the blocks properly when persisting. I bet this is only needed to trigger a "dirty" state.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty much this, yes 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this better than a
SET_DIRTY
action or something like that? It's a bit clearerThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's an option, one which would contradict the purpose of proposed #7409.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't necessarily agree that
SET_DIRTY
is clearer in the broader sense. In practical terms as it impacts what we're doing here, sure, the intent with callingeditPost
is to make sure the post becomes marked as dirty, but dirtiness is not really meant to be an explicit action, more a reflection of the current state (diff of edits and saved post). It's by various complexities of content/blocks syncing that we've landed at our current implementation, but I'd rather not further engrain ourselves to this idea of "make dirty" if we can help it.