Files
Toniandwxiaoguang 518e08c2c7 fix: discard stale markdown preview and diff responses (#40289)
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>
2026-11-10 14:22:53 +00:00

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']]);
});