mirror of
https://github.com/nexu-io/open-design.git
synced 2026-06-01 03:14:35 +07:00
- SettingsDialog: use 'sparkles' for Pet section nav icon (main's choice; the merge picked up release's 'paw' which is not in main's IconName union). - FileViewer.test.tsx: take release's version which passes projectKind on every render (release added that prop in #1509 analytics; main's tests predate that prop). - FileViewer.manual-edit{,-history}.test.tsx: keep main's tests but pass projectKind="prototype" so they satisfy the merged FileViewer Props.
174 lines
6.5 KiB
TypeScript
174 lines
6.5 KiB
TypeScript
// @vitest-environment jsdom
|
|
|
|
import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
import type { ComponentProps } from 'react';
|
|
import type { ProjectFile } from '../../src/types';
|
|
|
|
const panelState = vi.hoisted(() => ({
|
|
props: null as ComponentProps<typeof import('../../src/components/ManualEditPanel').ManualEditPanel> | null,
|
|
}));
|
|
|
|
vi.mock('../../src/components/ManualEditPanel', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('../../src/components/ManualEditPanel')>();
|
|
return {
|
|
...actual,
|
|
ManualEditPanel: (props: ComponentProps<typeof actual.ManualEditPanel>) => {
|
|
panelState.props = props;
|
|
return <div data-testid="mock-manual-edit-panel" />;
|
|
},
|
|
};
|
|
});
|
|
|
|
import { FileViewer } from '../../src/components/FileViewer';
|
|
|
|
afterEach(() => {
|
|
cleanup();
|
|
panelState.props = null;
|
|
vi.restoreAllMocks();
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
describe('FileViewer manual edit history regressions', () => {
|
|
it('flushes pending style edits before activating draw mode from manual edit', async () => {
|
|
const initialSource = '<!doctype html><html><body><h1 data-od-id="hero" style="color: #111111">Hero</h1></body></html>';
|
|
let saveResolve!: (value: Response) => void;
|
|
const saveResponse = new Promise<Response>((resolve) => {
|
|
saveResolve = resolve;
|
|
});
|
|
const savedSources: string[] = [];
|
|
const fetchMock = vi.fn(async (input: string | URL | Request, init?: RequestInit) => {
|
|
const url = typeof input === 'string' ? input : input instanceof Request ? input.url : String(input);
|
|
if (url.includes('/api/projects/project-1/deployments')) {
|
|
return new Response(JSON.stringify({ deployments: [] }), {
|
|
status: 200,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
}
|
|
if (url.includes('/api/projects/project-1/files') && init?.method === 'POST') {
|
|
const payload = JSON.parse(String(init.body)) as { content: string };
|
|
savedSources.push(payload.content);
|
|
return saveResponse;
|
|
}
|
|
if (url.includes('/api/projects/project-1/raw/preview.html')) {
|
|
return new Response(initialSource, { status: 200 });
|
|
}
|
|
return new Response('{}', { status: 200, headers: { 'Content-Type': 'application/json' } });
|
|
});
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
|
|
render(
|
|
<FileViewer projectId="project-1" projectKind="prototype" file={htmlPreviewFile()}
|
|
liveHtml={initialSource}
|
|
/>,
|
|
);
|
|
|
|
fireEvent.click(screen.getByTestId('manual-edit-mode-toggle'));
|
|
await waitFor(() => expect(panelState.props).not.toBeNull());
|
|
|
|
act(() => {
|
|
panelState.props?.onStyleChange?.('hero', { color: '#ef4444' }, 'Style: Hero');
|
|
});
|
|
fireEvent.click(screen.getByTestId('draw-overlay-toggle'));
|
|
|
|
await waitFor(() => expect(savedSources).toHaveLength(1));
|
|
expect(savedSources[0]).toContain('rgb(239, 68, 68)');
|
|
expect(screen.getByTestId('manual-edit-mode-toggle').getAttribute('aria-pressed')).toBe('true');
|
|
expect(screen.getByTestId('draw-overlay-toggle').getAttribute('aria-pressed')).toBe('false');
|
|
|
|
await act(async () => {
|
|
saveResolve(new Response(JSON.stringify({ file: htmlPreviewFile() }), {
|
|
status: 200,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
}));
|
|
await saveResponse;
|
|
});
|
|
|
|
await waitFor(() => expect(screen.getByTestId('manual-edit-mode-toggle').getAttribute('aria-pressed')).toBe('false'));
|
|
expect(screen.getByTestId('draw-overlay-toggle').getAttribute('aria-pressed')).toBe('true');
|
|
});
|
|
|
|
it('uses the undone source snapshot for a follow-up edit after undo', async () => {
|
|
const initialSource = '<!doctype html><html><body><h1 data-od-id="hero" style="color: #111111">Hero</h1></body></html>';
|
|
let persistedSource = initialSource;
|
|
const savedSources: string[] = [];
|
|
const fetchMock = vi.fn(async (input: string | URL | Request, init?: RequestInit) => {
|
|
const url = typeof input === 'string' ? input : input instanceof Request ? input.url : String(input);
|
|
if (url.includes('/api/projects/project-1/deployments')) {
|
|
return new Response(JSON.stringify({ deployments: [] }), {
|
|
status: 200,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
}
|
|
if (url.includes('/api/projects/project-1/files') && init?.method === 'POST') {
|
|
const payload = JSON.parse(String(init.body)) as { content: string };
|
|
persistedSource = payload.content;
|
|
savedSources.push(payload.content);
|
|
return new Response(JSON.stringify({ file: htmlPreviewFile() }), {
|
|
status: 200,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
}
|
|
if (url.includes('/api/projects/project-1/raw/preview.html')) {
|
|
return new Response(persistedSource, { status: 200 });
|
|
}
|
|
return new Response('{}', { status: 200, headers: { 'Content-Type': 'application/json' } });
|
|
});
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
|
|
render(
|
|
<FileViewer projectId="project-1" projectKind="prototype" file={htmlPreviewFile()}
|
|
liveHtml={initialSource}
|
|
/>,
|
|
);
|
|
|
|
fireEvent.click(screen.getByTestId('manual-edit-mode-toggle'));
|
|
await waitFor(() => expect(panelState.props).not.toBeNull());
|
|
|
|
act(() => {
|
|
panelState.props?.onApplyPatch(
|
|
{ kind: 'set-style', id: 'hero', styles: { color: '#ef4444' } },
|
|
'Style: Hero',
|
|
);
|
|
});
|
|
await waitFor(() => expect(savedSources).toHaveLength(1));
|
|
expect(savedSources[0]).toContain('rgb(239, 68, 68)');
|
|
|
|
act(() => {
|
|
panelState.props?.onUndo();
|
|
});
|
|
await waitFor(() => expect(savedSources).toHaveLength(2));
|
|
expect(savedSources[1]).toBe(initialSource);
|
|
|
|
act(() => {
|
|
panelState.props?.onApplyPatch(
|
|
{ kind: 'set-style', id: 'hero', styles: { backgroundColor: '#f97316' } },
|
|
'Style: Hero',
|
|
);
|
|
});
|
|
await waitFor(() => expect(savedSources).toHaveLength(3));
|
|
|
|
expect(savedSources[2]).toContain('background-color: rgb(249, 115, 22)');
|
|
expect(savedSources[2]).not.toContain('rgb(239, 68, 68)');
|
|
});
|
|
});
|
|
|
|
function htmlPreviewFile(): ProjectFile {
|
|
return {
|
|
name: 'preview.html',
|
|
path: 'preview.html',
|
|
type: 'file',
|
|
size: 1024,
|
|
mtime: 1710000000,
|
|
mime: 'text/html',
|
|
kind: 'html',
|
|
artifactManifest: {
|
|
version: 1,
|
|
kind: 'html',
|
|
title: 'Preview',
|
|
entry: 'preview.html',
|
|
renderer: 'html',
|
|
exports: ['html'],
|
|
},
|
|
};
|
|
}
|