test: expand markdown and post menu coverage

Adds focused regression coverage for quote previews, markdown link rendering, embed toggles, and desktop/mobile post-menu actions. It also fixes the undefined feed-state crash in use-state-string.ts and removes unnecessary runtime imports with import type.
This commit is contained in:
plebeius
2026-03-08 16:30:02 +08:00
parent 9444d3e01a
commit b2137c7f91
11 changed files with 1338 additions and 3 deletions
@@ -0,0 +1,63 @@
import * as React from 'react';
import { createElement } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import Embed, { canEmbed } from '../embed';
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
const act = (React as { act?: (cb: () => void | Promise<void>) => void | Promise<void> }).act as (cb: () => void | Promise<void>) => void | Promise<void>;
let container: HTMLDivElement;
let root: Root;
const renderEmbed = async (url: string) => {
await act(async () => {
root.render(createElement(Embed, { url }));
});
};
describe('Embed', () => {
beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
root = createRoot(container);
});
afterEach(() => {
act(() => root.unmount());
container.remove();
});
it('renders youtube and twitch embeds with the expected iframe sources', async () => {
await renderEmbed('https://youtu.be/video123');
expect(container.querySelector<HTMLIFrameElement>('iframe')?.getAttribute('src')).toBe('https://www.youtube.com/embed/video123');
await renderEmbed('https://www.twitch.tv/videos/987654321');
expect(container.querySelector<HTMLIFrameElement>('iframe')?.getAttribute('src')).toContain('video=987654321');
expect(container.querySelector<HTMLIFrameElement>('iframe')?.getAttribute('src')).toContain(`parent=${window.location.hostname}`);
});
it('renders x and reddit embeds through iframe markup and leaves unsupported urls empty', async () => {
await renderEmbed('https://x.com/test/status/123');
expect(container.querySelector<HTMLIFrameElement>('iframe')?.getAttribute('srcdoc')).toContain('twitter-tweet');
await renderEmbed('https://www.reddit.com/r/test/comments/abc123/example/');
expect(container.querySelector<HTMLIFrameElement>('iframe')?.getAttribute('srcdoc')).toContain('reddit-embed-bq');
await renderEmbed('https://example.com/plain-link');
expect(container.innerHTML).toBe('');
});
it('reports embeddable hosts through canEmbed and rejects unsupported reddit pages', () => {
expect(canEmbed(new URL('https://www.youtube.com/watch?v=abc123'))).toBe(true);
expect(canEmbed(new URL('https://yt.example/watch?v=abc123'))).toBe(true);
expect(canEmbed(new URL('https://www.reddit.com/r/test/comments/abc123/example/'))).toBe(true);
expect(canEmbed(new URL('https://www.reddit.com/r/test/'))).toBe(false);
expect(canEmbed(new URL('https://example.com/plain-link'))).toBe(false);
});
});