Skip to content

Commit ca36a8a

Browse files
authored
Merge pull request #10103 from google/fix/8229-e2e-tests
Update api-cache test to be more stable when running quickly
2 parents 2d2ccc4 + 49fb57b commit ca36a8a

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

assets/js/components/KeyMetrics/hooks/useDisplayCTAWidget.test.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,14 @@ describe( 'useDisplayCTAWidget hook', () => {
107107
)
108108
);
109109

110-
const { result } = await renderHook( () => useDisplayCTAWidget(), {
111-
registry,
112-
} );
110+
const { result, waitForRegistry } = await renderHook(
111+
() => useDisplayCTAWidget(),
112+
{
113+
registry,
114+
}
115+
);
113116

117+
await waitForRegistry();
114118
expect( result.current ).toBe( false );
115119
} );
116120

@@ -125,10 +129,14 @@ describe( 'useDisplayCTAWidget hook', () => {
125129
)
126130
);
127131

128-
const { result } = await renderHook( () => useDisplayCTAWidget(), {
129-
registry,
130-
} );
132+
const { result, waitForRegistry } = await renderHook(
133+
() => useDisplayCTAWidget(),
134+
{
135+
registry,
136+
}
137+
);
131138

139+
await waitForRegistry();
132140
expect( result.current ).toBe( false );
133141
} );
134142
} );

tests/e2e/mu-plugins/e2e-rest-time-endpoint.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function () {
2626
'callback' => function () {
2727
return array(
2828
'time' => time(),
29-
'microtime' => microtime(),
29+
'microtime' => microtime( true ),
3030
);
3131
},
3232
'permission_callback' => '__return_true',

tests/e2e/specs/api-cache.test.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ describe( 'API cache', () => {
7878

7979
const initialTimeData = await googleSiteKitAPIGetTime();
8080
expect( initialTimeData ).toMatchObject( {
81-
time: expect.any( Number ),
81+
microtime: expect.any( Number ),
8282
} );
8383

8484
// Show that the data is cached when fetching again.
@@ -99,9 +99,12 @@ describe( 'API cache', () => {
9999
// Now that we're in a new session, we expect the API cache to be clear
100100
// so the request should hit the backend and produce a new (greater) value.
101101
const newTimeData = await googleSiteKitAPIGetTime();
102+
expect( newTimeData ).not.toEqual( initialTimeData );
102103
expect( initialTimeData ).toMatchObject( {
103-
time: expect.any( Number ),
104+
microtime: expect.any( Number ),
104105
} );
105-
expect( newTimeData.time ).toBeGreaterThan( initialTimeData.time );
106+
expect( newTimeData.microtime ).toBeGreaterThan(
107+
initialTimeData.microtime
108+
);
106109
} );
107110
} );

tests/js/test-utils.js

+3
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,11 @@ const customRenderHook = ( callback, options = {} ) => {
208208
);
209209
}
210210

211+
const waitForRegistry = createWaitForRegistry( registry );
212+
211213
return {
212214
...renderHook( callback, { wrapper: Wrapper, ...renderHookOptions } ),
215+
waitForRegistry: () => actHook( waitForRegistry ),
213216
setInView,
214217
registry,
215218
};

0 commit comments

Comments
 (0)