Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ransome1 committed Apr 2, 2024
1 parent 97864c9 commit 8ee6152
Show file tree
Hide file tree
Showing 25 changed files with 458 additions and 458 deletions.
28 changes: 14 additions & 14 deletions src/__tests__/__mock__/recurrence.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

2024-04-01 Line 1 rec:1d due:2024-04-02
2024-04-01 Line 1 rec:w due:2024-04-08
2024-04-01 Line 1 rec:2m due:2024-06-01
2024-04-01 Line 1 rec:+1d due:2024-04-03
2024-04-01 Line 1 rec:7w due:2024-05-20
2024-04-01 Line 1 due:2023-07-24 rec:+1b
2024-04-01 taxes are due in one year t:2022-03-30 due:2022-04-30 rec:+1y
2024-04-01 Water plants @home +quick due:2024-04-08 t:2024-03-29 rec:1w
2024-04-01 Line 1 rec:+1d t:2023-09-20
2024-04-01 Line 1 rec:1d pri:A due:2024-04-02
2024-04-01 (A) Do something rec:d t:2024-04-02 @SomeContext
2024-04-01 Do something rec:0d
2024-04-01 Do something rec:0d due:2024-04-01
2024-04-01 Do something rec:0d due:2024-04-01 t:2024-04-01
2024-04-02 Line 1 rec:1d due:2024-04-03
2024-04-02 Line 1 rec:w due:2024-04-09
2024-04-02 Line 1 rec:2m due:2024-06-02
2024-04-02 Line 1 rec:+1d due:2024-04-04
2024-04-02 Line 1 rec:7w due:2024-05-21
2024-04-02 Line 1 due:2023-07-24 rec:+1b
2024-04-02 taxes are due in one year t:2022-03-30 due:2022-04-30 rec:+1y
2024-04-02 Water plants @home +quick due:2024-04-09 t:2024-03-30 rec:1w
2024-04-02 Line 1 rec:+1d t:2023-09-20
2024-04-02 Line 1 rec:1d pri:A due:2024-04-03
2024-04-02 (A) Do something rec:d t:2024-04-03 @SomeContext
2024-04-02 Do something rec:0d
2024-04-02 Do something rec:0d due:2024-04-02
2024-04-02 Do something rec:0d due:2024-04-02 t:2024-04-02
13 changes: 10 additions & 3 deletions src/__tests__/__mock__/test.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
Line 1
Line 2
Line 3
New line
Edited line
Multi line 1Multi line 2Multi line 3
Updated line
Append 1
Append 2
New line with relative threshold date t:June 3rd, 2005
Line4
Line5
Line6
Multi line 1Multi line 2Multi line 3
24 changes: 12 additions & 12 deletions src/__tests__/main/Archive.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { archiveTodos } from '../../main/modules/File/Archive';
import fs from 'fs/promises';
import fs from 'fs';

