@wordpress/preferences-persistence 2.54.1-next.v.202608281122.0 → 2.55.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
+ ## 2.55.0 (2026-09-10)
6
+
5
7
  ## 2.54.0 (2026-08-26)
6
8
 
7
9
  ## 2.53.0 (2026-08-12)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/preferences-persistence",
3
- "version": "2.54.1-next.v.202608281122.0",
3
+ "version": "2.55.0",
4
4
  "description": "Persistence utilities for `wordpress/preferences`.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -43,10 +43,13 @@
43
43
  "wpScript": true,
44
44
  "sideEffects": false,
45
45
  "dependencies": {
46
- "@wordpress/api-fetch": "^7.54.1-next.v.202608281122.0"
46
+ "@wordpress/api-fetch": "^7.55.0"
47
+ },
48
+ "devDependencies": {
49
+ "vitest": "^5.0.0"
47
50
  },
48
51
  "publishConfig": {
49
52
  "access": "public"
50
53
  },
51
- "gitHead": "de479d415171765f7b573c81aeec5334ff506a12"
54
+ "gitHead": "485f42ae8a1c58ceea18371a507fd4acfa86fbd8"
52
55
  }
@@ -1,84 +1,87 @@
1
+ import { setImmediate as setImmediateCallback } from 'node:timers';
2
+ import { describe, expect, it, vi } from 'vitest';
1
3
  import debounceAsync from '../debounce-async';
2
4
 
3
- // See https://stackoverflow.com/questions/52177631/jest-timer-and-promise-dont-work-well-settimeout-and-async-function.
4
- // Jest fake timers and async functions don't mix too well, since queued up
5
- // promises can prevent jest from calling timeouts.
6
- // This function flushes promises in the queue.
5
+ // Let promise callbacks settle without advancing the faked timeout clock.
7
6
  function flushPromises() {
8
- return new Promise( jest.requireActual( 'timers' ).setImmediate );
7
+ return new Promise( setImmediateCallback );
9
8
  }
10
9
 
11
- // Promisify a timeout for use with jest.fn.
10
+ // Promisify a timeout for use with vi.fn.
12
11
  function timeout( milliseconds ) {
13
12
  return new Promise( ( resolve ) => setTimeout( resolve, milliseconds ) );
14
13
  }
15
14
 
