@@ -3,6 +3,7 @@ import { JsonObject } from "../../src/jsonobject";
33import { InputMaskDateTime , getDateTimeLexems } from "../../src/mask/mask_datetime" ;
44import { QuestionTextModel } from "../../src/question_text" ;
55import { SurveyModel } from "../../src/survey" ;
6+ import { settings } from "../../src/settings" ;
67
78export default QUnit . module ( "Datetime mask" ) ;
89
@@ -1353,7 +1354,6 @@ QUnit.test("Mask datetime with defaultValue includes seconds, #10820", function
13531354 const q1 = < QuestionTextModel > survey . getQuestionByName ( "q1" ) ;
13541355 assert . equal ( q1 . inputValue , "09/04/2024 12:34:56" ) ;
13551356} ) ;
1356-
13571357QUnit . test ( "Mask datetime with defaultValueExpression today() and saveMaskedValue, Bug#11158" , function ( assert ) {
13581358 function todayMock ( ) {
13591359 return new Date ( 2025 , 3 , 10 ) ;
@@ -1377,4 +1377,44 @@ QUnit.test("Mask datetime with defaultValueExpression today() and saveMaskedValu
13771377 assert . equal ( q1 . inputValue , "10.04.2025" , "inputValue is masked" ) ;
13781378 assert . equal ( q1 . value , "10.04.2025" , "value is saved as masked" ) ;
13791379 FunctionFactory . Instance . unregister ( "todayMock" ) ;
1380+ } ) ;
1381+ QUnit . test ( "Age function with datetime mask and saveMaskedValue, #11157" , function ( assert ) {
1382+ const savedOnDateCreated = settings . onDateCreated ;
1383+ settings . onDateCreated = ( newDate : Date , reason : string , val : any ) : Date => {
1384+ if ( ! val ) {
1385+ return new Date ( 2025 , 3 , 10 ) ; // April 10, 2025 as "today"
1386+ }
1387+ return newDate ;
1388+ } ;
1389+ const survey = new SurveyModel ( {
1390+ elements : [
1391+ {
1392+ type : "text" ,
1393+ name : "Date of Birth" ,
1394+ valueName : "patient_dob" ,
1395+ maskType : "datetime" ,
1396+ maskSettings : {
1397+ saveMaskedValue : true ,
1398+ pattern : "mm-dd-yyyy"
1399+ }
1400+ } ,
1401+ {
1402+ type : "expression" ,
1403+ name : "Age" ,
1404+ expression : "age({patient_dob})"
1405+ }
1406+ ]
1407+ } ) ;
1408+ const dobQuestion = < QuestionTextModel > survey . getQuestionByName ( "Date of Birth" ) ;
1409+ const ageQuestion = survey . getQuestionByName ( "Age" ) ;
1410+ dobQuestion . inputValue = "01-15-1990" ;
1411+ assert . equal ( dobQuestion . value , "01-15-1990" , "DOB value is stored in masked format" ) ;
1412+ assert . equal ( ageQuestion . value , 35 , "Age should be calculated as 35 via inputValue" ) ;
1413+ survey . setValue ( "patient_dob" , "01-15-2000" ) ;
1414+ assert . equal ( ageQuestion . value , 25 , "Age should be calculated as 25 via setValue masked" ) ;
1415+ survey . data = { patient_dob : "01-15-2005" } ;
1416+ assert . equal ( ageQuestion . value , 20 , "Age should be calculated as 20 via survey.data masked" ) ;
1417+ survey . data = { patient_dob : "1990-01-15" } ;
1418+ assert . equal ( ageQuestion . value , 35 , "Age should be calculated as 35 via survey.data ISO format" ) ;
1419+ settings . onDateCreated = savedOnDateCreated ;
13801420} ) ;
0 commit comments