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

Local autosave: Clear after successful save #18051

Merged
merged 2 commits into from
Oct 24, 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
43 changes: 43 additions & 0 deletions packages/e2e-tests/specs/editor/various/autosave.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,49 @@ describe( 'autosave', () => {
toggleOfflineMode( false );
} );

it( 'should clear local autosave after successful save', async () => {
await clickBlockAppender();
await page.keyboard.type( 'before save' );
await saveDraft();

await page.keyboard.type( 'after save' );

// Trigger local autosave
expect( await page.evaluate( () => window.sessionStorage.length ) ).toBe( 0 );
await page.evaluate( () => window.wp.data.dispatch( 'core/editor' ).__experimentalLocalAutosave() );
expect( await page.evaluate( () => window.sessionStorage.length ) ).toBe( 1 );

// Save and wait for "Saved" to confirm save complete.
await Promise.all( [
page.waitForSelector( '.editor-post-saved-state.is-saved' ),
pressKeyWithModifier( 'primary', 'S' ),
] );
Copy link
Member

Choose a reason for hiding this comment

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

I've seen it in a few tests already. It might be a good idea to open a follow-up and put it into a shared utility helper.


expect( await page.evaluate( () => window.sessionStorage.length ) ).toBe( 1 );
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't the length be 0 in this case? The test description is:

should clear local autosave after successful save

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh wow, how did that happen? 😰 I'll look. In the meantime, in actual usage the issue seemed to be fixed.

} );

it( 'shouldn\'t clear local autosave if save fails', async () => {
await clickBlockAppender();
await page.keyboard.type( 'before save' );
await saveDraft();

await page.keyboard.type( 'after save' );
await page.evaluate( () => window.wp.data.dispatch( 'core/editor' ).__experimentalLocalAutosave() );
expect( await page.evaluate( () => window.sessionStorage.length ) ).toBe( 1 );

toggleOfflineMode( true );

// Save and wait for "Saved" to confirm save complete.
await Promise.all( [
page.waitForSelector( '.editor-post-saved-state.is-saved' ),
pressKeyWithModifier( 'primary', 'S' ),
] );

expect( await page.evaluate( () => window.sessionStorage.length ) ).toBe( 1 );

toggleOfflineMode( false );
Copy link
Member

Choose a reason for hiding this comment

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

In case the previous expectation fails, the offline mode will get propagated to other tests giving false negatives there. It's safer to clean up with afterEach.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, great point.

} );

it( 'shouldn\'t conflict with server-side autosave', async () => {
await clickBlockAppender();
await page.keyboard.type( 'before publish' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ function useAutosavePurge() {
const lastIsAutosaving = useRef( isAutosaving );

useEffect( () => {
if ( lastIsAutosaving.current && ! isAutosaving && ! didError ) {
if (
! didError && (
( lastIsAutosaving.current && ! isAutosaving ) ||
( lastIsDirty.current && ! isDirty )
)
) {
localAutosaveClear( postId );
}

Expand Down