16
15
  describe( 'debounceAsync', () => {
17
16
  it( 'uses a leading debounce, the first call happens immediately', () => {
18
- const fn = jest.fn( async () => {} );
17
+ const fn = vi.fn( async () => {} );
19
18
  const debounced = debounceAsync( fn, 20 );
20
19
  debounced();
21
20
  expect( fn ).toHaveBeenCalledTimes( 1 );
22
21
  } );
23
22
 
24
23
  it( 'calls the function on the leading edge and then once on the trailing edge when there are multiple calls', async () => {
25
- jest.useFakeTimers();
26
- const fn = jest.fn( async () => {} );
27
- const debounced = debounceAsync( fn, 20 );
28
-
29
- debounced( 'A' );
24
+ vi.useFakeTimers( { toFake: [ 'setTimeout', 'clearTimeout' ] } );
25
+ try {
26
+ const fn = vi.fn( async () => {} );
27
+ const debounced = debounceAsync( fn, 20 );
30
28
 
31
- expect( fn ).toHaveBeenCalledTimes( 1 );
29
+ debounced( 'A' );
32
30
 
33
- debounced( 'B' );
34
- debounced( 'C' );
35
- debounced( 'D' );
31
+ expect( fn ).toHaveBeenCalledTimes( 1 );
36
32
 
37
- await flushPromises();
38
- jest.runAllTimers();
33
+ debounced( 'B' );
34
+ debounced( 'C' );
35
+ debounced( 'D' );
39
36
 
40
- expect( fn ).toHaveBeenCalledTimes( 2 );
41
- expect( fn ).toHaveBeenCalledWith( 'A' );
42
- expect( fn ).toHaveBeenCalledWith( 'D' );
37
+ await flushPromises();
38
+ vi.runAllTimers();
43
39
 
44
- jest.runOnlyPendingTimers();
45
- jest.useRealTimers();
40
+ expect( fn ).toHaveBeenCalledTimes( 2 );
41
+ expect( fn ).toHaveBeenCalledWith( 'A' );
42
+ expect( fn ).toHaveBeenCalledWith( 'D' );
43
+ } finally {
44
+ vi.clearAllTimers();
45
+ vi.useRealTimers();
46
+ }
46
47
  } );
47
48
 
48
49
  it( 'ensures the delay has elapsed between calls', async () => {
49
- jest.useFakeTimers();
50
- const fn = jest.fn( async () => timeout( 10 ) );
51
- const debounced = debounceAsync( fn, 20 );
52
-
53
- // The first call has been triggered, but will take 10ms to resolve.
54
- debounced();
55
- debounced();
56
- debounced();
57
- debounced();
58
- expect( fn ).toHaveBeenCalledTimes( 1 );
59
-
60
- // The first call has resolved. The delay period has started but has yet to finish.
61
- await flushPromises();
62
- jest.advanceTimersByTime( 11 );
63
- expect( fn ).toHaveBeenCalledTimes( 1 );
64
-
65
- // The second call is about to commence, but hasn't yet.
66
- await flushPromises();
67
- jest.advanceTimersByTime( 18 );
68
- expect( fn ).toHaveBeenCalledTimes( 1 );
69
-
70
- // The second call has now commenced.
71
- await flushPromises();
72
- jest.advanceTimersByTime( 2 );
73
- expect( fn ).toHaveBeenCalledTimes( 2 );
74
-
75
- // No more calls happen.
76
- await flushPromises();
77
- jest.runAllTimers();
78
- expect( fn ).toHaveBeenCalledTimes( 2 );
79
-
80
- jest.runOnlyPendingTimers();
81
- jest.useRealTimers();
50
+ vi.useFakeTimers( { toFake: [ 'setTimeout', 'clearTimeout' ] } );
51
+ try {
52
+ const fn = vi.fn( async () => timeout( 10 ) );
53
+ const debounced = debounceAsync( fn, 20 );
54
+
55
+ // The first call has been triggered, but will take 10ms to resolve.
56
+ debounced();
57
+ debounced();
58
+ debounced();
59
+ debounced();
60
+ expect( fn ).toHaveBeenCalledTimes( 1 );
61
+
62
+ // The first call has resolved. The delay period has started but has yet to finish.
63
+ await flushPromises();
64
+ vi.advanceTimersByTime( 11 );
65
+ expect( fn ).toHaveBeenCalledTimes( 1 );
66
+
67
+ // The second call is about to commence, but hasn't yet.
68
+ await flushPromises();
69
+ vi.advanceTimersByTime( 18 );
70
+ expect( fn ).toHaveBeenCalledTimes( 1 );
71
+
72
+ // The second call has now commenced.
73
+ await flushPromises();
74
+ vi.advanceTimersByTime( 2 );
75
+ expect( fn ).toHaveBeenCalledTimes( 2 );
76
+
77
+ // No more calls happen.
78
+ await flushPromises();
79
+ vi.runAllTimers();
80
+ expect( fn ).toHaveBeenCalledTimes( 2 );
81
+ } finally {
82
+ vi.clearAllTimers();
83
+ vi.useRealTimers();
84
+ }
82
85
  } );
83
86
 
84
87
  it( 'is thenable, returning any data from promise resolution of the debounced function', async () => {
@@ -105,21 +108,11 @@ describe( 'debounceAsync', () => {
105
108
 
106
109
  const debounced = debounceAsync( fn, 20 );
107
110
 
108
- // Test traditional try/catch.
109
- try {
110
- await debounced();
111
- } catch ( error ) {
112
- // Disable reason - the test uses `expect.assertions` to ensure
113
- // conditional assertions are called.
114
- // eslint-disable-next-line jest/no-conditional-expect
115
- expect( error ).toBe( expectedError );
116
- }
111
+ // Test awaiting a rejection.
112
+ await expect( debounced() ).rejects.toBe( expectedError );
117
113
 
118
114
  // Test chained .catch().
119
115
  await debounced().catch( ( error ) => {
120
- // Disable reason - the test uses `expect.assertions` to ensure
121
- // conditional assertions are called.
122
- // eslint-disable-next-line jest/no-conditional-expect
123
116
  expect( error ).toBe( expectedError );
124
117
  } );
125
118
  } );
@@ -1,17 +1,23 @@
1
+ import { afterEach, describe, expect, it, vi } from 'vitest';
1
2
  import apiFetch from '@wordpress/api-fetch';
2
3
  import create from '..';
3
4
 
4
- jest.mock( '@wordpress/api-fetch' );
5
+ vi.mock( import( '@wordpress/api-fetch' ), () => ( {
6
+ default: vi.fn(),
7
+ } ) );
8
+
9
+ const mockedApiFetch = vi.mocked( apiFetch );
5
10
 
6
11
  describe( 'create', () => {
7
12
  afterEach( () => {
8
- apiFetch.mockReset();
13
+ mockedApiFetch.mockReset();
14
+ vi.restoreAllMocks();
9
15
  } );
10
16
 
11
17
  describe( 'set', () => {
12
18
  it( 'stores backup restoration data in localStorage', () => {
13
- apiFetch.mockResolvedValueOnce();
14
- const spy = jest.spyOn( global.Storage.prototype, 'setItem' );
19
+ mockedApiFetch.mockResolvedValueOnce();
20
+ const spy = vi.spyOn( global.Storage.prototype, 'setItem' );
15
21
 
16
22
  const localStorageRestoreKey = 'test';
17
23
  const { set } = create( { localStorageRestoreKey } );
@@ -33,14 +39,14 @@ describe( 'create', () => {
33
39
  } );
34
40
 
35
41
  it( 'sends data to the `users/me` endpoint', () => {
36
- apiFetch.mockResolvedValueOnce();
42
+ mockedApiFetch.mockResolvedValueOnce();
37
43
 
38
44
  const { set } = create();
39
45
 
40
46
  const data = { test: 1 };
41
47
  set( data );
42
48
 
43
- expect( apiFetch ).toHaveBeenCalledWith( {
49
+ expect( mockedApiFetch ).toHaveBeenCalledWith( {
44
50
  path: '/wp/v2/users/me',
45
51
  method: 'PUT',
46
52
  keepalive: true,
@@ -55,38 +61,32 @@ describe( 'create', () => {
55
61
 
56
62
  describe( 'get', () => {
57
63
  it( 'avoids using the REST API or local storage when data is preloaded', async () => {
58
- const getItemSpy = jest.spyOn(
59
- global.Storage.prototype,
60
- 'getItem'
61
- );
64
+ const getItemSpy = vi.spyOn( global.Storage.prototype, 'getItem' );
62
65
 
63
66
  const preloadedData = { preloaded: true };
64
67
  const { get } = create( { preloadedData } );
65
68
  expect( await get() ).toBe( preloadedData );
66
69
  expect( getItemSpy ).not.toHaveBeenCalled();
67
- expect( apiFetch ).not.toHaveBeenCalled();
70
+ expect( mockedApiFetch ).not.toHaveBeenCalled();
68
71
  } );
69
72
 
70
73
  it( 'returns from a local cache once `set` has been called', async () => {
71
- const getItemSpy = jest.spyOn(
72
- global.Storage.prototype,
73
- 'getItem'
74
- );
75
- apiFetch.mockResolvedValueOnce();
74
+ const getItemSpy = vi.spyOn( global.Storage.prototype, 'getItem' );
75
+ mockedApiFetch.mockResolvedValueOnce();
76
76
 
77
77
  const data = { cached: true };
78
78
  const { get, set } = create();
79
79
 
80
80
  // apiFetch was called as a result of calling `set`.
81
81
  set( data );
82
- expect( apiFetch ).toHaveBeenCalled();
83
- apiFetch.mockClear();
82
+ expect( mockedApiFetch ).toHaveBeenCalled();
83
+ mockedApiFetch.mockClear();
84
84
 
85
85
  // Neither localStorage.getItem or apiFetch are called as a result
86
86
  // of the call to `get`. A local cache is used.
87
87
  expect( await get() ).toEqual( expect.objectContaining( data ) );
88
88
  expect( getItemSpy ).not.toHaveBeenCalled();
89
- expect( apiFetch ).not.toHaveBeenCalled();
89
+ expect( mockedApiFetch ).not.toHaveBeenCalled();
90
90
  } );
91
91
 
92
92
  it( 'returns data from the users/me endpoint if there is no data in localStorage', async () => {
@@ -94,14 +94,13 @@ describe( 'create', () => {
94
94
  __timestamp: 0,
95
95
  test: 2,
96
96
  };
97
- apiFetch.mockResolvedValueOnce( {
97
+ mockedApiFetch.mockResolvedValueOnce( {
98
98
  meta: { persisted_preferences: data },
99
99
  } );
100
100
 
101
- jest.spyOn(
102
- global.Storage.prototype,
103
- 'getItem'
104
- ).mockReturnValueOnce( 'null' );
101
+ vi.spyOn( global.Storage.prototype, 'getItem' ).mockReturnValueOnce(
102
+ 'null'
103
+ );
105
104
 
106
105
  const { get } = create();
107
106
  expect( await get() ).toEqual( data );
@@ -112,14 +111,11 @@ describe( 'create', () => {
112
111
  _modified: '2022-04-22T00:00:00.000Z',
113
112
  test: 'api',
114
113
  };
115
- apiFetch.mockResolvedValueOnce( {
114
+ mockedApiFetch.mockResolvedValueOnce( {
116
115
  meta: { persisted_preferences: data },
117
116
  } );
118
117
 
119
- jest.spyOn(
120
- global.Storage.prototype,
121
- 'getItem'
122
- ).mockReturnValueOnce(
118
+ vi.spyOn( global.Storage.prototype, 'getItem' ).mockReturnValueOnce(
123
119
  JSON.stringify( {
124
120
  _modified: '2022-04-21T00:00:00.000Z',
125
121
  test: 'localStorage',
@@ -131,7 +127,7 @@ describe( 'create', () => {
131
127
  } );
132
128
 
133
129
  it( 'returns data from localStorage if it has a more recent modified date than data from the REST API', async () => {
134
- apiFetch.mockResolvedValueOnce( {
130
+ mockedApiFetch.mockResolvedValueOnce( {
135
131
  meta: {
136
132
  persisted_preferences: {
137
133
  _modified: '2022-04-21T00:00:00.000Z',
@@ -144,25 +140,23 @@ describe( 'create', () => {
144
140
  _modified: '2022-04-22T00:00:00.000Z',
145
141
  test: 'localStorage',
146
142
  };
147
- jest.spyOn(
148
- global.Storage.prototype,
149
- 'getItem'
150
- ).mockReturnValueOnce( JSON.stringify( data ) );
143
+ vi.spyOn( global.Storage.prototype, 'getItem' ).mockReturnValueOnce(
144
+ JSON.stringify( data )
145
+ );
151
146
 
152
147
  const { get } = create();
153
148
  expect( await get() ).toEqual( data );
154
149
  } );
155
150
 
156
151
  it( 'returns an empty object if neither local storage or the REST API return any data', async () => {
157
- apiFetch.mockResolvedValueOnce( {
152
+ mockedApiFetch.mockResolvedValueOnce( {
158
153
  meta: {
159
154
  persisted_preferences: null,
160
155
  },
161
156
  } );
162
- jest.spyOn(
163
- global.Storage.prototype,
164
- 'getItem'
165
- ).mockReturnValueOnce( 'null' );
157
+ vi.spyOn( global.Storage.prototype, 'getItem' ).mockReturnValueOnce(
158
+ 'null'
159
+ );
166
160
 
167
161
  const { get } = create();
168
162
  expect( await get() ).toEqual( {} );
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest';
1
2
  import convertEditPostPanels from '../convert-edit-post-panels';
2
3
 
3
4
  describe( 'convertEditPostPanels', () => {
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest';
1
2
  import { convertLegacyData } from '..';
2
3
 
3
4
  const legacyData = {
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest';
1
2
  import moveFeaturePreferences from '../move-feature-preferences';
2
3
 
3
4
  describe( 'moveFeaturePreferences', () => {
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest';
1
2
  import moveIndividualPreference from '../move-individual-preference';
2
3
 
3
4
  describe( 'moveIndividualPreference', () => {
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest';
1
2
  import moveInterfaceEnableItems from '../move-interface-enable-items';
2
3
 
3
4
  describe( 'moveInterfaceEnableItems', () => {
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest';
1
2
  import moveThirdPartyFeaturePreferences from '../move-third-party-feature-preferences';
2
3
 
3
4
  describe( 'moveThirdPartyFeaturePreferences', () => {
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest';
1
2
  import convertComplementaryAreas from '../convert-complementary-areas';
2
3
 
3
4
  describe( 'convertComplementaryAreas', () => {
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest';
1
2
  import convertEditorSettings from '../convert-editor-settings';
2
3
 
3
4
  describe( 'convertEditorSettings', () => {
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest';
1
2
  import convertPreferencesPackageData from '../';
2
3
 
3
4
  const input = {