Skip to content

Commit

Permalink
Fix paths in tests to work independent of OS. (#568)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
stephprobst authored Nov 27, 2023
1 parent 727eda9 commit 768ac16
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 44 deletions.
14 changes: 10 additions & 4 deletions src/__tests__/main/CreateRecurringTodo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
});

});
53 changes: 27 additions & 26 deletions src/__tests__/main/File.tsx
Original file line number Diff line number Diff line change
@@ -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(),
},
Expand All @@ -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
},
]);
Expand All @@ -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
},
]);
Expand All @@ -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
},
]);
Expand All @@ -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
},
]);
Expand Down
20 changes: 10 additions & 10 deletions src/main/modules/TodoObject/ChangeCompleteState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/main/modules/TodoObject/CreateRecurringTodo.tsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 768ac16

Please sign in to comment.