Question T16862
Visible to All Users

Unit test of custom question component

created a year ago

Hello,

While I am trying to test custom input component that I render as part of custom question output. I am getting an error in such test case

TypeError: Cannot set properties of undefined (setting 'renderCallback')

Are there any instructions of how to test it or how to create a Survey as a creator correctly?

JavaScript
import { Survey } from 'survey-react-ui'; test('renders component as an input inside survey form', () => { renderComponent({ creator: new Survey({}), }); expect(screen.getByTestId('input')).toBeInTheDocument(); });

Best regards,
Yaroslav

Answers approved by surveyjs Support

created a year ago (modified a year ago)

Hi Yaroslav,
From the provided code, I see that you initialize a SurveyModel with an empty survey definition. I'm afraid that a survey won't have any input fields if it doesn't receive any model.

Regarding the automated testing: Each survey question has the id property. The default property value is generated automatically and is assigned to the id attribute of the rendered HTML element.

Additionally, a question has an inputId property which is calculated based on the specified question id as follows: question.id + i. Survey questions may have multiple input fields. The id for each input field is generated as follows: question.inputId + "_" + choiceIndex (0 base).

Survey element id property values are generated automatically. You can get a list of survey questions using the survey.getAllQuestions function. Now, you can iterate through survey questions and get their id and inputId properties.

JavaScript
let allQuestions = survey.getAllQuestions(false, false, true); allQuestions.forEach(q => { console.log('Question ID: ' + q.id); console.log('Question input ID: ' + q.id); });

Also, you can programmatically modify id and input ID values using the following survey functions:

Within these events, you can obtain the rendered HTML Element using the options.htmlElement property and override a default question identifier using the options.htmlElement.id.

I hope that this information helps.

Please let me know if you require further assistance.