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

Core Data: Test entity undo reducer. #18642

Merged
merged 3 commits into from
Nov 23, 2019

Conversation

epiqueras
Copy link
Contributor

Closes #17434

Description

This PR adds unit tests for core-data's new undo reducer.

It also fixes a minor edge case where we were updating a reference to the last edit action even if the new action only contained transient edits. This was causing us to skip/delete an undo level when explicitly marking the last changes as persistent.

How has this been tested?

They are tests 😄

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR. .

@epiqueras epiqueras added [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests. [Package] Core data /packages/core-data [Feature] History History, undo, redo, revisions, autosave. labels Nov 20, 2019
@epiqueras epiqueras added this to the Future milestone Nov 20, 2019
@epiqueras epiqueras self-assigned this Nov 20, 2019
@epiqueras epiqueras requested review from ellatrix and mcsf November 20, 2019 19:47
lastEdits = {};
undoState = undefined;
expectedUndoState = [];
expectedUndoState.offset = 0;
Copy link
Member

Choose a reason for hiding this comment

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

This is interesting to see in a state, as normally the state should be serialisable. Could you comment on this in the reducer? Why can't it be nested?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It could be nested. I just followed from the previous iteration and I think I've seen other places where this is done.

I guess we should change it in another PR if we need to serialize it.

let action = {};
if ( args[ 0 ] === 'isUndo' || args[ 0 ] === 'isRedo' ) {
// We need to "apply" the undo level here and build
// the action to move the offset.
Copy link
Member

Choose a reason for hiding this comment

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

Why does the action need to be build based on the state? Isn't that something that can be done in the reducer?

Copy link
Member

Choose a reason for hiding this comment

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

Previously we had UNDO and REDO actions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because the editor grabs the undo edit and applies it, the undo reducer just keeps track of the offset.

// Don't update the last edit cache if the new one only has transient edits.
// Transient edits don't create new levels so updating the cache would make
// us skip an edit later when creating levels explicitly.
Object.keys( action.edits ).some( ( key ) => ! action.transientEdits[ key ] )
Copy link
Member

Choose a reason for hiding this comment

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

Is there an e2e test case that could fail without this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, it's not a flow that happens in the editor, but it could happen with other third party usage.

transientEdits,
};
action = {
type: 'EDIT_ENTITY_RECORD',
Copy link
Member

Choose a reason for hiding this comment

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

I really liked how the previous history reduces could work with any state and actions. This one seems to handle EDIT_ENTITY_RECORD. Is there any way it could be more abstracted? Cc @aduth.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What other actions does it need to handle for entities?

I don't think this is how this reducer will look like in the long term, specially after adding support for collaborative editing.

But, for now it gives us a lot of control over details that allow us to meet requirements for things like explicit levels and other nuances that the editor and block editor need. A more generalized abstraction could make that harder.

Copy link
Member

@ellatrix ellatrix left a comment

Choose a reason for hiding this comment

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

Thanks! It's great to have some tests :)

@epiqueras
Copy link
Contributor Author

@ellatrix My change to the reducer needed a tweak. I would appreciate another look 😄

@ellatrix
Copy link
Member

In 3bcddbe, I see changed and added code, but no extra tests. Would it make sense to have tests for this part?

@epiqueras
Copy link
Contributor Author

@ellatrix Yeah, but it fixed an e2e test that was failing, so it's already covered.

@epiqueras epiqueras force-pushed the add/core-data-undo-reducer-unit-tests branch from e817ff6 to f3ac6ba Compare November 23, 2019 00:30
@epiqueras epiqueras force-pushed the add/core-data-undo-reducer-unit-tests branch from f3ac6ba to 05e4845 Compare November 23, 2019 01:03
@epiqueras epiqueras merged commit ba8cd71 into master Nov 23, 2019
@epiqueras epiqueras deleted the add/core-data-undo-reducer-unit-tests branch November 23, 2019 01:37
@youknowriad youknowriad modified the milestones: Future, Gutenberg 7.0 Nov 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] History History, undo, redo, revisions, autosave. [Package] Core data /packages/core-data [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Increase Coverage of Undo/History E2E Tests
3 participants