|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 5 | +import { h } from 'preact'; |
| 6 | +import { render, screen, fireEvent, act, cleanup, waitFor } from '@testing-library/preact'; |
| 7 | +import type { ServerMessage, WsClient } from '../../src/ws-client.js'; |
| 8 | +import { DiscussionsPage } from '../../src/pages/DiscussionsPage.js'; |
| 9 | + |
| 10 | +vi.mock('react-i18next', () => ({ |
| 11 | + useTranslation: () => ({ |
| 12 | + t: (key: string) => key, |
| 13 | + }), |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock('../../src/components/P2pProgressCard.js', () => ({ |
| 17 | + P2pProgressCard: () => null, |
| 18 | +})); |
| 19 | + |
| 20 | +describe('DiscussionsPage', () => { |
| 21 | + let handler: ((msg: ServerMessage) => void) | null = null; |
| 22 | + let ws: WsClient; |
| 23 | + let scrollToMock: ReturnType<typeof vi.fn>; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + scrollToMock = vi.fn(); |
| 27 | + vi.spyOn(window, 'requestAnimationFrame').mockImplementation((cb: FrameRequestCallback) => { |
| 28 | + cb(0); |
| 29 | + return 1; |
| 30 | + }); |
| 31 | + Object.defineProperty(HTMLElement.prototype, 'scrollTo', { |
| 32 | + configurable: true, |
| 33 | + value: scrollToMock, |
| 34 | + }); |
| 35 | + |
| 36 | + ws = { |
| 37 | + send: vi.fn(), |
| 38 | + onMessage: (next: (msg: ServerMessage) => void) => { |
| 39 | + handler = next; |
| 40 | + return () => { handler = null; }; |
| 41 | + }, |
| 42 | + } as unknown as WsClient; |
| 43 | + }); |
| 44 | + |
| 45 | + afterEach(() => { |
| 46 | + cleanup(); |
| 47 | + vi.restoreAllMocks(); |
| 48 | + handler = null; |
| 49 | + }); |
| 50 | + |
| 51 | + it('defaults to auto-follow latest and scrolls to bottom when discussion content updates', async () => { |
| 52 | + const { container } = render(<DiscussionsPage ws={ws} />); |
| 53 | + |
| 54 | + expect(ws.send).toHaveBeenCalledWith({ type: 'p2p.list_discussions' }); |
| 55 | + |
| 56 | + await act(async () => { |
| 57 | + handler?.({ |
| 58 | + type: 'p2p.list_discussions_response', |
| 59 | + discussions: [{ id: 'disc-1', fileName: 'disc-1.md', preview: 'Topic 1', mtime: 100 }], |
| 60 | + } as ServerMessage); |
| 61 | + }); |
| 62 | + |
| 63 | + fireEvent.click(screen.getByText('Topic 1')); |
| 64 | + expect(ws.send).toHaveBeenLastCalledWith({ type: 'p2p.read_discussion', id: 'disc-1' }); |
| 65 | + |
| 66 | + const scrollEl = container.querySelector('.discussions-detail-scroll') as HTMLDivElement; |
| 67 | + Object.defineProperty(scrollEl, 'scrollHeight', { |
| 68 | + configurable: true, |
| 69 | + value: 640, |
| 70 | + }); |
| 71 | + |
| 72 | + await act(async () => { |
| 73 | + handler?.({ |
| 74 | + type: 'p2p.read_discussion_response', |
| 75 | + id: 'disc-1', |
| 76 | + content: 'Updated markdown', |
| 77 | + } as ServerMessage); |
| 78 | + }); |
| 79 | + |
| 80 | + expect((screen.getByLabelText('p2p.discussions.auto_follow_latest') as HTMLInputElement).checked).toBe(true); |
| 81 | + await waitFor(() => { |
| 82 | + expect(scrollToMock).toHaveBeenCalledWith({ top: 640, behavior: 'smooth' }); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('disables follow when unchecked, and re-enables it from the bottom arrow', async () => { |
| 87 | + const { container } = render(<DiscussionsPage ws={ws} />); |
| 88 | + |
| 89 | + await act(async () => { |
| 90 | + handler?.({ |
| 91 | + type: 'p2p.list_discussions_response', |
| 92 | + discussions: [{ id: 'disc-2', fileName: 'disc-2.md', preview: 'Topic 2', mtime: 100 }], |
| 93 | + } as ServerMessage); |
| 94 | + }); |
| 95 | + |
| 96 | + fireEvent.click(screen.getByText('Topic 2')); |
| 97 | + |
| 98 | + const scrollEl = container.querySelector('.discussions-detail-scroll') as HTMLDivElement; |
| 99 | + Object.defineProperty(scrollEl, 'scrollHeight', { |
| 100 | + configurable: true, |
| 101 | + value: 720, |
| 102 | + }); |
| 103 | + |
| 104 | + await act(async () => { |
| 105 | + handler?.({ |
| 106 | + type: 'p2p.read_discussion_response', |
| 107 | + id: 'disc-2', |
| 108 | + content: 'Initial content', |
| 109 | + } as ServerMessage); |
| 110 | + }); |
| 111 | + |
| 112 | + scrollToMock.mockClear(); |
| 113 | + |
| 114 | + const checkbox = screen.getByLabelText('p2p.discussions.auto_follow_latest') as HTMLInputElement; |
| 115 | + fireEvent.click(checkbox); |
| 116 | + expect(checkbox.checked).toBe(false); |
| 117 | + |
| 118 | + await act(async () => { |
| 119 | + handler?.({ |
| 120 | + type: 'p2p.read_discussion_response', |
| 121 | + id: 'disc-2', |
| 122 | + content: 'New content after manual scroll', |
| 123 | + } as ServerMessage); |
| 124 | + }); |
| 125 | + |
| 126 | + expect(scrollToMock).not.toHaveBeenCalled(); |
| 127 | + |
| 128 | + fireEvent.click(screen.getByTitle('p2p.discussions.scroll_top')); |
| 129 | + expect(checkbox.checked).toBe(false); |
| 130 | + expect(scrollToMock).toHaveBeenCalledWith({ top: 0, behavior: 'smooth' }); |
| 131 | + |
| 132 | + scrollToMock.mockClear(); |
| 133 | + |
| 134 | + fireEvent.click(screen.getByTitle('p2p.discussions.scroll_bottom')); |
| 135 | + expect(checkbox.checked).toBe(true); |
| 136 | + expect(scrollToMock).toHaveBeenCalledWith({ top: 720, behavior: 'smooth' }); |
| 137 | + }); |
| 138 | +}); |
0 commit comments