Files
site-builder/craft/src/editor/Canvas.device.test.tsx
T
shadowdao 22c59eb970 feat(site-builder): tablet/mobile canvas emulates published hide, stacking and full-width
Frame gets data-device; RenderNode mirrors hide flags and selection onto the
dom; editor.css emulates the exported breakpoints inside the narrowed frame.
Also: data-wsb-overridden counts only set values, responsive scope ids are
zone-prefixed (h-/b-/f-) so ROOT no longer collides, and comment delimiters
are stripped from responsive declarations.

Refs #37
2026-10-07 11:39:25 -07:00

60 lines
2.5 KiB
TypeScript

import React from 'react';
import { describe, test, expect, afterEach } from 'vitest';
// vitest strips CSS from `?raw`/`?inline` imports, so read the file directly
// (no @types/node in this project -- hence the ignores).
// @ts-ignore
import { readFileSync } from 'node:fs';
import { renderEditorHarness, EditorHarness } from '../test-utils/editorHarness';
import { DeviceProvider, useDevice } from '../state/DeviceContext';
import { Canvas } from './Canvas';
import type { DeviceMode } from '../types';
let h: EditorHarness | null = null;
let setDevice: (d: DeviceMode) => void = () => {};
const Grab: React.FC = () => {
setDevice = useDevice().setDevice;
return null;
};
afterEach(() => {
h?.unmount();
h = null;
});
const frame = () => h!.container.querySelector('.canvas-device-frame') as HTMLElement;
describe('Canvas device frame (#37)', () => {
test('frame carries data-device and follows setDevice()', () => {
h = renderEditorHarness();
h.mountChild(
<DeviceProvider>
<Grab />
<Canvas showGuides />
</DeviceProvider>,
);
expect(frame().getAttribute('data-device')).toBe('desktop');
h.act(() => setDevice('mobile'));
expect(frame().getAttribute('data-device')).toBe('mobile');
expect(frame().style.width).toBe('375px');
h.act(() => setDevice('tablet'));
expect(frame().getAttribute('data-device')).toBe('tablet');
expect(frame().style.width).toBe('768px');
});
});
describe('editor.css device emulation selectors', () => {
const css: string = readFileSync(`${(globalThis as any).process.cwd()}/src/styles/editor.css`, 'utf8');
const region = css.slice(css.indexOf('#37 device emulation'));
test('hide rules per device, selected-node exemption, stacking, fullwidth, badge', () => {
expect(region).toContain('.canvas-device-frame[data-device="desktop"] [data-hide-desktop]:not([data-craft-selected])');
expect(region).toContain('.canvas-device-frame[data-device="tablet"] [data-hide-tablet]:not([data-craft-selected])');
expect(region).toContain('.canvas-device-frame[data-device="mobile"] [data-hide-mobile]:not([data-craft-selected])');
expect(region).toContain('[data-hide-mobile][data-craft-selected]');
expect(region).toContain('[style*="display: flex"][style*="flex-direction: row"]');
expect(region).toContain('flex-direction: column !important');
expect(region).toContain('.canvas-device-frame:not([data-device="desktop"]) [data-fullwidth]');
expect(region).toContain('[data-device="mobile"]::before');
});
});