@wordpress/e2e-tests 7.23.0 → 7.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 7.24.0 (2024-03-06)
6
+
5
7
  ## 7.23.0 (2024-02-21)
6
8
 
7
9
  ## 7.22.0 (2024-02-09)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/e2e-tests",
3
- "version": "7.23.0",
3
+ "version": "7.24.0",
4
4
  "description": "End-To-End (E2E) tests for WordPress.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -23,13 +23,13 @@
23
23
  "node": ">=14"
24
24
  },
25
25
  "dependencies": {
26
- "@wordpress/e2e-test-utils": "^10.23.0",
27
- "@wordpress/interactivity": "^5.1.0",
28
- "@wordpress/interactivity-router": "^1.2.0",
29
- "@wordpress/jest-console": "^7.23.0",
30
- "@wordpress/jest-puppeteer-axe": "^6.23.0",
31
- "@wordpress/scripts": "^27.3.0",
32
- "@wordpress/url": "^3.53.0",
26
+ "@wordpress/e2e-test-utils": "^10.24.0",
27
+ "@wordpress/interactivity": "^5.2.0",
28
+ "@wordpress/interactivity-router": "^1.3.0",
29
+ "@wordpress/jest-console": "^7.24.0",
30
+ "@wordpress/jest-puppeteer-axe": "^6.24.0",
31
+ "@wordpress/scripts": "^27.4.0",
32
+ "@wordpress/url": "^3.54.0",
33
33
  "chalk": "^4.0.0",
34
34
  "expect-puppeteer": "^4.4.0",
35
35
  "filenamify": "^4.2.0",
@@ -46,5 +46,5 @@
46
46
  "publishConfig": {
47
47
  "access": "public"
48
48
  },
49
- "gitHead": "c139588f4c668b38bafbc5431f2f4e3903dbe683"
49
+ "gitHead": "ac3c3e465a083081a86a4da6ee6fb817b41e5130"
50
50
  }
@@ -21,7 +21,6 @@ function gutenberg_test_block_bindings_register_custom_fields() {
21
21
  'default' => 'Value of the text_custom_field',
22
22
  )
23
23
  );
24
- // TODO: Change url.
25
24
  register_meta(
26
25
  'post',
27
26
  'url_custom_field',
@@ -32,5 +31,24 @@ function gutenberg_test_block_bindings_register_custom_fields() {
32
31
  'default' => '#url-custom-field',
33
32
  )
34
33
  );
34
+ register_meta(
35
+ 'post',
36
+ '_protected_field',
37
+ array(
38
+ 'type' => 'string',
39
+ 'single' => true,
40
+ 'default' => 'protected field value',
41
+ )
42
+ );
43
+ register_meta(
44
+ 'post',
45
+ 'show_in_rest_false_field',
46
+ array(
47
+ 'show_in_rest' => false,
48
+ 'type' => 'string',
49
+ 'single' => true,
50
+ 'default' => 'show_in_rest false field value',
51
+ )
52
+ );
35
53
  }
36
54
  add_action( 'init', 'gutenberg_test_block_bindings_register_custom_fields' );
@@ -18,6 +18,7 @@ wp_enqueue_script_module( 'directive-context-view' );
18
18
  >
19
19
  <!-- rendered during hydration -->
20
20
  </pre>
21
+ <button data-testid="parent replace" data-wp-on--click="actions.replaceObj">Replace obj</button>
21
22
  <button
22
23
  data-testid="parent prop1"
23
24
  name="prop1"
@@ -50,6 +51,14 @@ wp_enqueue_script_module( 'directive-context-view' );
50
51
  >
51
52
  obj.prop5
52
53
  </button>
54
+ <button
55
+ data-testid="parent new"
56
+ name="new"
57
+ value="modifiedFromParent"
58
+ data-wp-on--click="actions.updateContext"
59
+ >
60
+ new
61
+ </button>
53
62
  <div
54
63
  data-wp-context='{ "prop2":"child","prop3":"child","obj":{"prop5":"child","prop6":"child"},"array":[4,5,6] }'
55
64
  >
@@ -59,6 +68,7 @@ wp_enqueue_script_module( 'directive-context-view' );
59
68
  >
60
69
  <!-- rendered during hydration -->
61
70
  </pre>
