-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
change-detection.test.js
292 lines (217 loc) · 8.19 KB
/
change-detection.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
* Internal dependencies
*/
import {
clickBlockAppender,
newPost,
pressWithModifier,
ensureSidebarOpened,
publishPost,
META_KEY,
} from '../support/utils';
describe( 'Change detection', () => {
let handleInterceptedRequest, hadInterceptedSave;
beforeEach( async () => {
hadInterceptedSave = false;
await newPost();
} );
afterEach( () => {
if ( handleInterceptedRequest ) {
releaseSaveIntercept();
}
} );
async function assertIsDirty( isDirty ) {
let hadDialog = false;
function handleOnDialog() {
hadDialog = true;
}
try {
page.on( 'dialog', handleOnDialog );
await page.reload();
// Ensure whether it was expected that dialog was encountered.
expect( hadDialog ).toBe( isDirty );
} catch ( error ) {
throw error;
} finally {
page.removeListener( 'dialog', handleOnDialog );
}
}
async function interceptSave() {
await page.setRequestInterception( true );
handleInterceptedRequest = ( interceptedRequest ) => {
if ( interceptedRequest.url().includes( '/wp/v2/posts' ) ) {
hadInterceptedSave = true;
} else {
interceptedRequest.continue();
}
};
page.on( 'request', handleInterceptedRequest );
}
async function releaseSaveIntercept() {
page.removeListener( 'request', handleInterceptedRequest );
await page.setRequestInterception( false );
hadInterceptedSave = false;
handleInterceptedRequest = null;
}
it( 'Should not save on new unsaved post', async () => {
await interceptSave();
// Keyboard shortcut Ctrl+S save.
await pressWithModifier( META_KEY, 'S' );
expect( hadInterceptedSave ).toBe( false );
} );
it( 'Should autosave post', async () => {
await page.type( '.editor-post-title__input', 'Hello World' );
// Force autosave to occur immediately.
await Promise.all( [
page.evaluate( () => window.wp.data.dispatch( 'core/editor' ).autosave() ),
page.waitForSelector( '.editor-post-saved-state.is-autosaving' ),
page.waitForSelector( '.editor-post-saved-state.is-saved' ),
] );
// Autosave draft as same user should do full save, i.e. not dirty.
await assertIsDirty( false );
} );
it( 'Should prompt to confirm unsaved changes for autosaved draft for non-content fields', async () => {
await page.type( '.editor-post-title__input', 'Hello World' );
// Toggle post as needing review (not persisted for autosave).
await ensureSidebarOpened();
const postPendingReviewButton = ( await page.$x( "//label[contains(text(), 'Pending Review')]" ) )[ 0 ];
await postPendingReviewButton.click( 'button' );
// Force autosave to occur immediately.
await Promise.all( [
page.evaluate( () => window.wp.data.dispatch( 'core/editor' ).autosave() ),
page.waitForSelector( '.editor-post-saved-state.is-autosaving' ),
page.waitForSelector( '.editor-post-saved-state.is-saved' ),
] );
await assertIsDirty( true );
} );
it( 'Should prompt to confirm unsaved changes for autosaved published post', async () => {
await page.type( '.editor-post-title__input', 'Hello World' );
await publishPost();
// Close publish panel.
await Promise.all( [
page.waitForFunction( () => ! document.querySelector( '.editor-post-publish-panel' ) ),
page.click( '.editor-post-publish-panel__header button' ),
] );
// Should be dirty after autosave change of published post.
await page.type( '.editor-post-title__input', '!' );
await Promise.all( [
page.waitForSelector( '.editor-post-publish-button.is-busy' ),
page.waitForSelector( '.editor-post-publish-button:not( .is-busy )' ),
page.evaluate( () => window.wp.data.dispatch( 'core/editor' ).autosave() ),
] );
await assertIsDirty( true );
} );
it( 'Should not prompt to confirm unsaved changes for new post', async () => {
await assertIsDirty( false );
} );
it( 'Should not prompt to confirm unsaved changes for new post with initial edits', async () => {
await newPost( {
title: 'My New Post',
content: 'My content',
excerpt: 'My excerpt',
} );
await assertIsDirty( false );
} );
it( 'Should prompt if property changed without save', async () => {
await page.type( '.editor-post-title__input', 'Hello World' );
await assertIsDirty( true );
} );
it( 'Should prompt if content added without save', async () => {
await clickBlockAppender();
await assertIsDirty( true );
} );
it( 'Should not prompt if changes saved', async () => {
await page.type( '.editor-post-title__input', 'Hello World' );
await Promise.all( [
// Wait for "Saved" to confirm save complete.
page.waitForSelector( '.editor-post-saved-state.is-saved' ),
// Keyboard shortcut Ctrl+S save.
pressWithModifier( META_KEY, 'S' ),
] );
await assertIsDirty( false );
} );
it( 'Should not save if all changes saved', async () => {
await page.type( '.editor-post-title__input', 'Hello World' );
await Promise.all( [
// Wait for "Saved" to confirm save complete.
page.waitForSelector( '.editor-post-saved-state.is-saved' ),
// Keyboard shortcut Ctrl+S save.
pressWithModifier( META_KEY, 'S' ),
] );
await interceptSave();
// Keyboard shortcut Ctrl+S save.
await pressWithModifier( META_KEY, 'S' );
expect( hadInterceptedSave ).toBe( false );
} );
it( 'Should prompt if save failed', async () => {
await page.type( '.editor-post-title__input', 'Hello World' );
await page.setOfflineMode( true );
await Promise.all( [
// Keyboard shortcut Ctrl+S save.
pressWithModifier( META_KEY, 'S' ),
// Ensure save update fails and presents button.
page.waitForXPath(
'//*[contains(@class, "components-notice") and contains(@class, "is-error")]/*[text()="Updating failed"]'
),
page.waitForSelector( '.editor-post-save-draft' ),
] );
// Need to disable offline to allow reload.
await page.setOfflineMode( false );
await assertIsDirty( true );
expect( console ).toHaveErroredWith( 'Failed to load resource: net::ERR_INTERNET_DISCONNECTED' );
} );
it( 'Should prompt if changes and save is in-flight', async () => {
await page.type( '.editor-post-title__input', 'Hello World' );
// Hold the posts request so we don't deal with race conditions of the
// save completing early. Other requests should be allowed to continue,
// for example the page reload test.
await interceptSave();
// Keyboard shortcut Ctrl+S save.
await pressWithModifier( META_KEY, 'S' );
await releaseSaveIntercept();
await assertIsDirty( true );
} );
it( 'Should prompt if changes made while save is in-flight', async () => {
await page.type( '.editor-post-title__input', 'Hello World' );
// Hold the posts request so we don't deal with race conditions of the
// save completing early. Other requests should be allowed to continue,
// for example the page reload test.
await interceptSave();
// Keyboard shortcut Ctrl+S save.
await pressWithModifier( META_KEY, 'S' );
await page.type( '.editor-post-title__input', '!' );
await releaseSaveIntercept();
await assertIsDirty( true );
} );
it( 'Should prompt if property changes made while save is in-flight, and save completes', async () => {
await page.type( '.editor-post-title__input', 'Hello World' );
// Hold the posts request so we don't deal with race conditions of the
// save completing early.
await interceptSave();
// Keyboard shortcut Ctrl+S save.
await pressWithModifier( META_KEY, 'S' );
// Dirty post while save is in-flight.
await page.type( '.editor-post-title__input', '!' );
// Allow save to complete. Disabling interception flushes pending.
await Promise.all( [
page.waitForSelector( '.editor-post-saved-state.is-saved' ),
releaseSaveIntercept(),
] );
await assertIsDirty( true );
} );
it( 'Should prompt if block revision is made while save is in-flight, and save completes', async () => {
await page.type( '.editor-post-title__input', 'Hello World' );
// Hold the posts request so we don't deal with race conditions of the
// save completing early.
await interceptSave();
// Keyboard shortcut Ctrl+S save.
await pressWithModifier( META_KEY, 'S' );
await clickBlockAppender();
// Allow save to complete. Disabling interception flushes pending.
await Promise.all( [
page.waitForSelector( '.editor-post-saved-state.is-saved' ),
releaseSaveIntercept(),
] );
await assertIsDirty( true );
} );
} );