-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathautosave.test.js
268 lines (221 loc) · 9.5 KB
/
autosave.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
* WordPress dependencies
*/
import {
clickBlockAppender,
createNewPost,
getEditedPostContent,
pressKeyWithModifier,
publishPost,
saveDraft,
toggleOfflineMode,
} from '@wordpress/e2e-test-utils';
// Constant to override editor preference
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() {
await page.waitForSelector( '.editor-post-save-draft' );
await Promise.all( [
page.waitForSelector( '.editor-post-saved-state.is-saved' ),
pressKeyWithModifier( 'primary', 'S' ),
] );
}
async function sleep( durationInSeconds ) {
// 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() {
await page.evaluate( () => window.sessionStorage.clear() );
}
async function readSessionStorageAutosave( postId ) {
return page.evaluate(
( key ) => window.sessionStorage.getItem( key ),
`wp-autosave-block-editor-post-${ postId }`
);
}
async function getCurrentPostId() {
return page.evaluate(
() => window.wp.data.select( 'core/editor' ).getCurrentPostId()
);
}
async function setLocalAutosaveInterval( value ) {
return page.evaluate( ( _value ) => {
window.wp.data.dispatch( 'core/edit-post' )
.__experimentalUpdateLocalAutosaveInterval( _value );
}, value );
}
function wrapParagraph( text ) {
return `<!-- wp:paragraph -->
<p>${ text }</p>
<!-- /wp:paragraph -->`;
}
describe( 'autosave', () => {
beforeEach( async () => {
await clearSessionStorage();
await createNewPost();
await setLocalAutosaveInterval( AUTOSAVE_INTERVAL_SECONDS );
} );
it( 'should save to sessionStorage', async () => {
await clickBlockAppender();
await page.keyboard.type( 'before save' );
await saveDraftWithKeyboard();
await page.keyboard.type( ' after save' );
// Wait long enough for local autosave to kick in
await sleep( AUTOSAVE_INTERVAL_SECONDS + 1 );
const id = await getCurrentPostId();
const autosave = await readSessionStorageAutosave( id );
const { content } = JSON.parse( autosave );
expect( content ).toBe( wrapParagraph( 'before save after save' ) );
// Test throttling by scattering typing
await page.keyboard.type( ' 1' );
await sleep( AUTOSAVE_INTERVAL_SECONDS - 4 );
await page.keyboard.type( '2' );
await sleep( 2 );
await page.keyboard.type( '3' );
await sleep( 2 );
const newAutosave = await readSessionStorageAutosave( id );
expect( JSON.parse( newAutosave ).content ).toBe( wrapParagraph( 'before save after save 123' ) );
} );
it( 'should recover from sessionStorage', async () => {
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() );
// Reload without saving on the server
await page.reload();
const notice = await page.$eval( '.components-notice__content', ( element ) => element.innerText );
expect( notice ).toContain( AUTOSAVE_NOTICE_LOCAL );
expect( await getEditedPostContent() ).toEqual( wrapParagraph( 'before save' ) );
await page.click( '.components-notice__action' );
expect( await getEditedPostContent() ).toEqual( wrapParagraph( 'before save after save' ) );
} );
it( 'should clear sessionStorage upon user logout', async () => {
await clickBlockAppender();
await page.keyboard.type( 'before save' );
await saveDraft();
// Fake local autosave
await page.evaluate( ( postId ) => window.sessionStorage.setItem(
`wp-autosave-block-editor-post-${ postId }`,
JSON.stringify( { post_title: 'A', content: 'B', excerpt: 'C' } )
), await getCurrentPostId() );
expect( await page.evaluate( () => window.sessionStorage.length ) ).toBe( 1 );
await Promise.all( [
page.waitForSelector( '#wp-admin-bar-logout', { visible: true } ),
page.hover( '#wp-admin-bar-my-account' ),
] );
await Promise.all( [
page.waitForNavigation(),
page.click( '#wp-admin-bar-logout' ),
] );
expect( await page.evaluate( () => window.sessionStorage.length ) ).toBe( 0 );
} );
it( 'shouldn\'t contaminate other posts', async () => {
await clickBlockAppender();
await page.keyboard.type( 'before save' );
await saveDraft();
// Fake local autosave
await page.evaluate( ( postId ) => window.sessionStorage.setItem(
`wp-autosave-block-editor-post-${ postId }`,
JSON.stringify( { post_title: 'A', content: 'B', excerpt: 'C' } )
), await getCurrentPostId() );
expect( await page.evaluate( () => window.sessionStorage.length ) ).toBe( 1 );
await page.reload();
const notice = await page.$eval( '.components-notice__content', ( element ) => element.innerText );
expect( notice ).toContain( 'The backup of this post in your browser is different from the version below.' );
await createNewPost();
expect( await page.$( '.components-notice__content' ) ).toBe( null );
} );
it( 'should clear local autosave after successful remote autosave', 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 );
// Trigger remote autosave
await page.evaluate( () => window.wp.data.dispatch( 'core/editor' ).autosave() );
expect( await page.evaluate( () => window.sessionStorage.length ) ).toBe( 0 );
} );
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 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 autosave remotely
toggleOfflineMode( true );
await page.evaluate( () => window.wp.data.dispatch( 'core/editor' ).autosave() );
expect( await page.evaluate( () => window.sessionStorage.length ) ).toBe( 1 );
} );
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 () => {
await clickBlockAppender();
await page.keyboard.type( 'before publish' );
await publishPost();
await page.click( '.wp-block-paragraph' );
await page.keyboard.type( ' after publish' );
// Trigger remote autosave
await page.evaluate( () => window.wp.data.dispatch( 'core/editor' ).autosave() );
// Force conflicting local autosave
await page.evaluate( () => window.wp.data.dispatch( 'core/editor' ).__experimentalLocalAutosave() );
expect( await page.evaluate( () => window.sessionStorage.length ) ).toBe( 1 );
await page.reload();
// FIXME: Occasionally, upon reload, there is no server-provided
// autosave value available, despite our having previously explicitly
// autosaved. The reasons for this are still unknown. Since this is
// unrelated to *local* autosave, until we can understand them, we'll
// drop this test's expectations if we don't have an autosave object
// available.
const stillHasRemoteAutosave = await page.evaluate( () => window.wp.data.select( 'core/editor' ).getEditorSettings().autosave );
if ( ! stillHasRemoteAutosave ) {
return;
}
// Only one autosave notice should be displayed.
const notices = await page.$$( '.components-notice' );
expect( notices.length ).toBe( 1 );
const notice = await page.$eval( '.components-notice__content', ( element ) => element.innerText );
expect( notice ).toContain( AUTOSAVE_NOTICE_REMOTE );
} );
afterEach( async () => {
toggleOfflineMode( false );
await clearSessionStorage();
} );
} );