@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 +2 -0
- package/package.json +6 -3
- package/src/create/test/debounce-async.js +61 -68
- package/src/create/test/{index.js → index.jsdom.test.js} +33 -39
- package/src/migrations/legacy-local-storage-data/test/convert-edit-post-panels.js +1 -0
- package/src/migrations/legacy-local-storage-data/test/index.js +1 -0
- package/src/migrations/legacy-local-storage-data/test/move-feature-preferences.js +1 -0
- package/src/migrations/legacy-local-storage-data/test/move-individual-preference.js +1 -0
- package/src/migrations/legacy-local-storage-data/test/move-interface-enable-items.js +1 -0
- package/src/migrations/legacy-local-storage-data/test/move-third-party-feature-preferences.js +1 -0
- package/src/migrations/preferences-package-data/test/convert-complementary-areas.js +1 -0
- package/src/migrations/preferences-package-data/test/convert-editor-settings.js +1 -0
- package/src/migrations/preferences-package-data/test/index.js +1 -0
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/preferences-persistence",
|
|
3
|
-
"version": "2.
|
|
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.
|
|
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": "
|
|
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
|
-
//
|
|
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(
|
|
7
|
+
return new Promise( setImmediateCallback );
|
|
9
8
|
}
|
|
10
9
|
|
|
11
|
-
// Promisify a timeout for use with
|
|
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 =
|
|
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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
29
|
+
debounced( 'A' );
|
|
32
30
|
|
|
33
|
-
|
|
34
|
-
debounced( 'C' );
|
|
35
|
-
debounced( 'D' );
|
|
31
|
+
expect( fn ).toHaveBeenCalledTimes( 1 );
|
|
36
32
|
|
|
37
|
-
|
|
38
|
-
|
|
33
|
+
debounced( 'B' );
|
|
34
|
+
debounced( 'C' );
|
|
35
|
+
debounced( 'D' );
|
|
39
36
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
expect( fn ).toHaveBeenCalledWith( 'D' );
|
|
37
|
+
await flushPromises();
|
|
38
|
+
vi.runAllTimers();
|
|
43
39
|
|
|
44
|
-
|
|
45
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
109
|
-
|
|
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
|
-
|
|
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
|
-
|
|
13
|
+
mockedApiFetch.mockReset();
|
|
14
|
+
vi.restoreAllMocks();
|
|
9
15
|
} );
|
|
10
16
|
|
|
11
17
|
describe( 'set', () => {
|
|
12
18
|
it( 'stores backup restoration data in localStorage', () => {
|
|
13
|
-
|
|
14
|
-
const spy =
|
|
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
|
-
|
|
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(
|
|
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 =
|
|
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(
|
|
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 =
|
|
72
|
-
|
|
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(
|
|
83
|
-
|
|
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(
|
|
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
|
-
|
|
97
|
+
mockedApiFetch.mockResolvedValueOnce( {
|
|
98
98
|
meta: { persisted_preferences: data },
|
|
99
99
|
} );
|
|
100
100
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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
|
-
|
|
114
|
+
mockedApiFetch.mockResolvedValueOnce( {
|
|
116
115
|
meta: { persisted_preferences: data },
|
|
117
116
|
} );
|
|
118
117
|
|
|
119
|
-
|
|
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
|
-
|
|
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
|
-
|
|
148
|
-
|
|
149
|
-
|
|
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
|
-
|
|
152
|
+
mockedApiFetch.mockResolvedValueOnce( {
|
|
158
153
|
meta: {
|
|
159
154
|
persisted_preferences: null,
|
|
160
155
|
},
|
|
161
156
|
} );
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
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( {} );
|