From 768ac161ef7af6d3387a4156a54c27c5bd04beae Mon Sep 17 00:00:00 2001 From: Stephan Probst Date: Mon, 27 Nov 2023 08:17:09 +0100 Subject: [PATCH] Fix paths in tests to work independent of OS. (#568) * Fix paths in tests to work independent of OS. The path strings caused the tests to fail on Windows. * Fixes priority and recurrence. Recurrence now does not create pri: attributes or due date. --- src/__tests__/main/CreateRecurringTodo.tsx | 14 +++-- src/__tests__/main/File.tsx | 53 ++++++++++--------- .../TodoObject/ChangeCompleteState.tsx | 20 +++---- .../TodoObject/CreateRecurringTodo.tsx | 8 +-- 4 files changed, 51 insertions(+), 44 deletions(-) diff --git a/src/__tests__/main/CreateRecurringTodo.tsx b/src/__tests__/main/CreateRecurringTodo.tsx index 87d86848..7e624c03 100644 --- a/src/__tests__/main/CreateRecurringTodo.tsx +++ b/src/__tests__/main/CreateRecurringTodo.tsx @@ -86,16 +86,22 @@ describe('Create recurring todos', () => { expect(fileContent.split('\n').pop()).toEqual(`${dateTodayString} Water plants @home +quick due:${dateInOneWeekString} t:${dateInOneWeekMinus10String} rec:1w`); }); - test('Should add a new todo adding a strict recurrence of one day to due date and threshold date, while the todo initially only has a threshold date', async () => { + test('Should add a new todo adding a strict recurrence of one day to threshold date. No due date should be created.', async () => { const recurringTodo = await createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Line 1 rec:+1d t:2023-09-19`, '1+d'); const fileContent = await fs.readFile('./src/__tests__/__mock__/recurrence.txt', 'utf8'); - expect(fileContent.split('\n').pop()).toEqual(`${dateTodayString} Line 1 rec:+1d t:2023-09-20 due:${dateTomorrowString}`); + expect(fileContent.split('\n').pop()).toEqual(`${dateTodayString} Line 1 rec:+1d t:2023-09-20`); }); - test('Should add a new todo and set the priority to the value of the pri extension', async () => { + test('Should add a new todo and preserve the priority in the pri extension, but should not set the priority (A) for the task.', async () => { const recurringTodo = await createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Line 1 rec:1d pri:A`, '1d'); const fileContent = await fs.readFile('./src/__tests__/__mock__/recurrence.txt', 'utf8'); - expect(fileContent.split('\n').pop()).toEqual(`(A) ${dateTodayString} Line 1 rec:1d pri:A due:${dateTomorrowString}`); + expect(fileContent.split('\n').pop()).toEqual(`${dateTodayString} Line 1 rec:1d pri:A due:${dateTomorrowString}`); + }); + + test('Should add a new todo based on a daily recurrence and a threshold date set for today, without unwanted due date and priority labels', async () => { + const recurringTodo = await createRecurringTodo(`x ${dateTodayString} ${dateTodayString} (A) Do something rec:d t:${dateTodayString} @SomeContext`, 'd'); + const fileContent = await fs.readFile('./src/__tests__/__mock__/recurrence.txt', 'utf8'); + expect(fileContent.split('\n').pop()).toEqual(`${dateTodayString} (A) Do something rec:d t:${dateTomorrowString} @SomeContext`); }); }); \ No newline at end of file diff --git a/src/__tests__/main/File.tsx b/src/__tests__/main/File.tsx index f4deb83f..58067935 100644 --- a/src/__tests__/main/File.tsx +++ b/src/__tests__/main/File.tsx @@ -1,12 +1,13 @@ +import path from 'path' import { configStorage } from '../../main/config'; import { addFile, removeFile, setFile } from '../../main/modules/File/File'; jest.mock('../../main/config', () => ({ configStorage: { get: jest.fn().mockReturnValue([ - { active: false, todoFileName: 'test1.txt', todoFilePath: '/path/to/test1.txt', todoFileBookmark: null, doneFilePath: '/path/to/done.txt', doneFileBookmark: null }, - { active: true, todoFileName: 'test2.txt', todoFilePath: '/path/to/test2.txt', todoFileBookmark: null, doneFilePath: '/path/to/done.txt', doneFileBookmark: null }, - { active: false, todoFileName: 'test3.txt', todoFilePath: '/path/to/test3.txt', todoFileBookmark: null, doneFilePath: '/path/to/done.txt', doneFileBookmark: null }, + { active: false, todoFileName: 'test1.txt', todoFilePath: path.join('/', 'path', 'to', 'test1.txt'), todoFileBookmark: null, doneFilePath: path.join('/', 'path', 'to', 'done.txt'), doneFileBookmark: null }, + { active: true, todoFileName: 'test2.txt', todoFilePath: path.join('/', 'path', 'to', 'test2.txt'), todoFileBookmark: null, doneFilePath: path.join('/', 'path', 'to', 'done.txt'), doneFileBookmark: null }, + { active: false, todoFileName: 'test3.txt', todoFilePath: path.join('/', 'path', 'to', 'test3.txt'), todoFileBookmark: null, doneFilePath: path.join('/', 'path', 'to', 'done.txt'), doneFileBookmark: null }, ]), set: jest.fn(), }, @@ -20,39 +21,39 @@ describe('File functions', () => { }); test('addFile should add a new file to the config storage', async () => { - await addFile('/path/to/test4.txt', null); + await addFile(path.join('/', 'path', 'to', 'test4.txt'), null); expect(configStorage.set).toHaveBeenCalledTimes(1); expect(configStorage.set).toHaveBeenCalledWith('files', [ { active: false, todoFileName: 'test1.txt', - todoFilePath: '/path/to/test1.txt', + todoFilePath: path.join('/', 'path', 'to', 'test1.txt'), todoFileBookmark: null, - doneFilePath: '/path/to/done.txt', + doneFilePath: path.join('/', 'path', 'to', 'done.txt'), doneFileBookmark: null }, { active: false, todoFileName: 'test2.txt', - todoFilePath: '/path/to/test2.txt', + todoFilePath: path.join('/', 'path', 'to', 'test2.txt'), todoFileBookmark: null, - doneFilePath: '/path/to/done.txt', + doneFilePath: path.join('/', 'path', 'to', 'done.txt'), doneFileBookmark: null }, { active: false, todoFileName: 'test3.txt', - todoFilePath: '/path/to/test3.txt', + todoFilePath: path.join('/', 'path', 'to', 'test3.txt'), todoFileBookmark: null, - doneFilePath: '/path/to/done.txt', + doneFilePath: path.join('/', 'path', 'to', 'done.txt'), doneFileBookmark: null }, { active: true, todoFileName: 'test4.txt', - todoFilePath: '/path/to/test4.txt', + todoFilePath: path.join('/', 'path', 'to', 'test4.txt'), todoFileBookmark: null, - doneFilePath: '/path/to/done.txt', + doneFilePath: path.join('/', 'path', 'to', 'done.txt'), doneFileBookmark: null }, ]); @@ -64,25 +65,25 @@ describe('File functions', () => { { active: false, todoFileName: 'test1.txt', - todoFilePath: '/path/to/test1.txt', + todoFilePath: path.join('/', 'path', 'to', 'test1.txt'), todoFileBookmark: null, - doneFilePath: '/path/to/done.txt', + doneFilePath: path.join('/', 'path', 'to', 'done.txt'), doneFileBookmark: null }, { active: false, todoFileName: 'test3.txt', - todoFilePath: '/path/to/test3.txt', + todoFilePath: path.join('/', 'path', 'to', 'test3.txt'), todoFileBookmark: null, - doneFilePath: '/path/to/done.txt', + doneFilePath: path.join('/', 'path', 'to', 'done.txt'), doneFileBookmark: null }, { active: true, todoFileName: 'test4.txt', - todoFilePath: '/path/to/test4.txt', + todoFilePath: path.join('/', 'path', 'to', 'test4.txt'), todoFileBookmark: null, - doneFilePath: '/path/to/done.txt', + doneFilePath: path.join('/', 'path', 'to', 'done.txt'), doneFileBookmark: null }, ]); @@ -94,17 +95,17 @@ describe('File functions', () => { { active: true, todoFileName: 'test1.txt', - todoFilePath: '/path/to/test1.txt', + todoFilePath: path.join('/', 'path', 'to', 'test1.txt'), todoFileBookmark: null, - doneFilePath: '/path/to/done.txt', + doneFilePath: path.join('/', 'path', 'to', 'done.txt'), doneFileBookmark: null }, { active: false, todoFileName: 'test3.txt', - todoFilePath: '/path/to/test3.txt', + todoFilePath: path.join('/', 'path', 'to', 'test3.txt'), todoFileBookmark: null, - doneFilePath: '/path/to/done.txt', + doneFilePath: path.join('/', 'path', 'to', 'done.txt'), doneFileBookmark: null }, ]); @@ -116,17 +117,17 @@ describe('File functions', () => { { active: false, todoFileName: 'test1.txt', - todoFilePath: '/path/to/test1.txt', + todoFilePath: path.join('/', 'path', 'to', 'test1.txt'), todoFileBookmark: null, - doneFilePath: '/path/to/done.txt', + doneFilePath: path.join('/', 'path', 'to', 'done.txt'), doneFileBookmark: null }, { active: true, todoFileName: 'test3.txt', - todoFilePath: '/path/to/test3.txt', + todoFilePath: path.join('/', 'path', 'to', 'test3.txt'), todoFileBookmark: null, - doneFilePath: '/path/to/done.txt', + doneFilePath: path.join('/', 'path', 'to', 'done.txt'), doneFileBookmark: null }, ]); diff --git a/src/main/modules/TodoObject/ChangeCompleteState.tsx b/src/main/modules/TodoObject/ChangeCompleteState.tsx index 870a3fb9..ed4f8dbf 100644 --- a/src/main/modules/TodoObject/ChangeCompleteState.tsx +++ b/src/main/modules/TodoObject/ChangeCompleteState.tsx @@ -10,6 +10,16 @@ async function changeCompleteState(todoString: string, state: boolean): Promise< JsTodoTxtObject.setCreated(JsTodoTxtObject.created() ? JsTodoTxtObject.created() : new Date()); JsTodoTxtObject.setCompleted(new Date()); + const recurrence = JsTodoTxtObject?.extensions().find((item) => item.key === 'rec'); + if (recurrence?.value) { + try { + const response = await createRecurringTodo(JsTodoTxtObject.toString(), recurrence.value); + console.log('changeCompleteState.ts:', response); + } catch (error: any) { + console.error(error); + } + } + const currentPriority = JsTodoTxtObject.priority(); if(currentPriority) { JsTodoTxtObject.setPriority(null) @@ -21,16 +31,6 @@ async function changeCompleteState(todoString: string, state: boolean): Promise< restorePreviousPriority(JsTodoTxtObject); } - const recurrence = JsTodoTxtObject?.extensions().find((item) => item.key === 'rec'); - if (state && recurrence?.value) { - try { - const response = await createRecurringTodo(JsTodoTxtObject.toString(), recurrence.value); - console.log('changeCompleteState.ts:', response); - } catch (error: any) { - console.error(error); - } - } - const updatedTodoString = JsTodoTxtObject.toString(); return Promise.resolve(updatedTodoString); diff --git a/src/main/modules/TodoObject/CreateRecurringTodo.tsx b/src/main/modules/TodoObject/CreateRecurringTodo.tsx index 689c7f29..52782f1c 100644 --- a/src/main/modules/TodoObject/CreateRecurringTodo.tsx +++ b/src/main/modules/TodoObject/CreateRecurringTodo.tsx @@ -1,7 +1,6 @@ import { Item } from 'jstodotxt'; import dayjs from 'dayjs'; import { writeTodoObjectToFile } from '../File/Write'; -import restorePreviousPriority from './RestorePreviousPriority'; import { configStorage } from '../../config'; enum RecurrenceInterval { @@ -53,8 +52,6 @@ const createRecurringTodo = async (string: string, recurrence: string): Promise< JsTodoTxtObject.setCreated(null); } - restorePreviousPriority(JsTodoTxtObject); - if (recurrence && completedDate) { const strictRecurrence: boolean = recurrence.startsWith('+'); const recurrenceInterval: any = strictRecurrence ? recurrence.slice(1) : recurrence; @@ -67,7 +64,10 @@ const createRecurringTodo = async (string: string, recurrence: string): Promise< const newThresholdDate = strictRecurrence ? addRecurrenceToDate(dayjs(oldThresholdDate).toDate(), recurrenceInterval) : dayjs(newDueDate).subtract(daysBetween, 'day').toDate(); - if(completedDate) JsTodoTxtObject.setExtension('due', dayjs(newDueDate).format('YYYY-MM-DD')); + + // If the user only uses threshold date and no due date, the recurrence should not create a due date: + const recurrenceOnlyForThresholdDate = oldThresholdDate && !oldDueDate; + if(completedDate && !recurrenceOnlyForThresholdDate) JsTodoTxtObject.setExtension('due', dayjs(newDueDate).format('YYYY-MM-DD')); if(oldThresholdDate) JsTodoTxtObject.setExtension('t', dayjs(newThresholdDate).format('YYYY-MM-DD')); JsTodoTxtObject.setComplete(false); JsTodoTxtObject.setCompleted(null);