Skip to content

Commit

Permalink
Added tests for <Markdown /> primitive
Browse files Browse the repository at this point in the history
  • Loading branch information
salmenus committed Jun 19, 2024
1 parent 2402835 commit 6878631
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
63 changes: 63 additions & 0 deletions specs/specs/aiChat/primitives/markdown-syntaxHighlighter.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
import {AdapterController} from '../../../utils/adapters';
import {adapterBuilder} from '../../../utils/adapterBuilder';
import {AiChat, Markdown, ResponseRenderer} from '@nlux-dev/react/src';
import {highlighter} from '@nlux-dev/highlighter/src';
import {memo} from 'react';
import {render} from '@testing-library/react';
import {waitForReactRenderCycle} from '../../../utils/wait';
import userEvent from '@testing-library/user-event';

describe('<AiChat /> + <Markdown /> + syntax highlighter', () => {
let adapterController: AdapterController | undefined;

beforeEach(() => {
adapterController = adapterBuilder()
.withBatchText(true)
.withStreamText(false)
.create();
});

afterEach(() => {
adapterController = undefined;
});

it('should parse and render the markdown content', async () => {
// Arrange
const responseRenderer: ResponseRenderer<string> = memo((props) => {
const {dataTransferMode, content} = props;
return (
<div>
<h1>Response Renderer</h1>
<Markdown>{content}</Markdown>
</div>
);
});

const {container} = render(
<AiChat
adapter={adapterController!.adapter}
messageOptions={{
responseRenderer,
showCodeBlockCopyButton: false,
syntaxHighlighter: highlighter,
}}
/>,
);
const textArea: HTMLTextAreaElement = container.querySelector('.nlux-comp-composer > textarea')!;
await waitForReactRenderCycle();

// Act
await userEvent.type(textArea, 'Hello{enter}');
await waitForReactRenderCycle();

adapterController!.resolve('```js\nvar someJsCode = true;\n\n```');
await waitForReactRenderCycle();

// Assert
const responseElement = container.querySelector('.nlux-comp-chatItem--received .nlux-markdown-container');
expect(responseElement!.innerHTML).toBe(
'<div class="code-block"><pre data-language="js" class="highlighter-dark"><div><span class="hljs-keyword">var</span> someJsCode = <span class="hljs-literal">true</span>;\n</div></pre></div>\n',
);
});
});
58 changes: 58 additions & 0 deletions specs/specs/aiChat/primitives/markdown.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
import {AdapterController} from '../../../utils/adapters';
import {adapterBuilder} from '../../../utils/adapterBuilder';
import {AiChat, Markdown, ResponseRenderer} from '@nlux-dev/react/src';
import {memo} from 'react';
import {render} from '@testing-library/react';
import {waitForReactRenderCycle} from '../../../utils/wait';
import userEvent from '@testing-library/user-event';

describe('<AiChat /> + <Markdown /> + inside custom renderers', () => {
let adapterController: AdapterController | undefined;

beforeEach(() => {
adapterController = adapterBuilder()
.withBatchText(true)
.withStreamText(false)
.create();
});

afterEach(() => {
adapterController = undefined;
});

it('should parse and render the markdown content', async () => {
// Arrange
const responseRenderer: ResponseRenderer<string> = memo((props) => {
const {dataTransferMode, content} = props;
return (
<div>
<h1>Response Renderer</h1>
<Markdown>{content}</Markdown>
</div>
);
});

const {container} = render(
<AiChat
adapter={adapterController!.adapter}
messageOptions={{responseRenderer}}
/>,
);
const textArea: HTMLTextAreaElement = container.querySelector('.nlux-comp-composer > textarea')!;
await waitForReactRenderCycle();

// Act
await userEvent.type(textArea, 'Hello{enter}');
await waitForReactRenderCycle();

adapterController!.resolve('**Yo!**');
await waitForReactRenderCycle();

// Assert
const responseElement = container.querySelector('.nlux-comp-chatItem--received');
expect(responseElement!.innerHTML).toEqual(
expect.stringContaining('<div class="nlux-markdown-container"><p><strong>Yo!</strong></p>\n</div>'),
);
});
});

0 comments on commit 6878631

Please sign in to comment.