71
+ <button data-testid="child replace" data-wp-on--click="actions.replaceObj">Replace obj</button>
62
72
  <button
63
73
  data-testid="child prop1"
64
74
  name="prop1"
@@ -107,6 +117,24 @@ wp_enqueue_script_module( 'directive-context-view' );
107
117
  >
108
118
  obj.prop6
109
119
  </button>
120
+ <button
121
+ data-testid="child copy obj"
122
+ data-wp-on--click="actions.copyObj"
123
+ >
124
+ Copy obj
125
+ </button>
126
+ <div>
127
+ Is proxy preserved? <span
128
+ data-testid="is proxy preserved"
129
+ data-wp-text="state.isProxyPreserved"
130
+ ></span>
131
+ </div>
132
+ <div>
133
+ Is proxy preserved on copy? <span
134
+ data-testid="is proxy preserved on copy"
135
+ data-wp-text="state.isProxyPreservedOnCopy"
136
+ ></span>
137
+ </div>
110
138
  </div>
111
139
  <br />
112
140
 
@@ -127,10 +155,15 @@ wp_enqueue_script_module( 'directive-context-view' );
127
155
  data-wp-router-region="navigation"
128
156
  data-wp-context='{ "text": "first page" }'
129
157
  >
158
+ <div data-wp-context='{}'>
159
+ <div data-testid="navigation inherited text" data-wp-text="context.text"></div>
160
+ <div data-testid="navigation inherited text2" data-wp-text="context.text2"></div>
161
+ </div>
130
162
  <div data-testid="navigation text" data-wp-text="context.text"></div>
131
163
  <div data-testid="navigation new text" data-wp-text="context.newText"></div>
132
164
  <button data-testid="toggle text" data-wp-on--click="actions.toggleText">Toggle Text</button>
133
165
  <button data-testid="add new text" data-wp-on--click="actions.addNewText">Add New Text</button>
166
+ <button data-testid="add text2" data-wp-on--click="actions.addText2">Add Text 2</button>
134
167
  <button data-testid="navigate" data-wp-on--click="actions.navigate">Navigate</button>
135
168
  <button data-testid="async navigate" data-wp-on--click="actions.asyncNavigate">Async Navigate</button>
136
169
  </div>
@@ -143,3 +176,41 @@ wp_enqueue_script_module( 'directive-context-view' );
143
176
  <span data-testid="non-default suffix context" data-wp-text="context.text"></span>
144
177
  <span data-testid="default suffix context" data-wp-text="context.defaultText"></span>
145
178
  </div>
179
+
180
+ <div
181
+ data-wp-interactive='directive-context'
182
+ data-wp-context='{ "list": [
183
+ { "id": 1, "text": "Text 1" },
184
+ { "id": 2, "text": "Text 2" }
185
+ ] }'
186
+ >
187
+ <button data-testid="select 1" data-wp-on--click="actions.selectItem" value=1>Select 1</button>
188
+ <button data-testid="select 2" data-wp-on--click="actions.selectItem" value=2>Select 2</button>
189
+ <div data-testid="selected" data-wp-text="state.selected"></div>
190
+ </div>
191
+
192
+ <div
193
+ data-wp-interactive="directive-context-watch"
194
+ data-wp-context='{"counter":0}'
195
+ >
196
+ <button
197
+ data-testid="counter parent"
198
+ data-wp-on--click="actions.increment"
199
+ data-wp-text="context.counter"
200
+ ></button>
201
+ <div
202
+ data-wp-context='{"counter":0, "changes":0}'
203
+ data-wp-watch="callbacks.countChanges"
204
+ >
205
+ <button
206
+ data-testid="counter child"
207
+ data-wp-on--click="actions.increment"
208
+ data-wp-text="context.counter"
209
+ >
210
+ </button>
211
+ <span
212
+ data-testid="counter changes"
213
+ data-wp-text="context.changes"
214
+ ></span>
215
+ </div>
216
+ </div>
@@ -9,6 +9,19 @@ store( 'directive-context', {
9
9
  const ctx = getContext();
10
10
  return JSON.stringify( ctx, undefined, 2 );
11
11
  },
12
+ get selected() {
13
+ const { list, selected } = getContext();
14
+ return list.find( ( obj ) => obj === selected )?.text;
15
+ },
16
+ get isProxyPreserved() {
17
+ const ctx = getContext();
18
+ const pointer = ctx.obj;
19
+ return pointer === ctx.obj;
20
+ },
21
+ get isProxyPreservedOnCopy() {
22
+ const { obj, obj2 } = getContext();
23
+ return obj === obj2;
24
+ }
12
25
  },
