файл пакета→ .shtukbase/packages/shtukbase-ui-react/test/smoke.test.ts
test/smoke.test.ts
36 строк, 2.1 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // The harness itself: the vendored (or installed) React renders a shadcn component through Radix, the parser reads it back.
- import { expect, test } from 'bun:test';
- import * as React from 'react';
- import { Button } from '../library/primitives/button.tsx';
- import { Switch } from '../library/primitives/switch.tsx';
- import { compare, parseHtml, render } from './lib.ts';
- test('a shadcn button renders with our class and the shadcn data attributes', () => {
- const [el] = parseHtml(render(Button, { variant: 'outline', size: 'sm', children: 'Cancel' }));
- expect(el).toMatchObject({ tag: 'button', attrs: { 'data-slot': 'button', 'data-variant': 'outline', 'data-size': 'sm' } });
- expect((el as { attrs: { class: string } }).attrs.class.split(' ')).toContain('button');
- });
- test('asChild goes through Radix Slot', () => {
- const html = render(Button, { asChild: true, children: React.createElement('a', { href: '#x' }, 'Open') });
- expect(parseHtml(html)[0]).toMatchObject({ tag: 'a', attrs: { href: '#x', 'data-slot': 'button' } });
- });
- test('a Radix switch renders its root and thumb', () => {
- const [el] = parseHtml(render(Switch, { defaultChecked: true })) as { tag: string; attrs: Record<string, string>; children: unknown[] }[];
- expect(el.tag).toBe('button');
- expect(el.attrs).toMatchObject({ role: 'switch', 'data-state': 'checked', 'data-slot': 'switch' });
- expect(el.children).toHaveLength(1);
- });
- test('compare: every HTML attribute and class must be in the React markup', () => {
- const html = parseHtml('<button class="button" data-slot="button" data-variant="outline">Cancel</button>')[0];
- const ok = parseHtml('<button type="button" class="button x" data-slot="button" data-variant="outline">Cancel</button>')[0];
- const bad = parseHtml('<button class="x" data-slot="button" data-variant="ghost">Close</button>')[0];
- expect(compare(html, ok)).toEqual([]);
- expect(compare(html, bad)).toEqual([
- '/button[button]: class lacks button',
- '/button[button]: attribute data-variant is "ghost", the HTML has "outline"',
- '/button[button]: HTML has "Cancel", React has "Close"',
- ]);
- });