|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * objectstack#36344 — a markdown value that starts with a heading, drawn in a |
| 11 | + * grid column, keeps the row the height of every other row. |
| 12 | + * |
| 13 | + * The grid clamps a rich-content cell to one line, but the line was the |
| 14 | + * heading's: `prose-sm` draws an `h1` at heading size, so the cell showed a |
| 15 | + * truncated H1 and the row grew. In a grid column (`inGridColumn`) the cell |
| 16 | + * now draws its value in the `cell` face: no heading element, the heading as |
| 17 | + * a bold line, and the body at the cell's own font size and line height. |
| 18 | + * Every other caller keeps the `document` face and its heading elements. |
| 19 | + * |
| 20 | + * jsdom does no layout, so these pins read the element and class contract the |
| 21 | + * row height rests on; the pixel readings are in the pull request. |
| 22 | + */ |
| 23 | + |
| 24 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 25 | +import React from 'react'; |
| 26 | +import { render, waitFor, cleanup } from '@testing-library/react'; |
| 27 | +import '@testing-library/jest-dom'; |
| 28 | +// Module scope, with the SAME specifier `MarkdownCellRenderer`'s `React.lazy` |
| 29 | +// factory uses, so the lazy chunk is resolved before any assertion runs. |
| 30 | +import '../widgets/MarkdownContent.js'; |
| 31 | +import { getCellRenderer, resolveCellRendererType } from '../index'; |
| 32 | + |
| 33 | +const HEADING_VALUE = '# Heading\n\nBody with **bold** text'; |
| 34 | +const HEADINGS = 'h1, h2, h3, h4, h5, h6'; |
| 35 | + |
| 36 | +afterEach(() => cleanup()); |
| 37 | + |
| 38 | +/** Resolve the way every read surface does, then settle past the Suspense fallback. */ |
| 39 | +async function renderMarkdown(value: string, props: { inGridColumn?: boolean } = {}) { |
| 40 | + const Renderer = getCellRenderer(resolveCellRendererType({ type: 'markdown' }) || 'markdown'); |
| 41 | + const utils = render(<Renderer value={value} field={{ type: 'markdown' } as any} {...props} />); |
| 42 | + const face = await waitFor(() => { |
| 43 | + const el = utils.container.querySelector<HTMLElement>('[data-markdown-face]'); |
| 44 | + if (!el) throw new Error('markdown body not rendered yet'); |
| 45 | + return el; |
| 46 | + }); |
| 47 | + return { ...utils, face }; |
| 48 | +} |
| 49 | + |
| 50 | +describe('objectstack#36344 — a markdown heading in a grid cell is drawn at the cell size', () => { |
| 51 | + it('in a grid column: no heading element, the heading is a bold line, the body inherits the cell size', async () => { |
| 52 | + const { face } = await renderMarkdown(HEADING_VALUE, { inGridColumn: true }); |
| 53 | + |
| 54 | + expect(face).toHaveAttribute('data-markdown-face', 'cell'); |
| 55 | + expect(face.querySelector(HEADINGS)).toBeNull(); |
| 56 | + |
| 57 | + const heading = face.querySelector<HTMLElement>('[data-markdown-heading]'); |
| 58 | + expect(heading?.tagName).toBe('P'); |
| 59 | + expect(heading).toHaveTextContent('Heading'); |
| 60 | + expect(heading).toHaveClass('font-semibold'); |
| 61 | + |
| 62 | + // The body takes the cell's own font size and line height, so the line |
| 63 | + // the grid's one-line clamp shows is a line of the cell's text. |
| 64 | + expect(face).toHaveClass('text-[length:inherit]', 'leading-[inherit]'); |
| 65 | + |
| 66 | + // The rest of the value is still formatted, not flattened to source text. |
| 67 | + expect(face.querySelector('strong')).toHaveTextContent('bold'); |
| 68 | + expect(face.textContent).not.toContain('#'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('in a grid column: every heading level is drawn the same way', async () => { |
| 72 | + const value = ['# one', '## two', '### three', '#### four', '##### five', '###### six'].join('\n\n'); |
| 73 | + const { face } = await renderMarkdown(value, { inGridColumn: true }); |
| 74 | + expect(face.querySelector(HEADINGS)).toBeNull(); |
| 75 | + expect(face.querySelectorAll('[data-markdown-heading]')).toHaveLength(6); |
| 76 | + }); |
| 77 | + |
| 78 | + it('CONTROL — outside a grid column (the record page) the heading element and its size stay', async () => { |
| 79 | + const { face } = await renderMarkdown(HEADING_VALUE); |
| 80 | + |
| 81 | + expect(face).toHaveAttribute('data-markdown-face', 'document'); |
| 82 | + expect(face.querySelector('h1')).toHaveTextContent('Heading'); |
| 83 | + expect(face.querySelector('[data-markdown-heading]')).toBeNull(); |
| 84 | + expect(face).toHaveClass('prose', 'prose-sm'); |
| 85 | + expect(face).not.toHaveClass('text-[length:inherit]'); |
| 86 | + expect(face).not.toHaveClass('leading-[inherit]'); |
| 87 | + }); |
| 88 | +}); |
0 commit comments