import { describe, expect, it } from 'bun:test' import { existsSync, readFileSync } from 'node:fs' import { Glob } from 'bun' import { DASHBOARD_LOADER_DELAY, skeletonRange, skeletonSize } from '../../storage/framework/defaults/functions/dashboard-loading' /** * While the dashboard waits for content it draws that content's shape: a * table's header and rows, a list's rows, a record's fields, a form's inputs, * a grid's cards, a chart's bars. The shape takes its place from the first * frame, so nothing moves when the answer comes, and is seen only once the * wait is long enough to notice. */ const defaults = 'storage/framework/defaults' const components = `${defaults}/resources/components/Dashboard` const read = (path: string) => readFileSync(path, 'utf8') const SKELETONS = ['SkeletonTable', 'SkeletonList', 'SkeletonDetail', 'SkeletonForm', 'SkeletonCards', 'SkeletonChart'] describe('skeleton sizes', () => { it('draws a whole number of rows, none for a bad count, and never a page of them', () => { expect(skeletonRange(3)).toEqual([0, 1, 2]) expect(skeletonRange('4')).toEqual([0, 1, 2, 3]) expect(skeletonRange(2.9)).toEqual([0, 1]) for (const bad of [0, -2, Number.NaN, 'many', undefined, null]) expect(skeletonRange(bad)).toEqual([]) expect(skeletonRange(500)).toHaveLength(24) }) it('varies a bar\'s size, but gives the same bar the same size on every render', () => { const sizes = skeletonRange(6).map(row => skeletonSize(row, 1)) expect(new Set(sizes).size).toBeGreaterThan(3) expect(skeletonRange(6).map(row => skeletonSize(row, 1))).toEqual(sizes) for (const row of skeletonRange(12)) { for (const column of skeletonRange(6)) { const share = Number.parseInt(skeletonSize(row, column), 10) expect(skeletonSize(row, column)).toMatch(/^\d+%$/) expect(share).toBeGreaterThanOrEqual(36) expect(share).toBeLessThanOrEqual(88) } } expect(skeletonSize(-1, 2)).toMatch(/^\d+%$/) }) }) describe('how a skeleton looks', () => { const theme = read(`${defaults}/styles/dashboard-theme.css`) it('is hidden for as long as the dashboard waits before saying it is loading', () => { // A shape seen for a few frames is a flicker, and most answers come sooner. expect(theme).toContain(`[data-dashboard-skeleton] {\n animation: dashboard-skeleton-in 200ms ease-out ${DASHBOARD_LOADER_DELAY}ms both;\n}`) expect(theme).toContain('@keyframes dashboard-skeleton-in {\n from { opacity: 0; }\n}') }) it('draws its bars in the text colour, so every theme and both modes have them', () => { expect(theme).toContain('background: color-mix(in srgb, currentColor 9%, transparent);') }) it('pulses only for a viewer who has not asked for less motion', () => { const calm = theme.indexOf('@media (prefers-reduced-motion: no-preference) {\n [data-dashboard-bone]') expect(calm).toBeGreaterThan(-1) expect(theme.indexOf('animation: dashboard-bone-pulse')).toBeGreaterThan(calm) }) }) describe('every skeleton', () => { for (const name of SKELETONS) { const source = read(`${components}/Skeleton/${name}.stx`) const template = source.slice(source.indexOf('') + ''.length) it(`${name} is one status that a screen reader hears by name, with bars it does not`, () => { expect(template).toMatch(/^\s*
') expect(template).toContain('aria-hidden="true"') expect(source).toContain('const label = useReactiveProp(\'label\', \'Loading\')') }) it(`${name} says nothing that is not known yet: no word, count or name`, () => { // The model list's old placeholder rows sat under an "Actions" header // over a footer that said "No records". const text = template .replace(//g, '') .replace(/\{\{--[\s\S]*?--\}\}/g, '') .replace(/<[^>]*>/g, '') .trim() expect(text).toBe('') }) } }) describe('where the dashboard waits', () => { it('no page draws the quiet panel any more', () => { expect(existsSync(`${components}/UI/QuietLoader.stx`)).toBe(false) }) const pages: Array<[string, string]> = [ ['Models/ModelRecordsDashboard', '\n { expect(read(`${components}/${page}.stx`)).toContain(skeleton) }) } }) describe('across the dashboard', () => { const files = [...new Glob('**/*.stx').scanSync(components)].sort() const sources = files.map(file => [file, read(`${components}/${file}`)] as const) it('every bar is a shared one: no component pulses blocks of its own colour', () => { // Its own colour was light only (bg-slate-300) or one theme's grey, and it // was seen at once, so a quick answer flickered. for (const [file, source] of sources) expect([file, source.includes('animate-pulse')]).toEqual([file, false]) }) it('no content waits behind a line of loading text or a lone spinner', () => { // "Loading query logs" in an empty box said the page was busy, but not // what was coming or where. A spinner beside a button or in a row that is // being refreshed is not this: it marks one control, not missing content. // Two kinds of line stay: one beside a field that is filled in once its // text comes (`inline-flex`, under an editor's disabled textarea), and one // under a list that is already drawn, saying more is coming. const textOnly = /<(?:p|div|section)\b[^>]*>\s*(?:]*animate-spin[^>]*><\/span>\s*)?(?:Loading|Fetching)[^<]*<\/(?:p|div|section)>/g const spinnerPanel = /
]*class="flex[^"]*items-center justify-center[^"]*"[^>]*>\s*]*animate-spin/gi const besideAControl = (line: string) => /class="inline-flex\b/.test(line) || /Loading more\b/.test(line) for (const [file, source] of sources) { const lines = [...source.matchAll(textOnly), ...source.matchAll(spinnerPanel)].map(match => match[0]) expect([file, lines.filter(line => !besideAControl(line))]).toEqual([file, []]) } }) it('a bar is drawn inside something that waits before it is seen', () => { for (const [file, source] of sources) { if (!source.includes('data-dashboard-bone')) continue expect([file, source.includes('data-dashboard-skeleton')]).toEqual([file, true]) } }) })