13
26
  actions: {
14
27
  updateContext( event ) {
@@ -22,6 +35,19 @@ store( 'directive-context', {
22
35
  const ctx = getContext();
23
36
  ctx.text = ctx.text === 'Text 1' ? 'Text 2' : 'Text 1';
24
37
  },
38
+ selectItem( event ) {
39
+ const ctx = getContext();
40
+ const value = parseInt( event.target.value );
41
+ ctx.selected = ctx.list.find( ( { id } ) => id === value );
42
+ },
43
+ replaceObj() {
44
+ const ctx = getContext();
45
+ ctx.obj = { overwritten: true };
46
+ },
47
+ copyObj() {
48
+ const ctx = getContext();
49
+ ctx.obj2 = ctx.obj;
50
+ }
25
51
  },
26
52
  } );
27
53
 
@@ -29,12 +55,17 @@ const html = `
29
55
  <div
30
56
  data-wp-interactive="directive-context-navigate"
31
57
  data-wp-router-region="navigation"
32
- data-wp-context='{ "text": "second page" }'
58
+ data-wp-context='{ "text": "second page", "text2": "second page" }'
33
59
  >
60
+ <div data-wp-context='{}'>
61
+ <div data-testid="navigation inherited text" data-wp-text="context.text"></div>
62
+ <div data-testid="navigation inherited text2" data-wp-text="context.text2"></div>
63
+ </div>
34
64
  <div data-testid="navigation text" data-wp-text="context.text"></div>
35
65
  <div data-testid="navigation new text" data-wp-text="context.newText"></div>
36
66
  <button data-testid="toggle text" data-wp-on--click="actions.toggleText">Toggle Text</button>
37
- <button data-testid="add new text" data-wp-on--click="actions.addNewText">Add new text</button>
67
+ <button data-testid="add new text" data-wp-on--click="actions.addNewText">Add New Text</button>
68
+ <button data-testid="add text2" data-wp-on--click="actions.addText2">Add Text 2</button>
38
69
  <button data-testid="navigate" data-wp-on--click="actions.navigate">Navigate</button>
39
70
  <button data-testid="async navigate" data-wp-on--click="actions.asyncNavigate">Async Navigate</button>
40
71
  </div>`;
@@ -49,13 +80,17 @@ const { actions } = store( 'directive-context-navigate', {
49
80
  const ctx = getContext();
50
81
  ctx.newText = 'some new text';
51
82
  },
83
+ addText2() {
84
+ const ctx = getContext();
85
+ ctx.text2 = 'some new text';
86
+ },
52
87
  navigate() {
53
88
  return import( '@wordpress/interactivity-router' ).then(
54
- ( { actions: routerActions } ) =>
55
- routerActions.navigate(
56
- window.location,
57
- { force: true, html },
58
- )
89
+ ( { actions: routerActions } ) => {
90
+ const url = new URL( window.location.href );
91
+ url.searchParams.set( 'next_page', 'true' );
92
+ return routerActions.navigate( url, { force: true, html } );
93
+ }
59
94
  );
60
95
 
61
96
  },
@@ -66,3 +101,21 @@ const { actions } = store( 'directive-context-navigate', {
66
101
  },
67
102
  },
68
103
  } );
104
+
105
+ store( 'directive-context-watch', {
106
+ actions: {
107
+ increment: () => {
108
+ const ctx = getContext();
109
+ ctx.counter = ctx.counter + 1;
110
+ },
111
+ },
112
+ callbacks: {
113
+ countChanges: () => {
114
+ const ctx = getContext();
115
+ // Subscribe to changes in counter.
116
+ // eslint-disable-next-line no-unused-expressions
117
+ ctx.counter;
118
+ ctx.changes = ctx.changes + 1;
119
+ },
120
+ },
121
+ });
@@ -15,6 +15,13 @@ if ( $attributes['disableNavigation'] ) {
15
15
  array( 'clientNavigationDisabled' => true )
16
16
  );
17
17
  }
