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

test/smoke.test.ts

36 lines, 2.1 KB.

  1. // The harness itself: the vendored (or installed) React renders a shadcn component through Radix, the parser reads it back.
  2. import { expect, test } from 'bun:test';
  3. import * as React from 'react';
  4. import { Button } from '../library/primitives/button.tsx';
  5. import { Switch } from '../library/primitives/switch.tsx';
  6. import { compare, parseHtml, render } from './lib.ts';
  7. test('a shadcn button renders with our class and the shadcn data attributes', () => {
  8. const [el] = parseHtml(render(Button, { variant: 'outline', size: 'sm', children: 'Cancel' }));
  9. expect(el).toMatchObject({ tag: 'button', attrs: { 'data-slot': 'button', 'data-variant': 'outline', 'data-size': 'sm' } });
  10. expect((el as { attrs: { class: string } }).attrs.class.split(' ')).toContain('button');
  11. });
  12. test('asChild goes through Radix Slot', () => {
  13. const html = render(Button, { asChild: true, children: React.createElement('a', { href: '#x' }, 'Open') });
  14. expect(parseHtml(html)[0]).toMatchObject({ tag: 'a', attrs: { href: '#x', 'data-slot': 'button' } });
  15. });
  16. test('a Radix switch renders its root and thumb', () => {
  17. const [el] = parseHtml(render(Switch, { defaultChecked: true })) as { tag: string; attrs: Record<string, string>; children: unknown[] }[];
  18. expect(el.tag).toBe('button');
  19. expect(el.attrs).toMatchObject({ role: 'switch', 'data-state': 'checked', 'data-slot': 'switch' });
  20. expect(el.children).toHaveLength(1);
  21. });
  22. test('compare: every HTML attribute and class must be in the React markup', () => {
  23. const html = parseHtml('<button class="button" data-slot="button" data-variant="outline">Cancel</button>')[0];
  24. const ok = parseHtml('<button type="button" class="button x" data-slot="button" data-variant="outline">Cancel</button>')[0];
  25. const bad = parseHtml('<button class="x" data-slot="button" data-variant="ghost">Close</button>')[0];
  26. expect(compare(html, ok)).toEqual([]);
  27. expect(compare(html, bad)).toEqual([
  28. '/button[button]: class lacks button',
  29. '/button[button]: attribute data-variant is "ghost", the HTML has "outline"',
  30. '/button[button]: HTML has "Cancel", React has "Close"',
  31. ]);
  32. });