Skip to content

Commit

Permalink
Refactoring, fixed test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ransome1 committed Dec 12, 2023
1 parent b8e8269 commit f0a8893
Show file tree
Hide file tree
Showing 38 changed files with 859 additions and 951 deletions.
18 changes: 9 additions & 9 deletions src/__tests__/__mock__/recurrence.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

2023-12-11 Line 1 rec:1d due:2023-12-12
2023-12-11 Line 1 rec:w due:2023-12-18
2023-12-11 Line 1 rec:2m due:2024-02-11
2023-12-11 Line 1 rec:+1d due:2023-12-13
2023-12-11 Line 1 rec:7w due:2024-01-29
2023-12-12 Line 1 rec:1d due:2023-12-13
2023-12-12 Line 1 rec:w due:2023-12-19
2023-12-12 Line 1 rec:2m due:2024-02-12
2023-12-12 Line 1 rec:+1d due:2023-12-14
2023-12-12 Line 1 rec:7w due:2024-01-30
2023-07-21 Line 1 due:2023-07-24 rec:+1b
2021-01-01 taxes are due in one year t:2022-03-30 due:2022-04-30 rec:+1y
2023-12-11 Water plants @home +quick due:2023-12-18 t:2023-12-18 rec:1w
2023-12-11 Line 1 rec:+1d t:2023-09-20
2023-12-11 Line 1 rec:1d pri:A due:2023-12-12
2023-12-11 (A) Do something rec:d t:2023-12-12 @SomeContext
2023-12-12 Water plants @home +quick due:2023-12-19 t:2023-12-19 rec:1w
2023-12-12 Line 1 rec:+1d t:2023-09-20
2023-12-12 Line 1 rec:1d pri:A due:2023-12-13
2023-12-12 (A) Do something rec:d t:2023-12-13 @SomeContext
55 changes: 29 additions & 26 deletions src/__tests__/main/Active.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
import { getActiveFile } from '../../main/modules/File/Active';
import { File } from '../../main/util';

