@@ -1018,107 +1018,63 @@ describe('toggle done', () => {
1018
1018
} ) ;
1019
1019
} ) ;
1020
1020
1021
- describe ( 'set correct created date on reccurence task' , ( ) => {
1022
- it ( 'does not set created date with disabled setting' , ( ) => {
1023
- // Arrange
1024
- const line = '- [ ] this is a task 📅 2021-09-12 🔁 every day' ;
1025
- updateSettings ( { setCreatedDate : false } ) ;
1026
-
1027
- // Act
1028
- const task = fromLine ( {
1029
- line,
1030
- } ) ;
1031
-
1032
- // Assert
1033
- expect ( task ) . not . toBeNull ( ) ;
1034
- expect ( task ! . createdDate ) . toBeNull ( ) ;
1035
-
1036
- const tasks = task ! . toggle ( ) ;
1037
- expect ( tasks . length ) . toEqual ( 2 ) ;
1038
- const nextTask : Task = tasks [ 0 ] ;
1039
- expect ( nextTask . createdDate ) . toBeNull ( ) ;
1021
+ describe ( 'created dates on recurring task' , ( ) => {
1022
+ beforeEach ( ( ) => {
1023
+ jest . useFakeTimers ( ) ;
1024
+ jest . setSystemTime ( new Date ( '2023-03-08' ) ) ;
1025
+ } ) ;
1040
1026
1041
- // cleanup
1027
+ afterEach ( ( ) => {
1028
+ jest . useRealTimers ( ) ;
1042
1029
resetSettings ( ) ;
1043
1030
} ) ;
1044
1031
1045
- it ( 'does not set created date with disabled setting when repeated has created date ' , ( ) => {
1032
+ it ( 'should not set created date with disabled setting' , ( ) => {
1046
1033
// Arrange
1047
- const line = '- [ ] this is a task ➕ 2021-09-11 📅 2021-09-12 🔁 every day ' ;
1034
+ const line = '- [ ] this is a task 🔁 every day 📅 2021-09-12' ;
1048
1035
updateSettings ( { setCreatedDate : false } ) ;
1049
1036
1050
1037
// Act
1051
- const task = fromLine ( {
1052
- line,
1053
- } ) ;
1054
-
1055
- // Assert
1056
- expect ( task ) . not . toBeNull ( ) ;
1057
- expect ( task ! . createdDate ) . not . toBeNull ( ) ;
1058
- expect ( task ! . createdDate ! . isSame ( moment ( '2021-09-11' , 'YYYY-MM-DD' ) ) ) . toStrictEqual ( true ) ;
1038
+ expect ( line ) . toToggleTo ( [
1039
+ '- [ ] this is a task 🔁 every day 📅 2021-09-13' ,
1040
+ '- [x] this is a task 🔁 every day 📅 2021-09-12 ✅ 2023-03-08' ,
1041
+ ] ) ;
1042
+ } ) ;
1059
1043
1060
- const tasks = task ! . toggle ( ) ;
1061
- expect ( tasks . length ) . toEqual ( 2 ) ;
1062
- const nextTask : Task = tasks [ 0 ] ;
1063
- expect ( nextTask . createdDate ) . toBeNull ( ) ;
1044
+ it ( 'should not set created date if setting disabled, even if original has created date' , ( ) => {
1045
+ // Arrange
1046
+ const line = '- [ ] this is a task 🔁 every day ➕ 2021-09-11 📅 2021-09-12' ;
1047
+ updateSettings ( { setCreatedDate : false } ) ;
1064
1048
1065
- // cleanup
1066
- resetSettings ( ) ;
1049
+ // Act
1050
+ expect ( line ) . toToggleTo ( [
1051
+ '- [ ] this is a task 🔁 every day 📅 2021-09-13' ,
1052
+ '- [x] this is a task 🔁 every day ➕ 2021-09-11 📅 2021-09-12 ✅ 2023-03-08' ,
1053
+ ] ) ;
1067
1054
} ) ;
1068
1055
1069
- it ( 'set created date with enabled setting' , ( ) => {
1056
+ it ( 'should set created date if setting enabled ' , ( ) => {
1070
1057
// Arrange
1071
- const today = '2023-03-08' ;
1072
- const todaySpy = jest . spyOn ( Date , 'now' ) . mockReturnValue ( moment ( today ) . valueOf ( ) ) ;
1073
- const line = '- [ ] this is a task 📅 2021-09-12 🔁 every day' ;
1058
+ const line = '- [ ] this is a task 🔁 every day 📅 2021-09-12' ;
1074
1059
updateSettings ( { setCreatedDate : true } ) ;
1075
1060
1076
1061
// Act
1077
- const task = fromLine ( {
1078
- line,
1079
- } ) ;
1080
-
1081
- // Assert
1082
- expect ( task ) . not . toBeNull ( ) ;
1083
- expect ( task ! . createdDate ) . toBeNull ( ) ;
1084
-
1085
- const tasks = task ! . toggle ( ) ;
1086
- expect ( tasks . length ) . toEqual ( 2 ) ;
1087
- const nextTask : Task = tasks [ 0 ] ;
1088
- expect ( nextTask . createdDate ) . not . toBeNull ( ) ;
1089
- expect ( nextTask ! . createdDate ! . isSame ( moment ( today , 'YYYY-MM-DD' ) ) ) . toStrictEqual ( true ) ;
1090
-
1091
- // cleanup
1092
- resetSettings ( ) ;
1093
- todaySpy . mockClear ( ) ;
1062
+ expect ( line ) . toToggleTo ( [
1063
+ '- [ ] this is a task 🔁 every day ➕ 2023-03-08 📅 2021-09-13' ,
1064
+ '- [x] this is a task 🔁 every day 📅 2021-09-12 ✅ 2023-03-08' ,
1065
+ ] ) ;
1094
1066
} ) ;
1095
1067
1096
- it ( 'set created date with enabled setting when repeated has created date' , ( ) => {
1068
+ it ( 'should set created date if setting enabled, when original has created date' , ( ) => {
1097
1069
// Arrange
1098
- const today = '2023-03-08' ;
1099
- const todaySpy = jest . spyOn ( Date , 'now' ) . mockReturnValue ( moment ( today ) . valueOf ( ) ) ;
1100
- const line = '- [ ] this is a task ➕ 2021-09-11 📅 2021-09-12 🔁 every day' ;
1070
+ const line = '- [ ] this is a task 🔁 every day ➕ 2021-09-11 📅 2021-09-12' ;
1101
1071
updateSettings ( { setCreatedDate : true } ) ;
1102
1072
1103
1073
// Act
1104
- const task = fromLine ( {
1105
- line,
1106
- } ) ;
1107
-
1108
- // Assert
1109
- expect ( task ) . not . toBeNull ( ) ;
1110
- expect ( task ! . createdDate ) . not . toBeNull ( ) ;
1111
- expect ( task ! . createdDate ! . isSame ( moment ( '2021-09-11' , 'YYYY-MM-DD' ) ) ) . toStrictEqual ( true ) ;
1112
-
1113
- const tasks = task ! . toggle ( ) ;
1114
- expect ( tasks . length ) . toEqual ( 2 ) ;
1115
- const nextTask : Task = tasks [ 0 ] ;
1116
- expect ( nextTask . createdDate ) . not . toBeNull ( ) ;
1117
- expect ( nextTask ! . createdDate ! . isSame ( moment ( today , 'YYYY-MM-DD' ) ) ) . toStrictEqual ( true ) ;
1118
-
1119
- // cleanup
1120
- resetSettings ( ) ;
1121
- todaySpy . mockClear ( ) ;
1074
+ expect ( line ) . toToggleTo ( [
1075
+ '- [ ] this is a task 🔁 every day ➕ 2023-03-08 📅 2021-09-13' ,
1076
+ '- [x] this is a task 🔁 every day ➕ 2021-09-11 📅 2021-09-12 ✅ 2023-03-08' ,
1077
+ ] ) ;
1122
1078
} ) ;
1123
1079
} ) ;
1124
1080
@@ -1134,19 +1090,9 @@ describe('order of recurring tasks', () => {
1134
1090
resetSettings ( ) ;
1135
1091
} ) ;
1136
1092
1137
- function togglingInUsersOrderShouldGive ( line : string , expectedLines : string [ ] ) {
1138
- // Arrange
1139
- const task = fromLine ( { line : line } ) ;
1140
-
1141
- // Act
1142
- const lines = task . toggleWithRecurrenceInUsersOrder ( ) . map ( ( t ) => t . toFileLineString ( ) ) ;
1143
-
1144
- // Assert
1145
- expect ( lines ) . toStrictEqual ( expectedLines ) ;
1146
- }
1147
-
1148
1093
it ( 'should put new task before old, by default' , ( ) => {
1149
- togglingInUsersOrderShouldGive ( '- [ ] this is a recurring task 🔁 every day' , [
1094
+ const line = '- [ ] this is a recurring task 🔁 every day' ;
1095
+ expect ( line ) . toToggleWithRecurrenceInUsersOrderTo ( [
1150
1096
'- [ ] this is a recurring task 🔁 every day' ,
1151
1097
'- [x] this is a recurring task 🔁 every day ✅ 2023-05-16' ,
1152
1098
] ) ;
@@ -1155,7 +1101,8 @@ describe('order of recurring tasks', () => {
1155
1101
it ( 'should honour new-task-before-old setting' , ( ) => {
1156
1102
updateSettings ( { recurrenceOnNextLine : false } ) ;
1157
1103
1158
- togglingInUsersOrderShouldGive ( '- [ ] this is a recurring task 🔁 every day' , [
1104
+ const line = '- [ ] this is a recurring task 🔁 every day' ;
1105
+ expect ( line ) . toToggleWithRecurrenceInUsersOrderTo ( [
1159
1106
'- [ ] this is a recurring task 🔁 every day' ,
1160
1107
'- [x] this is a recurring task 🔁 every day ✅ 2023-05-16' ,
1161
1108
] ) ;
@@ -1164,52 +1111,14 @@ describe('order of recurring tasks', () => {
1164
1111
it ( 'should honour old-task-before-new setting' , ( ) => {
1165
1112
updateSettings ( { recurrenceOnNextLine : true } ) ;
1166
1113
1167
- togglingInUsersOrderShouldGive ( '- [ ] this is a recurring task 🔁 every day' , [
1114
+ const line = '- [ ] this is a recurring task 🔁 every day' ;
1115
+ expect ( line ) . toToggleWithRecurrenceInUsersOrderTo ( [
1168
1116
'- [x] this is a recurring task 🔁 every day ✅ 2023-05-16' ,
1169
1117
'- [ ] this is a recurring task 🔁 every day' ,
1170
1118
] ) ;
1171
1119
} ) ;
1172
1120
} ) ;
1173
1121
1174
- declare global {
1175
- namespace jest {
1176
- interface Matchers < R > {
1177
- toBeIdenticalTo ( builder2 : TaskBuilder ) : R ;
1178
- }
1179
-
1180
- interface Expect {
1181
- toBeIdenticalTo ( builder2 : TaskBuilder ) : any ;
1182
- }
1183
-
1184
- interface InverseAsymmetricMatchers {
1185
- toBeIdenticalTo ( builder2 : TaskBuilder ) : any ;
1186
- }
1187
- }
1188
- }
1189
-
1190
- export function toBeIdenticalTo ( builder1 : TaskBuilder , builder2 : TaskBuilder ) {
1191
- const task1 = builder1 . build ( ) ;
1192
- const task2 = builder2 . build ( ) ;
1193
- const pass = task1 . identicalTo ( task2 ) ;
1194
-
1195
- if ( pass ) {
1196
- return {
1197
- message : ( ) => 'Tasks treated as identical, but should be different' ,
1198
- pass : true ,
1199
- } ;
1200
- }
1201
- return {
1202
- message : ( ) => {
1203
- return 'Tasks should be identical, but are treated as different' ;
1204
- } ,
1205
- pass : false ,
1206
- } ;
1207
- }
1208
-
1209
- expect . extend ( {
1210
- toBeIdenticalTo,
1211
- } ) ;
1212
-
1213
1122
describe ( 'identicalTo' , ( ) => {
1214
1123
it ( 'should check status' , ( ) => {
1215
1124
const lhs = new TaskBuilder ( ) . status ( Status . TODO ) ;
0 commit comments