Skip to content

Commit 95f0992

Browse files
Fix unit tests (vercel#37)
* Fix failing unit tests * Add test suite to PR and Release workflows * Create short-swans-rush.md
1 parent 70bd8bc commit 95f0992

File tree

4 files changed

+73
-23
lines changed

4 files changed

+73
-23
lines changed

.changeset/short-swans-rush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"streamdown": patch
3+
---
4+
5+
fix unit tests, run on release and PR

.github/workflows/release.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ jobs:
2828
- name: Install Dependencies
2929
run: pnpm i
3030

31+
- name: Run Tests
32+
run: pnpm test
33+
3134
- name: Create Release Pull Request or Publish to npm
3235
id: changesets
3336
uses: changesets/action@v1

.github/workflows/test.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Test
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
push:
8+
branches:
9+
- main
10+
11+
jobs:
12+
test:
13+
name: Run Tests
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout Repo
18+
uses: actions/checkout@v4
19+
20+
- name: Setup pnpm
21+
uses: pnpm/action-setup@v4
22+
23+
- name: Setup Node.js 22
24+
uses: actions/setup-node@v4
25+
with:
26+
node-version: 22
27+
28+
- name: Install Dependencies
29+
run: pnpm i
30+
31+
- name: Run Tests
32+
run: pnpm test

packages/streamdown/__tests__/components.test.tsx

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,14 @@ describe('Markdown Components', () => {
183183
code
184184
</Code>
185185
);
186-
const code = container.querySelector('code');
187-
expect(code).toBeTruthy();
188-
expect(code?.className).not.toContain('rounded');
189-
expect(code?.className).not.toContain('bg-muted');
186+
// Block code renders a CodeBlock component, not a plain code element
187+
const codeBlock = container.querySelector('[class*="my-4"]');
188+
expect(codeBlock).toBeTruthy();
189+
expect(codeBlock?.className).toContain('my-4');
190+
expect(codeBlock?.className).toContain('h-auto');
191+
expect(codeBlock?.className).toContain('rounded-lg');
192+
expect(codeBlock?.className).toContain('border');
193+
expect(codeBlock?.className).toContain('p-4');
190194
});
191195

192196
it('should render pre with code block', () => {
@@ -195,24 +199,32 @@ describe('Markdown Components', () => {
195199
children: 'const x = 1;',
196200
});
197201
const { container } = render(<Pre node={null as any}>{codeElement}</Pre>);
198-
// The pre component renders a CodeBlock, not a pre element
199-
const codeBlock = container.querySelector('[class*="my-4"]');
200-
expect(codeBlock).toBeTruthy();
201-
expect(codeBlock?.className).toContain('my-4');
202-
expect(codeBlock?.className).toContain('h-auto');
202+
// The pre component now just returns its children
203+
// The code element should be present as a child
204+
const code = container.querySelector('code');
205+
expect(code).toBeTruthy();
206+
expect(code?.textContent).toBe('const x = 1;');
203207
});
204208

205209
it('should extract language from code className', () => {
206-
const Pre = components.pre!;
207-
const codeElement = React.createElement('code', {
208-
children: 'const x = 1;',
209-
className: 'language-javascript',
210-
});
211-
const nodeWithClassName = {
212-
properties: { className: 'language-javascript' }
213-
};
214-
const { container } = render(<Pre node={nodeWithClassName as any}>{codeElement}</Pre>);
215-
// The pre component renders a CodeBlock with the extracted language
210+
const Code = components.code!;
211+
// Test the code component directly since it handles language extraction
212+
const { container } = render(
213+
<Code
214+
node={
215+
{
216+
position: {
217+
start: { line: 1, column: 1 },
218+
end: { line: 2, column: 10 },
219+
},
220+
} as any
221+
}
222+
className="language-javascript"
223+
>
224+
const x = 1;
225+
</Code>
226+
);
227+
// Code component with multi-line position renders a CodeBlock
216228
const codeBlock = container.querySelector('[class*="my-4"]');
217229
expect(codeBlock).toBeTruthy();
218230
});
@@ -222,10 +234,8 @@ describe('Markdown Components', () => {
222234
const { container } = render(
223235
<Pre node={null as any}>plain text code</Pre>
224236
);
225-
// The pre component renders a CodeBlock which processes code asynchronously
226-
const codeBlock = container.querySelector('[class*="my-4"]');
227-
expect(codeBlock).toBeTruthy();
228-
// The CodeBlock component exists but text is rendered asynchronously
237+
// The pre component now just returns its children directly
238+
expect(container.textContent).toBe('plain text code');
229239
});
230240
});
231241

0 commit comments

Comments
 (0)