ShtukbaseStandard
package file→ .shtukbase/packages/shtukbase-ui-react/test/place.test.ts

test/place.test.ts

154 lines, 7.3 KB.

  1. // The `place` action and the three checks on a scratch project: the library lands in the folder of the config page, an
  2. // update keeps what the project edited, and each check has a passing and a failing fixture.
  3. import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
  4. import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
  5. import { tmpdir } from 'node:os';
  6. import { dirname, join } from 'node:path';
  7. import { check as same } from '../checks/same.ts';
  8. import { check as states } from '../checks/states.ts';
  9. import { check as library } from '../checks/library.ts';
  10. import { HTML_COMPONENTS, PKG } from './lib.ts';
  11. import { files, MANIFEST } from '../actions/kitlib.ts';
  12. let root: string;
  13. beforeEach(() => {
  14. root = mkdtempSync(join(tmpdir(), 'sb-ui-react-'));
  15. });
  16. afterEach(() => rmSync(root, { recursive: true, force: true }));
  17. const put = (rel: string, text: string) => {
  18. mkdirSync(dirname(join(root, rel)), { recursive: true });
  19. writeFileSync(join(root, rel), text);
  20. };
  21. const place = (...args: string[]) => {
  22. const p = Bun.spawnSync(['bun', join(PKG, 'actions', 'place.ts'), ...args], { cwd: root, stdout: 'pipe', stderr: 'pipe' });
  23. if (p.exitCode !== 0) throw new Error(`place failed: ${p.stderr.toString()}${p.stdout.toString()}`);
  24. return p.stdout.toString();
  25. };
  26. /** A project that has the HTML library's CSS (what `@import` finds) and, when the package is there, its components. */
  27. const withHtml = () => {
  28. put('design/shtukbase-ui-html/ui.css', '.button{}\n');
  29. if (existsSync(HTML_COMPONENTS)) for (const f of files(HTML_COMPONENTS)) put(`design/shtukbase-ui-html/components/${f}`, readFileSync(join(HTML_COMPONENTS, f), 'utf8'));
  30. };
  31. describe('place', () => {
  32. test('lays the library in the default folder, every file and a manifest', () => {
  33. withHtml();
  34. const out = place();
  35. expect(out).toContain('src/sb-ui/');
  36. const man = JSON.parse(readFileSync(join(root, 'src/sb-ui', MANIFEST), 'utf8'));
  37. expect(Object.keys(man.files)).toContain('primitives/button.tsx');
  38. expect(Object.keys(man.files)).toContain('styles/index.css');
  39. for (const rel of Object.keys(man.files)) expect(existsSync(join(root, 'src/sb-ui', rel)), rel).toBe(true);
  40. });
  41. test('the CSS import points at the project\'s ui.css, from the folder of the config page', () => {
  42. put('design/shtukbase-ui-react.md', '---\ntype: Page\ntarget: app/ui\ncss: design/shtukbase-ui-html/ui.css\n---\n');
  43. withHtml();
  44. place();
  45. expect(readFileSync(join(root, 'app/ui/styles/index.css'), 'utf8')).toContain('@import "../../../design/shtukbase-ui-html/ui.css";');
  46. });
  47. test('a second run changes nothing; an edited file is kept, --force replaces it', () => {
  48. withHtml();
  49. place();
  50. const edited = join(root, 'src/sb-ui/primitives/badge.tsx');
  51. writeFileSync(edited, `${readFileSync(edited, 'utf8')}// mine\n`);
  52. expect(place()).toContain('kept 1');
  53. expect(readFileSync(edited, 'utf8')).toContain('// mine');
  54. expect(place('--force')).toContain('written 1');
  55. expect(readFileSync(edited, 'utf8')).not.toContain('// mine');
  56. });
  57. });
  58. describe('SBR-R01 library', () => {
  59. test('passes on a placed library', () => {
  60. withHtml();
  61. place();
  62. expect(library(root)).toEqual([]);
  63. });
  64. test('fails on a missing file, a broken import and a missing CSS', () => {
  65. withHtml();
  66. place();
  67. rmSync(join(root, 'src/sb-ui/lib/controls.ts'));
  68. const out = library(root).join('\n');
  69. expect(out).toContain('lib/controls.ts: library file is missing');
  70. expect(out).toContain('import "../lib/controls" finds no file');
  71. rmSync(join(root, 'design/shtukbase-ui-html/ui.css'));
  72. expect(library(root).join('\n')).toContain('@import');
  73. });
  74. test('says that the library is not placed', () => {
  75. expect(library(root).join('\n')).toContain('the library is not placed');
  76. });
  77. });
  78. describe('SBR-R02 states', () => {
  79. test('passes on a placed library', () => {
  80. withHtml();
  81. place();
  82. expect(states(root)).toEqual([]);
  83. });
  84. test('fails on a component with no states file and on a states file with no meta', () => {
  85. withHtml();
  86. place();
  87. rmSync(join(root, 'src/sb-ui/primitives/kicker.states.tsx'));
  88. writeFileSync(join(root, 'src/sb-ui/primitives/chip.states.tsx'), 'export const nothing = 1;\n');
  89. const out = states(root).join('\n');
  90. expect(out).toContain('primitives/kicker.tsx: no states file');
  91. expect(out).toContain('primitives/chip.states.tsx: the states file has no export const meta');
  92. });
  93. });
  94. describe('SBR-R03 same components as the HTML library', () => {
  95. test('passes on a placed library next to the HTML library', () => {
  96. withHtml();
  97. place();
  98. expect(same(root)).toEqual([]);
  99. });
  100. test('fails on an id that is not the file name, a level in the wrong folder, a header with problems', () => {
  101. withHtml();
  102. place();
  103. const p = (rel: string, f: (t: string) => string) => writeFileSync(join(root, 'src/sb-ui', rel), f(readFileSync(join(root, 'src/sb-ui', rel), 'utf8')));
  104. p('primitives/kicker.tsx', (t) => t.replace('id: kicker', 'id: kickerx'));
  105. p('primitives/chip.tsx', (t) => t.replace('level: primitive', 'level: composite'));
  106. p('primitives/badge.tsx', (t) => t.replace('level: primitive', 'level: nonsense'));
  107. const out = same(root).join('\n');
  108. expect(out).toContain('the header id is "kickerx", the file is "kicker"');
  109. expect(out).toContain('level "composite" belongs in composites/, not primitives/');
  110. expect(out).toContain('level "nonsense" is not one of');
  111. });
  112. test.skipIf(!existsSync(HTML_COMPONENTS))('fails when the headers of the two libraries differ or a component is missing', () => {
  113. withHtml();
  114. place();
  115. const f = join(root, 'src/sb-ui/primitives/button.tsx');
  116. writeFileSync(f, readFileSync(f, 'utf8').replace('options: [sm, default, lg, icon]', 'options: [sm, default, lg]'));
  117. rmSync(join(root, 'src/sb-ui/blocks/area.tsx'));
  118. const out = same(root).join('\n');
  119. expect(out).toContain('button.tsx: header controls differs from the HTML library');
  120. expect(out).toContain('the HTML library has "area", the React library has not');
  121. });
  122. test('without an HTML library there is nothing to compare, the headers are still checked', () => {
  123. put('design/shtukbase-ui-html/ui.css', '.button{}\n');
  124. place();
  125. expect(same(root)).toEqual([]);
  126. });
  127. });
  128. describe('with design-components', () => {
  129. const DC = join(PKG, '..', 'design-components');
  130. test.skipIf(!existsSync(HTML_COMPONENTS) || !existsSync(join(DC, 'actions', 'sync.ts')))(
  131. 'place lists the target in sources: of the collection, and sync joins the React files to the HTML ones',
  132. () => {
  133. withHtml();
  134. put('design/components.md', readFileSync(join(DC, 'pages', 'design', 'components.md'), 'utf8'));
  135. expect(place()).toContain('added src/sb-ui to sources:');
  136. expect(readFileSync(join(root, 'design/components.md'), 'utf8')).toContain('sources: [src/sb-ui]');
  137. expect(place()).not.toContain('added src/sb-ui');
  138. const p = Bun.spawnSync(['bun', join(DC, 'actions', 'sync.ts')], { cwd: root, stdout: 'pipe', stderr: 'pipe' });
  139. expect(p.exitCode, p.stderr.toString() + p.stdout.toString()).toBe(0);
  140. expect(p.stdout.toString()).toMatch(/(\d+) components \(\1 html, \1 react\)/);
  141. expect(readFileSync(join(root, 'design/components/package-card.md'), 'utf8')).toContain('src/sb-ui/composites/package-card.tsx');
  142. },
  143. );
  144. });