Skip to content

Commit

Permalink
Add and fix new unit test to fix unit tests in Creator v2, #3027
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Jun 28, 2021
1 parent 6646c97 commit b5f71b6
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,13 @@ export class Base {
public iteratePropertiesHash(func: (hash: any, key: any) => void) {
var keys: any[] = [];
for (var key in this.propertyHash) {
if (key === "value" && this.isEditingSurveyElement) continue;
if (
key === "value" &&
this.isEditingSurveyElement &&
Array.isArray((<any>this).value)
)
continue;

keys.push(key);
}
keys.forEach((key) => func(this.propertyHash, key));
Expand Down
8 changes: 7 additions & 1 deletion src/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1169,9 +1169,15 @@ export class Question extends SurveyElement
return this.getUnbindValue(this.value);
}
protected getUnbindValue(value: any) {
if (this.isEditingSurveyElement) return value;
if (this.isValueSurveyElement(value)) return value;
return Helpers.getUnbindValue(value);
}
protected isValueSurveyElement(val: any): boolean {
if (!val) return false;
if (Array.isArray(val))
return val.length > 0 ? this.isValueSurveyElement(val[0]) : false;
return !!val.getType && !!val.onPropertyChanged;
}
private canClearValueAsInvisible(): boolean {
if (this.isVisible && this.isParentVisible) return false;
if (!!this.page && this.page.isStarted) return false;
Expand Down
5 changes: 4 additions & 1 deletion src/question_matrixdynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ export class QuestionMatrixDynamicModel extends QuestionMatrixDropdownModelBase
this.rowCountValue = 0;
super.unbindValue();
}
protected isValueSurveyElement(val: any): boolean {
return this.isEditingSurveyElement || super.isValueSurveyElement(val);
}
private addRowCore() {
var prevRowCount = this.rowCount;
this.rowCount = this.rowCount + 1;
Expand All @@ -359,7 +362,7 @@ export class QuestionMatrixDynamicModel extends QuestionMatrixDropdownModelBase
newValue = this.createNewValue();
}
if (
!this.isEditingSurveyElement &&
!this.isValueSurveyElement(newValue) &&
!Helpers.isTwoValueEquals(newValue[newValue.length - 1], row.value)
) {
newValue[newValue.length - 1] = row.value;
Expand Down
44 changes: 44 additions & 0 deletions tests/editingObjectTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { Serializer } from "../src/jsonobject";
import { ItemValue } from "../src/itemvalue";
import { QuestionMultipleTextModel } from "../src/question_multipletext";
import { QuestionMatrixModel } from "../src/question_matrix";
import { Question } from "../src/question";
import { QuestionCheckboxModel } from "../src/question_checkbox";

export default QUnit.module("Survey.editingObj Tests");

Expand Down Expand Up @@ -1113,3 +1115,45 @@ QUnit.test("Do not break reactive array in original object", function(assert) {
);
assert.notOk(colCountOnChanged["value"], "We do not iterate by value");
});

QUnit.test("Value property editor test", function(assert) {
var propertyGridValueJSON = {
name: "propertygrid_value",
showInToolbox: false,
questionJSON: {
type: "html",
html: "empty",
},
onValueChanged: (question: Question, name: string, newValue: any) => {
var displayValue = question.isEmpty()
? "empty"
: JSON.stringify(question.value);
question.contentQuestion.html = displayValue;
},
};

ComponentCollection.Instance.add(propertyGridValueJSON);
var question = new QuestionCheckboxModel("q1");
question.choices = [
{ value: 1, text: "Item 1" },
{ value: 2, text: "Item 2" },
];
var survey = new SurveyModel({
elements: [
{
type: "propertygrid_value",
name: "defaultValue",
},
],
});
survey.editingObj = question;
var editQuestion = survey.getQuestionByName("defaultValue");
var htmlQuestion = editQuestion.contentQuestion;
assert.equal(htmlQuestion.html, "empty");
question.defaultValue = [1, 2];
assert.equal(htmlQuestion.html, "[1,2]");
question.defaultValue = undefined;
assert.equal(htmlQuestion.html, "empty");

ComponentCollection.Instance.clear();
});

0 comments on commit b5f71b6

Please sign in to comment.