jest.mock('electron', () => ({
app: {
Expand Down Expand Up @@ -33,22 +33,22 @@ jest.mock('../../main/modules/Menu', () => ({
}));

describe('Archiving', () => {
beforeEach(async() => {
beforeEach(() => {
jest.clearAllMocks();
await fs.writeFile('./src/__tests__/__mock__/archiving.txt', `Unfinished todo 1\nx 2022-02-01 Finished todo 3\nUnfinished todo 2\nx 2022-02-08 Finished todo 1\nUnfinished todo 3\nx 2022-02-17 Finished todo 2`, 'utf8');
await fs.writeFile('./src/__tests__/__mock__/done.txt', `x 2022-02-02 todo from done.txt 1\nx 2022-02-03 todo from done.txt 2\nx 2022-02-04 todo from done.txt 3\nx 2022-02-05 todo from done.txt 4`, 'utf8');
fs.writeFileSync('./src/__tests__/__mock__/archiving.txt', `Unfinished todo 1\nx 2022-02-01 Finished todo 3\nUnfinished todo 2\nx 2022-02-08 Finished todo 1\nUnfinished todo 3\nx 2022-02-17 Finished todo 2`, 'utf8');
fs.writeFileSync('./src/__tests__/__mock__/done.txt', `x 2022-02-02 todo from done.txt 1\nx 2022-02-03 todo from done.txt 2\nx 2022-02-04 todo from done.txt 3\nx 2022-02-05 todo from done.txt 4`, 'utf8');
});

afterAll(async () => {
await fs.unlink('./src/__tests__/__mock__/archiving.txt');
await fs.unlink('./src/__tests__/__mock__/done.txt');
afterAll(() => {
fs.unlinkSync('./src/__tests__/__mock__/archiving.txt');
fs.unlinkSync('./src/__tests__/__mock__/done.txt');
});

test('Should collect data from todo and done file and merge it properly', async () => {
await archiveTodos();
const fileContent = await fs.readFile('./src/__tests__/__mock__/done.txt', 'utf8');
test('Should collect data from todo and done file and merge it properly', () => {
archiveTodos();
const fileContent = fs.readFileSync('./src/__tests__/__mock__/done.txt', 'utf8');
const expectedContent = `x 2022-02-02 todo from done.txt 1\nx 2022-02-03 todo from done.txt 2\nx 2022-02-04 todo from done.txt 3\nx 2022-02-05 todo from done.txt 4\nx 2022-02-01 Finished todo 3\nx 2022-02-08 Finished todo 1\nx 2022-02-17 Finished todo 2`;
await new Promise((resolve) => setTimeout(resolve, 1000));
expect(fileContent).toEqual(expectedContent);
//await new Promise((resolve) => setTimeout(resolve, 1000));
setTimeout(() => expect(fileContent).toEqual(expectedContent), 1000);
});
});
90 changes: 45 additions & 45 deletions src/__tests__/main/CreateRecurringTodo.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'fs/promises';
import fs from 'fs';
import { createRecurringTodo } from '../../main/modules/ProcessDataRequest/CreateRecurringTodo';
import dayjs from 'dayjs';

Expand Down Expand Up @@ -32,92 +32,92 @@ const dateTodayInTwoMonths = dateToday.add(2, 'month').format('YYYY-MM-DD');
const dateTodayInSevenWeeks = dateToday.add(7, 'week').format('YYYY-MM-DD');

describe('Create recurring todos', () => {
beforeEach(async () => {
beforeEach(() => {
jest.clearAllMocks();
await fs.writeFile('./src/__tests__/__mock__/recurrence.txt', '');
fs.writeFileSync('./src/__tests__/__mock__/recurrence.txt', '');
});

test('Should add a new todo with due date set to tomorrow', async () => {
await createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Line 1 rec:1d`, '1d');
const fileContent = await fs.readFile('./src/__tests__/__mock__/recurrence.txt', 'utf8');
test('Should add a new todo with due date set to tomorrow', () => {
createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Line 1 rec:1d`, '1d');
const fileContent = fs.readFileSync('./src/__tests__/__mock__/recurrence.txt', 'utf8');
expect(fileContent.split('\n').pop()).toEqual(dateTodayString + ' Line 1 rec:1d due:' + dateTomorrowString);
});

test('Should add a new todo with due date set to next week', async () => {
await createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Line 1 rec:w`, 'w');
const fileContent = await fs.readFile('./src/__tests__/__mock__/recurrence.txt', 'utf8');
test('Should add a new todo with due date set to next week', () => {
createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Line 1 rec:w`, 'w');
const fileContent = fs.readFileSync('./src/__tests__/__mock__/recurrence.txt', 'utf8');
expect(fileContent.split('\n').pop()).toEqual(dateTodayString + ' Line 1 rec:w due:' + dateInOneWeekString);
});

test('Should add a new todo with due date set to today in 2 months', async () => {
await createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Line 1 rec:2m`, '2m');
const fileContent = await fs.readFile('./src/__tests__/__mock__/recurrence.txt', 'utf8');
test('Should add a new todo with due date set to today in 2 months', () => {
createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Line 1 rec:2m`, '2m');
const fileContent = fs.readFileSync('./src/__tests__/__mock__/recurrence.txt', 'utf8');
expect(fileContent.split('\n').pop()).toEqual(dateTodayString + ' Line 1 rec:2m due:' + dateTodayInTwoMonths);
});

test('Should add a new todo with due date set to day after tomorrow', async () => {
await createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Line 1 rec:+1d due:${dateTomorrowString}`, '+1d');
const fileContent = await fs.readFile('./src/__tests__/__mock__/recurrence.txt', 'utf8');
test('Should add a new todo with due date set to day after tomorrow', () => {
createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Line 1 rec:+1d due:${dateTomorrowString}`, '+1d');
const fileContent = fs.readFileSync('./src/__tests__/__mock__/recurrence.txt', 'utf8');
expect(fileContent.split('\n').pop()).toEqual(dateTodayString + ' Line 1 rec:+1d due:' + dateDayAfterTomorrowString);
});

test('Should add a new todo with due date set to today in 7 weeks', async () => {
await createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Line 1 rec:7w due:${dateTomorrowString}`, '7w');
const fileContent = await fs.readFile('./src/__tests__/__mock__/recurrence.txt', 'utf8');
test('Should add a new todo with due date set to today in 7 weeks', () => {
createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Line 1 rec:7w due:${dateTomorrowString}`, '7w');
const fileContent = fs.readFileSync('./src/__tests__/__mock__/recurrence.txt', 'utf8');
expect(fileContent.split('\n').pop()).toEqual(dateTodayString + ' Line 1 rec:7w due:' + dateTodayInSevenWeeks);
});

test('Should add a new todo with due date set to next possible business day, based off a todo which already contains a due date', async () => {
await createRecurringTodo(`x 2023-07-21 2023-07-21 Line 1 due:2023-07-21 rec:+1b`, '+1b');
const fileContent = await fs.readFile('./src/__tests__/__mock__/recurrence.txt', 'utf8');
test('Should add a new todo with due date set to next possible business day, based off a todo which already contains a due date', () => {
createRecurringTodo(`x 2023-07-21 2023-07-21 Line 1 due:2023-07-21 rec:+1b`, '+1b');
const fileContent = fs.readFileSync('./src/__tests__/__mock__/recurrence.txt', 'utf8');
expect(fileContent.split('\n').pop()).toEqual(`${dateTodayString} Line 1 due:2023-07-24 rec:+1b`);
});

test('Should add a new todo adding a strict recurrence of one year to due date and threshold date', async () => {
await createRecurringTodo(`x 2021-01-01 2021-01-01 taxes are due in one year t:2021-03-30 due:2021-04-30 rec:+1y`, '+1y');
const fileContent = await fs.readFile('./src/__tests__/__mock__/recurrence.txt', 'utf8');
test('Should add a new todo adding a strict recurrence of one year to due date and threshold date', () => {
createRecurringTodo(`x 2021-01-01 2021-01-01 taxes are due in one year t:2021-03-30 due:2021-04-30 rec:+1y`, '+1y');
const fileContent = fs.readFileSync('./src/__tests__/__mock__/recurrence.txt', 'utf8');
expect(fileContent.split('\n').pop()).toEqual(`${dateTodayString} taxes are due in one year t:2022-03-30 due:2022-04-30 rec:+1y`);
});

test('Should add a new todo adding a non-strict recurrence of one week to due date and threshold date', async () => {
await createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Water plants @home +quick due:2021-07-19 t:2021-07-09 rec:1w`, '1w');
const fileContent = await fs.readFile('./src/__tests__/__mock__/recurrence.txt', 'utf8');
test('Should add a new todo adding a non-strict recurrence of one week to due date and threshold date', () => {
createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Water plants @home +quick due:2021-07-19 t:2021-07-09 rec:1w`, '1w');
const fileContent = fs.readFileSync('./src/__tests__/__mock__/recurrence.txt', 'utf8');
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 threshold date. No due date should be created.', async () => {
await createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Line 1 rec:+1d t:2023-09-19`, '+1d');
const fileContent = await fs.readFile('./src/__tests__/__mock__/recurrence.txt', 'utf8');
test('Should add a new todo adding a strict recurrence of one day to threshold date. No due date should be created.', () => {
createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Line 1 rec:+1d t:2023-09-19`, '+1d');
const fileContent = fs.readFileSync('./src/__tests__/__mock__/recurrence.txt', 'utf8');
expect(fileContent.split('\n').pop()).toEqual(`${dateTodayString} Line 1 rec:+1d t:2023-09-20`);
});

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 () => {
await createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Line 1 rec:1d pri:A`, '1d');
const fileContent = await fs.readFile('./src/__tests__/__mock__/recurrence.txt', 'utf8');
test('Should add a new todo and preserve the priority in the pri extension, but should not set the priority (A) for the task.', () => {
createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Line 1 rec:1d pri:A`, '1d');
const fileContent = fs.readFileSync('./src/__tests__/__mock__/recurrence.txt', 'utf8');
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 () => {
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');
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', () => {
createRecurringTodo(`x ${dateTodayString} ${dateTodayString} (A) Do something rec:d t:${dateTodayString} @SomeContext`, 'd');
const fileContent = fs.readFileSync('./src/__tests__/__mock__/recurrence.txt', 'utf8');
expect(fileContent.split('\n').pop()).toEqual(`${dateTodayString} (A) Do something rec:d t:${dateTomorrowString} @SomeContext`);
});

test('Should add a new todo based on a zero daily recurrence and no due date and no threshold date are set', async () => {
await createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Do something rec:0d`, '0d');
const fileContent = await fs.readFile('./src/__tests__/__mock__/recurrence.txt', 'utf8');
test('Should add a new todo based on a zero daily recurrence and no due date and no threshold date are set', () => {
createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Do something rec:0d`, '0d');
const fileContent = fs.readFileSync('./src/__tests__/__mock__/recurrence.txt', 'utf8');
expect(fileContent.split('\n').pop()).toEqual(`${dateTodayString} Do something rec:0d`);
});

test('Should add a new todo based on a zero daily recurrence and a due date of today is set', async () => {
await createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Do something rec:0d due:1999-11-11`, '0d');
const fileContent = await fs.readFile('./src/__tests__/__mock__/recurrence.txt', 'utf8');
test('Should add a new todo based on a zero daily recurrence and a due date of today is set', () => {
createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Do something rec:0d due:1999-11-11`, '0d');
const fileContent = fs.readFileSync('./src/__tests__/__mock__/recurrence.txt', 'utf8');
expect(fileContent.split('\n').pop()).toEqual(`${dateTodayString} Do something rec:0d due:${dateTodayString}`);
});

test('Should add a new todo based on a zero daily recurrence and a due date and a threshold date of today are set', async () => {
await createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Do something rec:0d due:1999-11-11 t:2010-03-05`, '0d');
const fileContent = await fs.readFile('./src/__tests__/__mock__/recurrence.txt', 'utf8');
test('Should add a new todo based on a zero daily recurrence and a due date and a threshold date of today are set', () => {
createRecurringTodo(`x ${dateTodayString} ${dateTodayString} Do something rec:0d due:1999-11-11 t:2010-03-05`, '0d');
const fileContent = fs.readFileSync('./src/__tests__/__mock__/recurrence.txt', 'utf8');
expect(fileContent.split('\n').pop()).toEqual(`${dateTodayString} Do something rec:0d due:${dateTodayString} t:${dateTodayString}`);
});

Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/main/Dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { dialog } from 'electron';
import { openFile, createFile } from '../../main/modules/File/Dialog';
import { addFile } from '../../main/modules/File/File';
import fs from 'fs/promises';
import fs from 'fs';

jest.mock('../../main/main', () => ({
mainWindow: jest.fn(),
Expand Down Expand Up @@ -36,8 +36,8 @@ jest.mock('../../main/modules/File/File', () => ({
addFile: jest.fn(),
}));

jest.mock('fs/promises', () => ({
writeFile: jest.fn(),
jest.mock('fs', () => ({
writeFileSync: jest.fn(),
}));

describe('openFile', () => {
Expand Down Expand Up @@ -90,7 +90,7 @@ describe('createFile', () => {
filters: [{ name: 'Text files', extensions: ['txt'] }, { name: 'All files', extensions: ['*'] }],
});

expect(fs.writeFile).toHaveBeenCalledWith('./src/__tests__/__mock__/fileDialog.txt', '', 'utf8');
expect(fs.writeFileSync).toHaveBeenCalledWith('./src/__tests__/__mock__/fileDialog.txt', '', 'utf-8');
expect(addFile).toHaveBeenCalledWith('./src/__tests__/__mock__/fileDialog.txt', null);
});

Expand Down
6 changes: 6 additions & 0 deletions src/__tests__/main/Filters.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { applyAttributes } from '../../main/modules/Filters/Filters';

jest.mock('../../main/config', () => ({
config: {
get: jest.fn()
},
}));

describe('Should filter todos based on passed filters', () => {
const todoObjects = [
{ id: 1, body: 'Test', created: null, complete: false, completed: null, priority: null, contexts: null, projects: ['Project 1'], due: '2023-01-01', dueString: '2023-01-01', t: null, tString: null, rec: null, hidden: false, pm: null, visible: true, string: '' },
Expand Down
Loading

0 comments on commit 8ee6152

Please sign in to comment.