11import React from 'react'
22import { afterEach , describe , expect , it , vi } from 'vitest'
33import { GenericJsxEditor , JsxComponentDescriptor , MDXEditor , MDXEditorMethods , jsxPlugin } from '../'
4- import { render , act } from '@testing-library/react'
4+ import { render , act , waitFor } from '@testing-library/react'
55
66// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
77; ( globalThis as any ) . IS_REACT_ACT_ENVIRONMENT = true
@@ -23,6 +23,12 @@ afterEach(() => {
2323 vi . restoreAllMocks ( )
2424} )
2525
26+ const flushEditorUpdates = async ( ) => {
27+ await act ( async ( ) => {
28+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) )
29+ } )
30+ }
31+
2632describe ( 'jsx markdown import export' , ( ) => {
2733 // produces a warning about act
2834 it . todo ( 'skips jsx import if not specified' , async ( ) => {
@@ -39,7 +45,7 @@ describe('jsx markdown import export', () => {
3945 expect ( processedMarkdown ) . toEqual ( markdown . trim ( ) )
4046 } )
4147
42- it ( 'routes capitalized jsx components sharing an html tag name to the jsx visitor' , ( ) => {
48+ it ( 'routes capitalized jsx components sharing an html tag name to the jsx visitor' , async ( ) => {
4349 const descriptors : JsxComponentDescriptor [ ] = [
4450 {
4551 name : 'Section' ,
@@ -52,6 +58,7 @@ describe('jsx markdown import export', () => {
5258 const { container } = render (
5359 < MDXEditor markdown = { `<Section>Section content</Section>` } plugins = { [ jsxPlugin ( { jsxComponentDescriptors : descriptors } ) ] } />
5460 )
61+ await flushEditorUpdates ( )
5562 expect ( container . querySelector ( 'section' ) ) . toBeNull ( )
5663 } )
5764
@@ -84,9 +91,13 @@ describe('jsx markdown import export', () => {
8491 ]
8592
8693 act ( ( ) => {
87- render ( < MDXEditor ref = { ref } markdown = { `<Wrapper><Section /></Wrapper>` } plugins = { [ jsxPlugin ( { jsxComponentDescriptors : descriptors } ) ] } /> )
94+ render (
95+ < MDXEditor ref = { ref } markdown = { `<Wrapper><Section /></Wrapper>` } plugins = { [ jsxPlugin ( { jsxComponentDescriptors : descriptors } ) ] } />
96+ )
97+ } )
98+ await waitFor ( ( ) => {
99+ expect ( ref . current ?. getMarkdown ( ) ?? '' ) . toContain ( `import { Wrapper, Section } from './components'` )
88100 } )
89- await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) )
90101
91102 const processedMarkdown = ref . current ?. getMarkdown ( ) ?? ''
92103
0 commit comments