-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathapi-cache.test.js
110 lines (95 loc) · 3.14 KB
/
api-cache.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
/**
* API caching functions tests.
*
* Site Kit by Google, Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* WordPress dependencies
*/
import { visitAdminPage } from '@wordpress/e2e-test-utils';
/**
* Internal dependencies
*/
import {
resetSiteKit,
safeLoginUser,
setupSiteKit,
useRequestInterception,
} from '../utils';
import { deleteAuthCookie } from '../utils/delete-auth-cookie';
async function goToWordPressDashboard() {
await visitAdminPage( 'index.php' );
}
async function googleSiteKitAPIGetTime() {
await page.waitForFunction(
() =>
window.googlesitekit !== undefined &&
window.googlesitekit.api !== undefined
);
return await page.evaluate( () => {
// `useCache` is enabled by default for `get` calls,
// but we'll be explicit here for the sake of the test.
return window.googlesitekit.api.get( 'e2e', 'util', 'time', null, {
useCache: true,
} );
} );
}
describe( 'API cache', () => {
beforeAll( async () => {
await page.setRequestInterception( true );
useRequestInterception( ( request ) => {
const url = request.url();
if ( url.match( 'search-console/data/searchanalytics' ) ) {
request.respond( { status: 200, body: '[]' } );
} else {
request.continue();
}
} );
await setupSiteKit();
} );
afterAll( async () => {
await resetSiteKit();
} );
it( 'isolates client storage between sessions', async () => {
// Navigate to WP dashboard to use SK APIs
// while minimizing side-effects from reporting, etc.
await goToWordPressDashboard();
const initialTimeData = await googleSiteKitAPIGetTime();
expect( initialTimeData ).toMatchObject( {
microtime: expect.any( Number ),
} );
// Show that the data is cached when fetching again.
const timeData = await googleSiteKitAPIGetTime();
expect( timeData ).toEqual( initialTimeData );
// Delete auth cookie to sign out the current user.
// This was needed in the past due to cache clearing
// that was hooked into the logout action.
// Deleting cookies makes it more clear that the observed
// result is due to a new session only. It's also faster.
await deleteAuthCookie();
await safeLoginUser( 'admin', 'password' );
await goToWordPressDashboard();
// Now that we're in a new session, we expect the API cache to be clear
// so the request should hit the backend and produce a new (greater) value.
const newTimeData = await googleSiteKitAPIGetTime();
expect( newTimeData ).not.toEqual( initialTimeData );
expect( initialTimeData ).toMatchObject( {
microtime: expect.any( Number ),
} );
expect( newTimeData.microtime ).toBeGreaterThan(
initialTimeData.microtime
);
} );
} );