18
+
19
+ if ( isset( $attributes['data'] ) ) {
20
+ wp_interactivity_state(
21
+ 'router',
22
+ array( 'data' => $attributes['data'] )
23
+ );
24
+ }
18
25
  ?>
19
26
 
20
27
  <div
@@ -24,8 +31,12 @@ if ( $attributes['disableNavigation'] ) {
24
31
  <h2 data-testid="title"><?php echo $attributes['title']; ?></h2>
25
32
 
26
33
  <output
27
- data-testid="router navigations"
28
- data-wp-text="state.navigations"
34
+ data-testid="router navigations pending"
35
+ data-wp-text="state.navigations.pending"
36
+ >NaN</output>
37
+ <output
38
+ data-testid="router navigations count"
39
+ data-wp-text="state.navigations.count"
29
40
  >NaN</output>
30
41
  <output
31
42
  data-testid="router status"
@@ -39,24 +50,30 @@ if ( $attributes['disableNavigation'] ) {
39
50
  Timeout <span data-wp-text="state.timeout">NaN</span>
40
51
  </button>
41
52
 
42
- <?php
43
- if ( isset( $attributes['links'] ) ) {
44
- foreach ( $attributes['links'] as $key => $link ) {
45
- $i = $key += 1;
46
- echo <<<HTML
47
- <a
48
- data-testid="link $i"
49
- data-wp-on--click="actions.navigate"
50
- href="$link"
51
- >link $i</a>
52
- <a
53
- data-testid="link $i with hash"
54
- data-wp-on--click="actions.navigate"
55
- data-force-navigation="true"
56
- href="$link#link-$i-with-hash"
57
- >link $i with hash</a>
53
+ <nav>
54
+ <?php
55
+ if ( isset( $attributes['links'] ) ) {
56
+ foreach ( $attributes['links'] as $key => $link ) {
57
+ $i = $key += 1;
58
+ echo <<<HTML
59
+ <a
60
+ data-testid="link $i"
61
+ data-wp-on--click="actions.navigate"
62
+ href="$link"
63
+ >link $i</a>
64
+ <a
65
+ data-testid="link $i with hash"
66
+ data-wp-on--click="actions.navigate"
67
+ data-force-navigation="true"
68
+ href="$link#link-$i-with-hash"
69
+ >link $i with hash</a>
58
70
  HTML;
71
+ }
59
72
  }
60
- }
61
- ?>
73
+ ?>
74
+ </nav>
75
+ <div data-testid="getterProp" data-wp-text="state.data.getterProp"></div>
76
+ <div data-testid="prop1" data-wp-text="state.data.prop1"></div>
77
+ <div data-testid="prop2" data-wp-text="state.data.prop2"></div>
78
+ <div data-testid="prop3" data-wp-text="state.data.prop3"></div>
62
79
  </div>
@@ -6,14 +6,23 @@ import { store } from '@wordpress/interactivity';
6
6
  const { state } = store( 'router', {
7
7
  state: {
8
8
  status: 'idle',
9
- navigations: 0,
9
+ navigations: {
10
+ pending: 0,
11
+ count: 0,
12
+ },
10
13
  timeout: 10000,
14
+ data: {
15
+ get getterProp() {
16
+ return `value from getter (${ state.data.prop1 })`;
17
+ }
18
+ }
11
19
  },
12
20
  actions: {
13
21
  *navigate( e ) {
14
22
  e.preventDefault();
15
23
 
16
- state.navigations += 1;
24
+ state.navigations.count += 1;
25
+ state.navigations.pending += 1;
17
26
  state.status = 'busy';
18
27
 
19
28
  const force = e.target.dataset.forceNavigation === 'true';
@@ -24,9 +33,9 @@ const { state } = store( 'router', {
24
33
  );
25
34
  yield actions.navigate( e.target.href, { force, timeout } );
26
35
 
27
- state.navigations -= 1;
36
+ state.navigations.pending -= 1;
28
37
 
29
- if ( state.navigations === 0 ) {
38
+ if ( state.navigations.pending === 0 ) {
30
39
  state.status = 'idle';
31
40
  }
32
41
  },
@@ -1,346 +0,0 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import {
5
- clickBlockAppender,
6
- createNewPost,
7
- getEditedPostContent,
8
- pressKeyWithModifier,
9
- publishPost,
10
- saveDraft,
11
- toggleOfflineMode,
12
- canvas,
13
- } from '@wordpress/e2e-test-utils';
14
-
15
- // Constant to override editor preference
16
- const AUTOSAVE_INTERVAL_SECONDS = 5;
17
-
18
- const AUTOSAVE_NOTICE_REMOTE =
19
- 'There is an autosave of this post that is more recent than the version below.';
20
- const AUTOSAVE_NOTICE_LOCAL =
21
- 'The backup of this post in your browser is different from the version below.';
22
-
23
- // Save and wait for "Saved" to confirm save complete. Preserves focus in the
24
- // editing area.
25
- async function saveDraftWithKeyboard() {
26
- await page.waitForSelector( '.editor-post-save-draft' );
27
- await Promise.all( [
28
- page.waitForSelector( '.editor-post-saved-state.is-saved' ),
29
- pressKeyWithModifier( 'primary', 'S' ),
30
- ] );
31
- }
32
-
33
- async function sleep( durationInSeconds ) {
34
- // Rule `no-restricted-syntax` recommends `waitForSelector` against
35
- // `waitFor`, which isn't apt for the use case, when provided an integer,
36
- // of waiting for a given amount of time.
37
- // eslint-disable-next-line no-restricted-syntax
38
- await page.waitForTimeout( durationInSeconds * 1000 );
39
- }
40
-
41
- async function clearSessionStorage() {
42
- await page.evaluate( () => window.sessionStorage.clear() );
43
- }
44
-
45
- async function readSessionStorageAutosave( postId ) {
46
- return page.evaluate(
47
- ( key ) => window.sessionStorage.getItem( key ),
48
- `wp-autosave-block-editor-post-${ postId ? postId : 'auto-draft' }`
49
- );
50
- }
51
-
52
- async function getCurrentPostId() {
53
- return page.evaluate( () =>
54
- window.wp.data.select( 'core/editor' ).getCurrentPostId()
55
- );
56
- }
57
-
58
- async function setLocalAutosaveInterval( value ) {
59
- return page.evaluate( ( _value ) => {
60
- window.wp.data.dispatch( 'core/editor' ).updateEditorSettings( {
61
- localAutosaveInterval: _value,
62
- } );
63
- }, value );
64
- }
65
-
66
- function wrapParagraph( text ) {
67
- return `<!-- wp:paragraph -->
68
- <p>${ text }</p>
69
- <!-- /wp:paragraph -->`;
70
- }
71
-
72
- describe( 'autosave', () => {
73
- beforeEach( async () => {
74
- await clearSessionStorage();
75
- await createNewPost();
76
- await setLocalAutosaveInterval( AUTOSAVE_INTERVAL_SECONDS );
77
- } );
78
-
79
- it( 'should save to sessionStorage', async () => {
80
- // Wait for the original timeout to kick in, it will schedule
81
- // another run using the updated interval length of AUTOSAVE_INTERVAL_SECONDS.
82
- await sleep( 15 );
83
-
84
- await clickBlockAppender();
85
- await page.keyboard.type( 'before save' );
86
- await saveDraftWithKeyboard();
87
- await sleep( 1 );
88
- await page.keyboard.type( ' after save' );
89
-
90
- // Wait long enough for local autosave to kick in.
91
- await sleep( AUTOSAVE_INTERVAL_SECONDS + 1 );
92
-
93
- const id = await getCurrentPostId();
94
- const autosave = await readSessionStorageAutosave( id );
95
- const { content } = JSON.parse( autosave );
96
- expect( content ).toBe( wrapParagraph( 'before save after save' ) );
97
- } );
98
-
99
- it( 'should recover from sessionStorage', async () => {
100
- await clickBlockAppender();
101
- await page.keyboard.type( 'before save' );
102
- await saveDraftWithKeyboard();
103
- await page.keyboard.type( ' after save' );
104
-
105
- // Trigger local autosave.
106
- await page.evaluate( () =>
107
- window.wp.data.dispatch( 'core/editor' ).autosave( { local: true } )
108
- );
109
- // Reload without saving on the server.
110
- await page.reload();
111
- await page.waitForSelector( '.edit-post-layout' );
112
-
113
- const notice = await page.$eval(
114
- '.components-notice__content',
115
- ( element ) => element.innerText
116
- );
117
- expect( notice ).toContain( AUTOSAVE_NOTICE_LOCAL );
118
-
119
- expect( await getEditedPostContent() ).toEqual(
120
- wrapParagraph( 'before save' )
121
- );
122
- await page.click( '.components-notice__action' );
123
- expect( await getEditedPostContent() ).toEqual(
124
- wrapParagraph( 'before save after save' )
125
- );
126
- } );
127
-
128
- it( "shouldn't contaminate other posts", async () => {
129
- await clickBlockAppender();
130
- await page.keyboard.type( 'before save' );
131
- await saveDraft();
132
-
133
- // Fake local autosave.
134
- await page.evaluate(
135
- ( postId ) =>
136
- window.sessionStorage.setItem(
137
- `wp-autosave-block-editor-post-${ postId }`,
138
- JSON.stringify( {
139
- post_title: 'A',
140
- content: 'B',
141
- excerpt: 'C',
142
- } )
143
- ),
144
- await getCurrentPostId()
145
- );
146
- expect(
147
- await page.evaluate( () => window.sessionStorage.length )
148
- ).toBe( 1 );
149
-
150
- await page.reload();
151
- await page.waitForSelector( '.edit-post-layout' );
152
- const notice = await page.$eval(
153
- '.components-notice__content',
154
- ( element ) => element.innerText
155
- );
156
- expect( notice ).toContain(
157
- 'The backup of this post in your browser is different from the version below.'
158
- );
159
-
160
- await createNewPost();
161
- expect( await page.$( '.components-notice__content' ) ).toBe( null );
162
- } );
163
-
164
- it( 'should clear local autosave after successful remote autosave', async () => {
165
- // Edit, save draft, edit again.
166
- await clickBlockAppender();
167
- await page.keyboard.type( 'before save' );
168
- await saveDraftWithKeyboard();
169
- await page.keyboard.type( ' after save' );
170
-
171
- // Trigger local autosave.
172
- await page.evaluate( () =>
173
- window.wp.data.dispatch( 'core/editor' ).autosave( { local: true } )
174
- );
175
- expect(
176
- await page.evaluate( () => window.sessionStorage.length )
177
- ).toBeGreaterThanOrEqual( 1 );
178
-
179
- // Trigger remote autosave.
180
- await page.evaluate( () =>
181
- window.wp.data.dispatch( 'core/editor' ).autosave()
182
- );
183
- expect(
184
- await page.evaluate( () => window.sessionStorage.length )
185
- ).toBe( 0 );
186
- } );
187
-
188
- it( "shouldn't clear local autosave if remote autosave fails", async () => {
189
- // Edit, save draft, edit again.
190
- await clickBlockAppender();
191
- await page.keyboard.type( 'before save' );
192
- await saveDraftWithKeyboard();
193
- await page.keyboard.type( ' after save' );
194
-
195
- // Trigger local autosave.
196
- await page.evaluate( () =>
197
- window.wp.data.dispatch( 'core/editor' ).autosave( { local: true } )
198
- );
199
- expect(
200
- await page.evaluate( () => window.sessionStorage.length )
201
- ).toBe( 1 );
202
-
203
- // Bring network down and attempt to autosave remotely.
204
- toggleOfflineMode( true );
205
- await page.evaluate( () =>
206
- window.wp.data.dispatch( 'core/editor' ).autosave()
207
- );
208
- expect(
209
- await page.evaluate( () => window.sessionStorage.length )
210
- ).toBe( 1 );
211
- } );
212
-
213
- it( 'should clear local autosave after successful save', async () => {
214
- // Edit, save draft, edit again.
215
- await clickBlockAppender();
216
- await page.keyboard.type( 'before save' );
217
- await saveDraftWithKeyboard();
218
- await page.keyboard.type( ' after save' );
219
-
220
- // Trigger local autosave.
221
- await page.evaluate( () =>
222
- window.wp.data.dispatch( 'core/editor' ).autosave( { local: true } )
223
- );
224
- expect(
225
- await page.evaluate( () => window.sessionStorage.length )
226
- ).toBe( 1 );
227
-
228
- await saveDraftWithKeyboard();
229
- expect(
230
- await page.evaluate( () => window.sessionStorage.length )
231
- ).toBe( 0 );
232
- } );
233
-
234
- it( "shouldn't clear local autosave if save fails", async () => {
235
- // Edit, save draft, edit again.
236
- await clickBlockAppender();
237
- await page.keyboard.type( 'before save' );
238
- await saveDraftWithKeyboard();
239
- await page.keyboard.type( ' after save' );
240
-
241
- // Trigger local autosave.
242
- await page.evaluate( () =>
243
- window.wp.data.dispatch( 'core/editor' ).autosave( { local: true } )
244
- );
245
- expect(
246
- await page.evaluate( () => window.sessionStorage.length )
247
- ).toBeGreaterThanOrEqual( 1 );
248
-
249
- // Bring network down and attempt to save.
250
- toggleOfflineMode( true );
251
- saveDraftWithKeyboard();
252
- expect(
253
- await page.evaluate( () => window.sessionStorage.length )
254
- ).toBeGreaterThanOrEqual( 1 );
255
- } );
256
-
257
- it( "shouldn't conflict with server-side autosave", async () => {
258
- await clickBlockAppender();
259
- await page.keyboard.type( 'before publish' );
260
- await publishPost();
261
-
262
- await canvas().click( '[data-type="core/paragraph"]' );
263
- await page.keyboard.type( ' after publish' );
264
-
265
- // Trigger remote autosave.
266
- await page.evaluate( () =>
267
- window.wp.data.dispatch( 'core/editor' ).autosave()
268
- );
269
-
270
- // Force conflicting local autosave.
271
- await page.evaluate( () =>
272
- window.wp.data.dispatch( 'core/editor' ).autosave( { local: true } )
273
- );
274
- expect(
275
- await page.evaluate( () => window.sessionStorage.length )
276
- ).toBeGreaterThanOrEqual( 1 );
277
-
278
- await page.reload();
279
- await page.waitForSelector( '.edit-post-layout' );
280
-
281
- // FIXME: Occasionally, upon reload, there is no server-provided
282
- // autosave value available, despite our having previously explicitly
283
- // autosaved. The reasons for this are still unknown. Since this is
284
- // unrelated to *local* autosave, until we can understand them, we'll
285
- // drop this test's expectations if we don't have an autosave object
286
- // available.
287
- const stillHasRemoteAutosave = await page.evaluate(
288
- () =>
289
- window.wp.data.select( 'core/editor' ).getEditorSettings()
290
- .autosave
291
- );
292
- if ( ! stillHasRemoteAutosave ) {
293
- return;
294
- }
295
-
296
- // Only one autosave notice should be displayed.
297
- const notices = await page.$$( '.components-notice' );
298
- expect( notices.length ).toBe( 1 );
299
- const notice = await page.$eval(
300
- '.components-notice__content',
301
- ( element ) => element.innerText
302
- );
303
- expect( notice ).toContain( AUTOSAVE_NOTICE_REMOTE );
304
- } );
305
-
306
- it( 'should clear sessionStorage upon user logout', async () => {
307
- await clickBlockAppender();
308
- await page.keyboard.type( 'before save' );
309
- await saveDraft();
310
-
311
- // Fake local autosave.
312
- await page.evaluate(
313
- ( postId ) =>
314
- window.sessionStorage.setItem(
315
- `wp-autosave-block-editor-post-${ postId }`,
316
- JSON.stringify( {
317
- post_title: 'A',
318
- content: 'B',
319
- excerpt: 'C',
320
- } )
321
- ),
322
- await getCurrentPostId()
323
- );
324
- expect(
325
- await page.evaluate( () => window.sessionStorage.length )
326
- ).toBe( 1 );
327
-
328
- await Promise.all( [
329
- page.waitForSelector( '#wp-admin-bar-logout', { visible: true } ),
330
- page.hover( '#wp-admin-bar-my-account' ),
331
- ] );
332
- await Promise.all( [
333
- page.waitForNavigation(),
334
- page.click( '#wp-admin-bar-logout' ),
335
- ] );
336
-
337
- expect(
338
- await page.evaluate( () => window.sessionStorage.length )
339
- ).toBe( 0 );
340
- } );
341
-
342
- afterEach( async () => {
343
- toggleOfflineMode( false );
344
- await clearSessionStorage();
345
- } );
346
- } );