mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-11 19:39:02 +00:00
Preview and diff tabs render whichever response arrives last, so a slow earlier request can overwrite the result of a newer click. Introduce ExclusiveFetch class to handle duplicate requests. Fixes: https://github.com/go-gitea/gitea/issues/40223 --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
35 lines
1.2 KiB
TypeScript
35 lines
1.2 KiB
TypeScript
import {GET, POST, PATCH, PUT, DELETE, ExclusiveFetch} from './fetch.ts';
|
|
|
|
// This test satisfies the linter for unused functions.
|
|
test('exports', () => {
|
|
expect(GET).toBeTruthy();
|
|
expect(POST).toBeTruthy();
|
|
expect(PATCH).toBeTruthy();
|
|
expect(PUT).toBeTruthy();
|
|
expect(DELETE).toBeTruthy();
|
|
});
|
|
|
|
test('ExclusiveFetch discards a superseded response while reading its body', async () => {
|
|
const bodyStarted = Promise.withResolvers<void>();
|
|
const oldBody = Promise.withResolvers<string>();
|
|
const oldResponse = new Response();
|
|
vi.spyOn(oldResponse, 'text').mockImplementation(() => {
|
|
bodyStarted.resolve();
|
|
return oldBody.promise;
|
|
});
|
|
const fetchMock = vi.spyOn(globalThis, 'fetch')
|
|
.mockResolvedValueOnce(oldResponse)
|
|
.mockResolvedValueOnce(new Response('new'));
|
|
onTestFinished(() => { fetchMock.mockRestore() });
|
|
|
|
const exclusiveFetch = new ExclusiveFetch();
|
|
const render = vi.fn();
|
|
const oldRequest = exclusiveFetch.request('/old').fetchText(render);
|
|
await bodyStarted.promise;
|
|
await exclusiveFetch.request('/new').fetchText(render);
|
|
|
|
oldBody.resolve('old');
|
|
await oldRequest;
|
|
expect(render.mock.calls).toEqual([['new']]);
|
|
});
|