@wordpress/preferences-persistence 2.54.1-next.v.202609031004.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 +4 -4
- package/src/create/test/index.jsdom.test.js +33 -39
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,13 +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
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"vitest": "^
|
|
49
|
+
"vitest": "^5.0.0"
|
|
50
50
|
},
|
|
51
51
|
"publishConfig": {
|
|
52
52
|
"access": "public"
|
|
53
53
|
},
|
|
54
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "485f42ae8a1c58ceea18371a507fd4acfa86fbd8"
|
|
55
55
|
}
|
|
@@ -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( {} );
|