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*