fix(popular-posts): randomize popular thread board selection on mount

This commit is contained in:
plebeius
2026-03-08 16:58:55 +08:00
parent 04b98b84a4
commit c27590372c
2 changed files with 62 additions and 29 deletions
@@ -69,6 +69,21 @@ const renderHook = async (addresses: string[], subplebbits: Array<unknown>) => {
});
};
const resetHookRoot = () => {
act(() => root.unmount());
container.remove();
container = document.createElement('div');
document.body.appendChild(container);
root = createRoot(container);
};
const mockRandomSequence = (values: number[]) => {
let index = 0;
return vi.spyOn(Math, 'random').mockImplementation(() => values[index++] ?? values.at(-1) ?? 0);
};
describe('usePopularPosts', () => {
beforeEach(() => {
vi.clearAllMocks();
@@ -169,4 +184,28 @@ describe('usePopularPosts', () => {
expect(latestValue.isLoading).toBe(true);
expect(latestValue.popularPosts).toEqual([]);
});
it('reshuffles the selected boards on each mount while keeping one top thread per board', async () => {
const addresses = Array.from({ length: 10 }, (_, index) => `board-${index}.eth`);
const subplebbits = addresses.map((address, index) => createSubplebbit(address, [createPost(address, 'top', 30 - index), createPost(address, 'backup', 10 - index)]));
const keepOrderRandom = mockRandomSequence(Array.from({ length: addresses.length - 1 }, () => 0.999_999));
await renderHook(addresses, subplebbits);
expect(latestValue.isLoading).toBe(false);
expect(latestValue.popularPosts.map((post) => post.subplebbitAddress)).toEqual(addresses.slice(0, 8));
expect(latestValue.popularPosts.every((post) => post.cid.endsWith('-top'))).toBe(true);
keepOrderRandom.mockRestore();
resetHookRoot();
const rotateOrderRandom = mockRandomSequence(Array.from({ length: addresses.length - 1 }, () => 0));
await renderHook(addresses, subplebbits);
expect(latestValue.isLoading).toBe(false);
expect(latestValue.popularPosts.map((post) => post.subplebbitAddress)).toEqual(addresses.slice(1, 9));
expect(latestValue.popularPosts.every((post) => post.cid.endsWith('-top'))).toBe(true);
rotateOrderRandom.mockRestore();
});
});