-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for <Markdown /> primitive
- Loading branch information
Showing
2 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
specs/specs/aiChat/primitives/markdown-syntaxHighlighter.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>'), | ||
); | ||
}); | ||
}); |