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 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
65 changes: 53 additions & 12 deletions packages/e2e-tests/specs/editor/various/autosave.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@ const AUTOSAVE_INTERVAL_SECONDS = 5;
const AUTOSAVE_NOTICE_REMOTE = 'There is an autosave of this post that is more recent than the version below.';
const AUTOSAVE_NOTICE_LOCAL = 'The backup of this post in your browser is different from the version below.';

// Save and wait for "Saved" to confirm save complete. Preserves focus in the
// editing area.
async function saveDraftWithKeyboard() {
return pressKeyWithModifier( 'primary', 's' );
await page.waitForSelector( '.editor-post-save-draft' );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI, this line here was the missing key to make the tests work in a headless setting.

Copy link
Member

Choose a reason for hiding this comment

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

Does page.waitForSelector( '.editor-post-saved-state.is-saved' ) not check for this as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They're different things:

  1. Make sure that the editor is "ready" for a save. In real usage, or when testing with PUPPETEER_HEADLESS=false or _SLOWMO=50, this isn't an issue, but when testing fast it's possible to reach saveDraftWithKeyboard before the UI has updated to reflect its dirty state.
  2. Thanks to this wait, we ensure that the ⌘S keypress actually triggers a save, and that's when we concert the next two promises under Promise.all.

Copy link
Member

Choose a reason for hiding this comment

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

it's a common issue, that we don't wait for UI elements to show up. We should do it more often to avoid false negatives.

await Promise.all( [
page.waitForSelector( '.editor-post-saved-state.is-saved' ),
pressKeyWithModifier( 'primary', 'S' ),
] );
}

async function sleep( durationInSeconds ) {
return new Promise( ( resolve ) =>
setTimeout( resolve, durationInSeconds * 1000 ) );
// Rule `no-restricted-syntax` recommends `waitForSelector` against
// `waitFor`, which isn't apt for the use case, when provided an integer,
// of waiting for a given amount of time.
// eslint-disable-next-line no-restricted-syntax
await page.waitFor( durationInSeconds * 1000 );
}

async function clearSessionStorage() {
Expand Down Expand Up @@ -153,11 +162,11 @@ describe( 'autosave', () => {
} );

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

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

// Trigger local autosave
await page.evaluate( () => window.wp.data.dispatch( 'core/editor' ).__experimentalLocalAutosave() );
Expand All @@ -169,21 +178,52 @@ describe( 'autosave', () => {
} );

it( 'shouldn\'t clear local autosave if remote autosave fails', async () => {
// Edit, save draft, edit again
await clickBlockAppender();
await page.keyboard.type( 'before save' );
await saveDraft();
await saveDraftWithKeyboard();
await page.keyboard.type( ' after save' );

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

// Bring network down and attempt to autosave remotely
toggleOfflineMode( true );

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

toggleOfflineMode( false );
it( 'should clear local autosave after successful save', async () => {
// Edit, save draft, edit again
await clickBlockAppender();
await page.keyboard.type( 'before save' );
await saveDraftWithKeyboard();
await page.keyboard.type( ' after save' );

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

await saveDraftWithKeyboard();
expect( await page.evaluate( () => window.sessionStorage.length ) ).toBe( 0 );
} );

it( 'shouldn\'t clear local autosave if save fails', async () => {
// Edit, save draft, edit again
await clickBlockAppender();
await page.keyboard.type( 'before save' );
await saveDraftWithKeyboard();
await page.keyboard.type( ' after save' );

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

// Bring network down and attempt to save
toggleOfflineMode( true );
saveDraftWithKeyboard();
expect( await page.evaluate( () => window.sessionStorage.length ) ).toBe( 1 );
} );

it( 'shouldn\'t conflict with server-side autosave', async () => {
Expand Down Expand Up @@ -221,7 +261,8 @@ describe( 'autosave', () => {
expect( notice ).toContain( AUTOSAVE_NOTICE_REMOTE );
} );

afterAll( async () => {
afterEach( async () => {
toggleOfflineMode( false );
await clearSessionStorage();
} );
} );
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