Skip to content

Commit

Permalink
#7289 - unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksey Novikov committed Nov 27, 2023
1 parent 7fa4535 commit aa21ae8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/question_signaturepad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ export class QuestionSignaturePadModel extends QuestionFileModelBase {
this.canvas.toBlob((blob: Blob) => {
this.uploadFiles([new File([blob], this.name + "." + correctFormatData(this.dataFormat), { type: this.getFormat() })]);
this.valueWasChangedFromLastUpload = false;
}, this.dataFormat);
}, this.getFormat());
}, 100);
}
}
Expand Down
122 changes: 64 additions & 58 deletions tests/question_signaturepadtests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ QUnit.test("check allowClear", (assert) => {
const signaturepad = <QuestionSignaturePadModel>survey.getQuestionByName("q1");
assert.equal(signaturepad.allowClear, true, "allowClear");
assert.equal(signaturepad.readOnly, false, "readOnly");
assert.equal(signaturepad.canShowClearButton, false, "canShowClearButton");

signaturepad.valueWasChangedFromLastUpload = true;
assert.equal(signaturepad.canShowClearButton, true, "canShowClearButton");
signaturepad.valueWasChangedFromLastUpload = false;
assert.equal(signaturepad.canShowClearButton, false, "canShowClearButton");

signaturepad.value = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='100' width='100'%3E%3Ccircle cx='50' cy='50' r='40' /%3E%3C/svg%3E";
assert.equal(signaturepad.canShowClearButton, true, "canShowClearButton");

signaturepad.allowClear = false;
Expand Down Expand Up @@ -366,61 +374,59 @@ QUnit.test("check rendered size properties", (assert) => {
assert.equal(signaturepadQuestion.renderedCanvasWidth, "100%");
});

// QUnit.only("Question Signature upload files", function (assert) {
// var json = {
// questions: [
// {
// type: "signaturepad",
// name: "signature",
// storeDataAsText: false,
// },
// ],
// };

// var survey = new SurveyModel(json);
// var q1: QuestionSignaturePadModel = <any>survey.getQuestionByName("signature");
// var done = assert.async();

// var fileName;
// var fileType;
// var fileContent;
// survey.onUploadFiles.add((survey, options) => {
// let file = options.files[0];
// let fileReader = new FileReader();
// fileReader.onload = (e) => {
// fileName = file.name;
// fileType = file.type;
// fileContent = fileReader.result;
// };
// fileReader.readAsDataURL(file);

// setTimeout(
// () =>
// options.callback(
// "success",
// options.files.map((file) => {
// return { file: file, content: file.name + "_url" };
// })
// ),
// 2
// );
// });

// q1["signaturePad"].fromDataURL("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='100' width='100'%3E%3Ccircle cx='50' cy='50' r='40' /%3E%3C/svg%3E");
// q1.onBlur();

// survey.onValueChanged.add((survey, options) => {
// assert.equal(q1.value.length, 1, "2 file");
// assert.equal(
// q1.value[0].content,
// q1.value[0].name + "_url",
// "content"
// );

// done();

// assert.equal(fileType, "image/svg+xml");
// assert.equal(fileName, "signature.SVG");
// assert.equal(fileContent, "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='100' width='100'%3E%3Ccircle cx='50' cy='50' r='40' /%3E%3C/svg%3E");
// });
// });
QUnit.test("Question Signature upload files", function (assert) {
var json = {
questions: [
{
type: "signaturepad",
name: "signature",
dataFormat: "svg",
storeDataAsText: false,
},
],
};

var survey = new SurveyModel(json);
var q1: QuestionSignaturePadModel = <any>survey.getQuestionByName("signature");
var done = assert.async();

var fileName;
var fileType;
var fileContent;
survey.onUploadFiles.add((survey, options) => {
let file = options.files[0];
let fileReader = new FileReader();
fileReader.onload = (e) => {
fileName = file.name;
fileType = file.type;
fileContent = fileReader.result;
};
fileReader.readAsDataURL(file);
setTimeout(
() =>
options.callback(
"success",
options.files.map((file) => {
return { file: file, content: file.name + "_url" };
})
),
2
);
});

const el = document.createElement("div");
el.append(document.createElement("canvas"));
q1.afterRenderQuestionElement(el);
q1["signaturePad"].fromDataURL("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='100' width='100'%3E%3Ccircle cx='50' cy='50' r='40' /%3E%3C/svg%3E");
q1.valueWasChangedFromLastUpload = true;
q1.onBlur();

survey.onValueChanged.add((survey, options) => {
assert.equal(q1.value, "signature.svg_url");

assert.equal(fileType, "image/svg+xml");
assert.equal(fileName, "signature.svg");
//assert.equal(fileContent, "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='100' width='100'%3E%3Ccircle cx='50' cy='50' r='40' /%3E%3C/svg%3E");
done();
});
});

0 comments on commit aa21ae8

Please sign in to comment.