33 */
44import moment from 'moment/moment' ;
55import { TasksFile } from '../../src/Scripting/TasksFile' ;
6+ import { ListItem } from '../../src/Task/ListItem' ;
67import { Task } from '../../src/Task/Task' ;
78import { TaskLocation } from '../../src/Task/TaskLocation' ;
8- import { ListItem } from '../../src/Task/ListItem' ;
99import { TaskBuilder } from '../TestingTools/TaskBuilder' ;
1010import { fromLine } from '../TestingTools/TestHelpers' ;
1111import { createChildListItem } from './ListItemHelpers' ;
@@ -115,6 +115,8 @@ describe('list item parsing', () => {
115115 expect ( item . description ) . toEqual ( 'without checkbox' ) ;
116116 expect ( item . originalMarkdown ) . toEqual ( '- without checkbox' ) ;
117117 expect ( item . statusCharacter ) . toEqual ( null ) ;
118+ expect ( item . indentation ) . toEqual ( '' ) ;
119+ expect ( item . listMarker ) . toEqual ( '-' ) ;
118120 } ) ;
119121
120122 it ( 'should read a list item with checkbox' , ( ) => {
@@ -133,6 +135,20 @@ describe('list item parsing', () => {
133135 expect ( item . statusCharacter ) . toEqual ( 'x' ) ;
134136 } ) ;
135137
138+ it ( 'should read a list item with indentation' , ( ) => {
139+ const item = new ListItem ( ' - indented' , null , taskLocation ) ;
140+
141+ expect ( item . description ) . toEqual ( 'indented' ) ;
142+ expect ( item . indentation ) . toEqual ( ' ' ) ;
143+ } ) ;
144+
145+ it ( 'should read a list marker' , ( ) => {
146+ expect ( new ListItem ( '* xxx' , null , taskLocation ) . listMarker ) . toEqual ( '*' ) ;
147+ expect ( new ListItem ( '- xxx' , null , taskLocation ) . listMarker ) . toEqual ( '-' ) ;
148+ expect ( new ListItem ( '+ xxx' , null , taskLocation ) . listMarker ) . toEqual ( '+' ) ;
149+ expect ( new ListItem ( '2. xxx' , null , taskLocation ) . listMarker ) . toEqual ( '2.' ) ;
150+ } ) ;
151+
136152 it ( 'should accept a non list item' , ( ) => {
137153 // we tried making the constructor throw if given a non list item
138154 // but it broke lots of normal Task uses in the tests (TaskBuilder)
@@ -144,6 +160,32 @@ describe('list item parsing', () => {
144160 } ) ;
145161} ) ;
146162
163+ describe ( 'list item writing' , ( ) => {
164+ it ( 'should write a simple list item' , ( ) => {
165+ const item = new ListItem ( '- simple' , null , taskLocation ) ;
166+
167+ expect ( item . toFileLineString ( ) ) . toEqual ( '- simple' ) ;
168+ } ) ;
169+
170+ it ( 'should write a simple check list item' , ( ) => {
171+ const item = new ListItem ( '- [ ] simple checklist' , null , taskLocation ) ;
172+
173+ expect ( item . toFileLineString ( ) ) . toEqual ( '- [ ] simple checklist' ) ;
174+ } ) ;
175+
176+ it ( 'should write an indented list item' , ( ) => {
177+ const item = new ListItem ( ' - indented' , null , taskLocation ) ;
178+
179+ expect ( item . toFileLineString ( ) ) . toEqual ( ' - indented' ) ;
180+ } ) ;
181+
182+ it ( 'should write a list item with a different list marker' , ( ) => {
183+ const item = new ListItem ( '* star' , null , taskLocation ) ;
184+
185+ expect ( item . toFileLineString ( ) ) . toEqual ( '* star' ) ;
186+ } ) ;
187+ } ) ;
188+
147189describe ( 'related items' , ( ) => {
148190 it ( 'should detect if no closest parent task' , ( ) => {
149191 const task = fromLine ( { line : '- [ ] task' } ) ;
@@ -283,3 +325,56 @@ describe('checking if mixed lists are identical', () => {
283325 expect ( ListItem . listsAreIdentical ( list2 , list2 ) ) . toEqual ( true ) ;
284326 } ) ;
285327} ) ;
328+
329+ describe ( 'list item checking and unchecking' , ( ) => {
330+ it ( 'should create a checked list item' , ( ) => {
331+ const listItem = new ListItem (
332+ '- [ ] description' ,
333+ new ListItem ( '- [ ] parent' , null , taskLocation ) ,
334+ taskLocation ,
335+ ) ;
336+
337+ const checkedListItem = listItem . checkOrUncheck ( ) ;
338+
339+ expect ( checkedListItem . parent ) . toEqual ( null ) ;
340+ expect ( checkedListItem . taskLocation ) . toBe ( taskLocation ) ;
341+ expect ( checkedListItem . statusCharacter ) . toEqual ( 'x' ) ;
342+ expect ( checkedListItem . originalMarkdown ) . toEqual ( '- [x] description' ) ;
343+ } ) ;
344+
345+ it ( 'should create a checked list item and preserve the list marker' , ( ) => {
346+ const listItem = new ListItem ( '* [ ] check me' , null , taskLocation ) ;
347+
348+ const checkedListItem = listItem . checkOrUncheck ( ) ;
349+
350+ expect ( checkedListItem . statusCharacter ) . toEqual ( 'x' ) ;
351+ expect ( checkedListItem . originalMarkdown ) . toEqual ( '* [x] check me' ) ;
352+ } ) ;
353+
354+ it ( 'should create an unchecked list item' , ( ) => {
355+ const listItem = new ListItem ( '4. [#] uncheck me' , null , taskLocation ) ;
356+
357+ const checkedListItem = listItem . checkOrUncheck ( ) ;
358+
359+ expect ( checkedListItem . statusCharacter ) . toEqual ( ' ' ) ;
360+ expect ( checkedListItem . originalMarkdown ) . toEqual ( '4. [ ] uncheck me' ) ;
361+ } ) ;
362+
363+ it ( 'should preserve a non-checklist item' , ( ) => {
364+ const listItem = new ListItem ( '- no checkbox' , null , taskLocation ) ;
365+
366+ const newListItem = listItem . checkOrUncheck ( ) ;
367+
368+ expect ( newListItem . statusCharacter ) . toEqual ( null ) ;
369+ expect ( newListItem . originalMarkdown ) . toEqual ( '- no checkbox' ) ;
370+ } ) ;
371+
372+ it . failing ( 'should preserve a non-checklist item with checkbox-like string in description' , ( ) => {
373+ const listItem = new ListItem ( '- this looks like a checkbox [f]' , null , taskLocation ) ;
374+
375+ const newListItem = listItem . checkOrUncheck ( ) ;
376+
377+ expect ( newListItem . statusCharacter ) . toEqual ( null ) ;
378+ expect ( newListItem . originalMarkdown ) . toEqual ( '- this looks like a checkbox [f]' ) ;
379+ } ) ;
380+ } ) ;
0 commit comments