I am building a React JS application using TypeScript. In my application, I am using TinyMCE editor in a form. I am also writing integration tests using Cypress in TypeScript. I am now having trouble setting the TinyMCE editor value in the Cypress using TypeScript. I am using this library to manipulate TinyMCE editor in Cypress, https://github.com/ForeachOS/cypress-tinymce.
This is my React JS TinyMCE editor code.
import React, { FC } from 'react'
import { Editor } from '@tinymce/tinymce-react'
interface RichEditorFieldProps {
id: string
name?: string
value?: string
onChange: (fieldName: string, value: string) => void
}
const RichEditorField: FC<RichEditorFieldProps> = (
props: RichEditorFieldProps
) => {
const getFieldName = () => {
return props.name ? props.name : props.id
}
return (
<div cy-data-id={props.id} className={'rich-editor-field-container'}>
<Editor
id={props.id}
apiKey={'zjf0nctg4gmjj0a5u5uxm7pcrs0ka4hwcuxzskhxh5hauugf'}
value={props.value ?? ''}
onEditorChange={(content) => {
props.onChange(getFieldName(), content)
}}
/>
</div>
)
}
I am rendering that component as follows:
<FormRichEditorField
value={formik.values.specification}
label={'Specification'}
error={
formik.touched.specification &&
formik.errors.specification
}
id={'specification'}
onChange={formik.setFieldValue}
/>
What I want to highlight is that the id of the editor is specification.
To write the test for that in Cypress, I installed the package I mentioned, https://github.com/ForeachOS/cypress-tinymce.
After I installed the package and import it into support/index.ts file, I cannot just call the function as follows:
cy.setTinyMceContent('specification', 'This is the new content');
I have to add the following code to make it compatible with TypeScript:
declare namespace Cypress {
interface Chainable<Subject = any> {
setTinyMceContent(tinyMceId: string, content: any): void
}
}
Then I set the value of the TinyMCE editor field as follow in my Cypress test:
cy.setTinyMceContent('specification', 'This is testing')
When I run the test, I am getting the following error:
Cannot read properties of undefined (reading 'specification')
What is wrong with my code and how can I fix it?