describe('Get active file', () => {
jest.mock('../../main/config', () => ({
configStorage: {
get: jest.fn((key) => {
if (key === 'files') {
return [
{
active: false,
todoFileName: 'test1.txt',
todoFilePath: '/test1.txt',
todoFileBookmark: null,
doneFilePath: 'done.txt',
doneFileBookmark: null,
},
{
active: true,
todoFileName: 'test2.txt',
todoFilePath: '/test2.txt',
todoFileBookmark: null,
doneFilePath: 'done.txt',
doneFileBookmark: null,
}
];
}
}),
set: jest.fn(),
},
}));

describe('Get active file', () => {
test('Should return the active file', () => {
const files: File[] = [
{
active: false,
todoFileName: 'test1.txt',
todoFilePath: '/test1.txt',
todoFileBookmark: null,
doneFilePath: 'done.txt',
doneFileBookmark: null,
},
{
active: true,
todoFileName: 'test2.txt',
todoFilePath: '/test2.txt',
todoFileBookmark: null,
doneFilePath: 'done.txt',
doneFileBookmark: null,
},
]
const activeFile = getActiveFile(files);
const activeFile = getActiveFile();
expect(activeFile).toEqual({
active: true,
todoFileName: 'test2.txt',
Expand All @@ -32,10 +41,4 @@ describe('Get active file', () => {
doneFileBookmark: null,
});
});

test('Should return null if the files array is empty', () => {
const files: File[] = []
const activeFile = getActiveFile(files);
expect(activeFile).toEqual(null);
});
});
2 changes: 1 addition & 1 deletion src/__tests__/main/Archive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('Archiving', () => {
});

test('Should collect data from todo and done file and merge it properly', async () => {
await archiveTodos(undefined);
await archiveTodos();
const fileContent = await fs.readFile('./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));
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/main/Attributes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('Set of filters must create a respective set of attributes and its coun
created: { '2026-01-01': { count: 2, notify: false} },
completed: {},
};
await updateAttributes(todoObjects, sorting, true);
await updateAttributes(todoObjects, sorting, false);
expect(attributes).toEqual(expectedAttributes);
});

Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/main/ChangeCompleteState.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { changeCompleteState } from '../../main/modules/TodoObject/ChangeCompleteState';
import { changeCompleteState } from '../../main/modules/ProcessDataRequest/ChangeCompleteState';
import { Item } from 'jstodotxt';
import dayjs from 'dayjs';

const date: string = dayjs(new Date()).format('YYYY-MM-DD');

jest.mock('../../main/modules/TodoObject/CreateRecurringTodo', () => ({
jest.mock('../../main/modules/ProcessDataRequest/CreateRecurringTodo', () => ({
createRecurringTodo: jest.fn(),
}));

Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/main/CreateRecurringTodo.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fs from 'fs/promises';
import { writeTodoObjectToFile } from '../../main/modules/File/Write';
import { createRecurringTodo } from '../../main/modules/TodoObject/CreateRecurringTodo';
import { createRecurringTodo } from '../../main/modules/ProcessDataRequest/CreateRecurringTodo';
import { getActiveFile } from '../../main/modules/File/Active';
import { lines } from '../../main/modules/TodoObject/CreateTodoObjects';
import { lines } from '../../main/modules/ProcessDataRequest/CreateTodoObjects';
import dayjs from 'dayjs';

jest.mock('../../main/modules/TodoObject/CreateTodoObjects', () => ({
jest.mock('../../main/modules/ProcessDataRequest/CreateTodoObjects', () => ({
lines: [''],
}));

Expand Down
18 changes: 11 additions & 7 deletions src/__tests__/main/CreateTodoObjects.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dayjs from 'dayjs';
import { createTodoObjects } from '../../main/modules/TodoObject/CreateTodoObjects';
import { createTodoObjects } from '../../main/modules/ProcessDataRequest/CreateTodoObjects';
import { configStorage } from '../../main/config';

const dateTodayString: string = dayjs(new Date()).format('YYYY-MM-DD');
Expand All @@ -17,19 +17,20 @@ jest.mock('../../main/config', () => ({
}));

const fileContent = `(B) Test +project @context todo 1 due:2023-12-31 t:2024-03-24 h:1 test @anotherContext pm:4 and a strict rec:+2w\nx 2023-07-23 2023-07-21 Test todo 2\nTest todo 3 due:end of the year\nTest todo 4 t:first day of next year`;
const todoObjects = createTodoObjects(fileContent);

describe('Create todo objects', () => {

beforeEach(() => {
jest.clearAllMocks();
});

test('should create 4 todo objects', () => {
test('should create 4 todo objects', async () => {
const todoObjects = await createTodoObjects(fileContent);
expect(todoObjects).toHaveLength(4);
});

test('should create a todo object', () => {
test('should create a todo object', async () => {
const todoObjects = await createTodoObjects(fileContent);
expect(todoObjects[0]).toEqual({
id: 0,
body: 'Test +project @context todo 1 due:2023-12-31 t:2024-03-24 h:1 test @anotherContext pm:4 and a strict rec:+2w',
Expand All @@ -51,7 +52,8 @@ describe('Create todo objects', () => {
});
});

test('should create a finished todo object', () => {
test('should create a finished todo object', async () => {
const todoObjects = await createTodoObjects(fileContent);
expect(todoObjects[1]).toEqual({
id: 1,
body: 'Test todo 2',
Expand All @@ -73,7 +75,8 @@ describe('Create todo objects', () => {
});
});

test('should create a todo object with speaking due date', () => {
test('should create a todo object with speaking due date', async () => {
const todoObjects = await createTodoObjects(fileContent);
expect(todoObjects[2]).toEqual({
id: 2,
body: 'Test todo 3 due:end of the year',
Expand All @@ -95,7 +98,8 @@ describe('Create todo objects', () => {
});
});

test('should create a todo object with speaking t date', () => {
test('should create a todo object with speaking t date', async () => {
const todoObjects = await createTodoObjects(fileContent);
expect(todoObjects[3]).toEqual({
id: 3,
body: 'Test todo 4 t:first day of next year',
Expand Down
20 changes: 12 additions & 8 deletions src/__tests__/main/File.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path'
import { configStorage } from '../../main/config';
import { createMenu } from '../../main/modules/Menu';
import { addFile, removeFile, setFile } from '../../main/modules/File/File';

jest.mock('../../main/main', () => ({
Expand Down Expand Up @@ -38,8 +39,8 @@ describe('File functions', () => {
jest.clearAllMocks();
});

test('addFile should add a new file to the config storage', async () => {
await addFile(path.join('/', 'path', 'to', 'test4.txt'), null);
test('addFile should add a new file to the config storage', () => {
addFile(path.join('/', 'path', 'to', 'test4.txt'), null);
expect(configStorage.set).toHaveBeenCalledTimes(1);
expect(configStorage.set).toHaveBeenCalledWith('files', [
{
Expand Down Expand Up @@ -76,8 +77,9 @@ describe('File functions', () => {
},
]);
});
test('removeFile should remove a file from the config storage, the active file stays unchanged', async () => {
await removeFile(1);

test('removeFile should remove a file from the config storage, the active file stays unchanged', () => {
removeFile(1);
expect(configStorage.set).toHaveBeenCalledTimes(1);
expect(configStorage.set).toHaveBeenCalledWith('files', [
{
Expand Down Expand Up @@ -106,8 +108,9 @@ describe('File functions', () => {
},
]);
});
test('removeFile should remove the active file from the config storage, a new active file is defined', async () => {
await removeFile(2);

test('removeFile should remove the active file from the config storage, a new active file is defined', () => {
removeFile(2);
expect(configStorage.set).toHaveBeenCalledTimes(1);
expect(configStorage.set).toHaveBeenCalledWith('files', [
{
Expand All @@ -128,8 +131,9 @@ describe('File functions', () => {
},
]);
});
test('setFile should set a file as active in the config storage', async () => {
await setFile(1);

test('setFile should set a file as active in the config storage', () => {
setFile(1);
expect(configStorage.set).toHaveBeenCalledTimes(1);
expect(configStorage.set).toHaveBeenCalledWith('files', [
{
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/main/Filters.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { applyFilters } from '../../main/modules/Filters';
import { applyFilters } from '../../main/modules/Filters/Filters';

describe('Should filter todos based on passed filters', () => {
const todoObjects = [
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/main/HandleNotification.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { handleNotification, sendNotification } from '../../main/modules/HandleNotification';
import { configStorage } from '../../main/config';
import { badge } from '../../main/modules/TodoObject/CreateTodoObjects';
import { badge } from '../../main/modules/ProcessDataRequest/CreateTodoObjects';
import { Notification } from 'electron';
import dayjs from 'dayjs';

Expand All @@ -10,7 +10,7 @@ const dateTomorrowString = dateToday.add(1, 'day').format('YYYY-MM-DD');
const dateInSevenDaysString = dateToday.add(7, 'day').format('YYYY-MM-DD');
const dateInTwentyDaysString = dateToday.add(20, 'day').format('YYYY-MM-DD');

jest.mock('../../main/modules/TodoObject/CreateTodoObjects', () => ({
jest.mock('../../main/modules/ProcessDataRequest/CreateTodoObjects', () => ({
badge: {
count: 0,
},
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/main/ProcessTodoObjects.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sortAndGroupTodoObjects, flattenTodoObjects, applySearchString, countTodoObjects } from '../../main/modules/TodoObject/ProcessTodoObjects';
import { sortAndGroupTodoObjects, flattenTodoObjects, applySearchString, countTodoObjects } from '../../main/modules/ProcessDataRequest/ProcessTodoObjects';

jest.mock('../../main/config', () => ({
configStorage: {
Expand Down Expand Up @@ -178,8 +178,8 @@ describe('Process todo.txt objects', () => {
});

test('Objects are counted correctly', () => {
const count: number = countTodoObjects(todoObjects);
expect(count).toEqual(7);
const count: number = countTodoObjects(todoObjects, true);
expect(count).toEqual(1);
});

test('Search for "test3" result in 1 found object', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/main/Write.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs/promises';
import { writeTodoObjectToFile, removeLineFromFile } from '../../main/modules/File/Write';
import { lines } from '../../main/modules/TodoObject/CreateTodoObjects';
import { lines } from '../../main/modules/ProcessDataRequest/CreateTodoObjects';
import { configStorage } from '../../main/config';
import dayjs from 'dayjs';

Expand Down Expand Up @@ -29,7 +29,7 @@ jest.mock('../../main/config', () => ({
},
}));

jest.mock('../../main/modules/TodoObject/CreateTodoObjects', () => ({
jest.mock('../../main/modules/ProcessDataRequest/CreateTodoObjects', () => ({
lines: ['Line 1', 'Line 2', 'Line 3'],
}));

Expand Down
10 changes: 3 additions & 7 deletions src/main/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import path from 'path';
import { app, nativeTheme } from 'electron';
import fs from 'fs';
import { mainWindow } from './main';
import createTray from './modules/Tray';
import { createTray } from './modules/Tray';
import { File, ConfigData } from './util';
import processDataRequest from './modules/ProcessDataRequest';
import processDataRequest from './modules/ProcessDataRequest/ProcessDataRequest';
import handleTheme from './modules/Theme';
import crypto from 'crypto';

Expand Down Expand Up @@ -130,11 +130,7 @@ configStorage.onDidChange('colorTheme', (colorTheme) => {
});

configStorage.onDidChange('tray', () => {
createTray().then(result => {
console.log('config.ts:', result);
}).catch(error => {
console.error('config.ts:', error);
});
createTray();
});

nativeTheme.on('updated', handleTheme);
Expand Down
30 changes: 6 additions & 24 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { app, BrowserWindow, nativeTheme } from 'electron';
import path from 'path';
import fs from 'fs';
import { configStorage } from './config';
import createMenu from './modules/Menu';
import { createMenu } from './modules/Menu';
import { resolveHtmlPath, getAssetPath, File } from './util';
import { createFileWatcher, watcher } from './modules/File/Watcher';
import createTray from './modules/Tray';
import { createTray } from './modules/Tray';
import './modules/Ipc';
import handleTheme from './modules/Theme';

Expand All @@ -20,13 +20,7 @@ const handleCreateWindow = () => {
if (mainWindow) {
mainWindow.show();
} else {
createWindow()
.then((result) => {
console.log('main.ts:', result);
})
.catch((error) => {
console.error('main.ts:', error);
});
createWindow();
}
}

Expand Down Expand Up @@ -185,24 +179,12 @@ app
.on('activate', handleCreateWindow)
.whenReady()
.then(() => {
createWindow().then(result => {
console.log('main.ts:', result);
}).catch(error => {
console.error('main.ts:', error);
});
createWindow();

createMenu(files).then(result => {
console.log('main.ts:', result);
}).catch(error => {
console.error('main.ts:', error);
});
createMenu(files);

if(tray) {
createTray().then(result => {
console.log('main.ts:', result);
}).catch(error => {
console.error('main.ts:', error);
});
createTray();
}

eventListeners
Expand Down
Loading

0 comments on commit f0a8893

Please sign in to comment.