Question T5002
Visible to All Users

SurveyModel data not updating in unit tests when changing input field text

created 4 years ago (modified 4 years ago)

Angular 9, Jasmine & Karma

Essentially we're trying to validate that when we try to go to the next page without filling out a required field, an error message appears. The positive case is that when we fill out a field that was originally empty, it should let us through. We're successfully able to query the input element, and set the value - and then even try sending a new event to trigger and input change to potentially cause the surveymodel logic to call the onValueChanged etc… but that does not seem to be happening. Tried wrapping the test in async, as well as

Code
fixture.whenStable().then(() => { ... }

Code:

Code
it('should be able to navigate to next page once empty field is filled', () => { const firstNameInput = querySelector('.sv_qstn input[aria-label="First Name"]') firstNameInput.value = 'John' firstNameInput.dispatchEvent(new Event('input')); querySelector('.btn-navigation').click() fixture.detectChanges() // fail by default so I can debug expect(true).toBeFalsy() // expect(<some check that indicates on new page>) }) ... function querySelector(query) { return fixture.debugElement.nativeElement.querySelector(query) }

Tossed a breakpoint into the middle of the test and the input field content is changed, but doesn't look like that update propagated to the SurveyModel. Thoughts?

Answers approved by surveyjs Support

created 4 years ago

Hello,

The actual events SurveyJS subscribed to get changes to model can be found in our source code, in this repo - https://github.com/surveyjs/survey-library. For KnockoutJS (this core library is being used in survey-angular) it is the https://github.com/surveyjs/survey-library/blob/master/src/knockout/templates/question-text.html template file. As you can see from this file, the "input" event is used for value binding with the https://surveyjs.io/Documentation/Library?id=surveymodel#textUpdateMode option set to onTyping value.

Summarizing. You can try to set the https://surveyjs.io/Documentation/Library?id=surveymodel#textUpdateMode option to onTyping value. You can trigger the "blur" event instead of "input". Hope this helps.

Thanks, Serge
SurveyJS Team