Skip to content

Commit

Permalink
Fixed test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ransome1 committed Aug 26, 2023
1 parent e01ce7c commit 1ecbf8c
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/__tests__/__mock__/recurrence.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

2023-08-22 Line 1 rec:1d due:2023-08-23
2023-08-22 Line 1 rec:2m due:2023-10-22
2023-08-22 Line 1 rec:+1d due:2023-08-24
2023-08-22 Line 1 rec:7w due:2023-10-10
2023-08-26 Line 1 rec:1d due:2023-08-27
2023-08-26 Line 1 rec:2m due:2023-10-26
2023-08-26 Line 1 rec:+1d due:2023-08-28
2023-08-26 Line 1 rec:7w due:2023-10-14
2023-07-21 Line 1 rec:+1b due:2023-07-24
2 changes: 1 addition & 1 deletion src/__tests__/__mock__/test.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Line 1
Edited line
New line
2023-08-22 New line with creation date
2023-08-26 New line with creation date
7 changes: 4 additions & 3 deletions src/__tests__/main/File.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('File functions', () => {
beforeEach(() => {
jest.clearAllMocks();
});

test('addFile should add a new file to the config storage', async () => {
await addFile('/path/to/test4.txt');
expect(configStorage.set).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -49,7 +50,7 @@ describe('File functions', () => {
]);
});
test('removeFile should remove a file from the config storage, the active file stays unchanged', async () => {
await removeFile(1);
await removeFile(null, 1);
expect(configStorage.set).toHaveBeenCalledTimes(1);
expect(configStorage.set).toHaveBeenCalledWith('files', [
{
Expand All @@ -73,7 +74,7 @@ describe('File functions', () => {
]);
});
test('removeFile should remove the active file from the config storage, a new active file is defined', async () => {
await removeFile(2);
await removeFile(null, 2);
expect(configStorage.set).toHaveBeenCalledTimes(1);
expect(configStorage.set).toHaveBeenCalledWith('files', [
{
Expand All @@ -91,7 +92,7 @@ describe('File functions', () => {
]);
});
test('setFile should set a file as active in the config storage', async () => {
await setFile({}, 1);
await setFile(null, 1);
expect(configStorage.set).toHaveBeenCalledTimes(1);
expect(configStorage.set).toHaveBeenCalledWith('files', [
{
Expand Down
10 changes: 6 additions & 4 deletions src/__tests__/main/Filters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { applyFilters, createAttributesObject } from '../../main/modules/Filters
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', t: null, rec: null, hidden: false, pm: null, string: '' },
{ id: 1, body: 'Test', created: null, complete: true, completed: null, priority: null, contexts: null, projects: ['Project 2'], due: '2023-02-01', t: null, rec: null, hidden: false, pm: null, string: '' },
{ id: 1, body: 'Test', created: null, complete: false, completed: null, priority: null, contexts: null, projects: ['Project 1'], due: '2023-03-01', t: null, rec: null, hidden: false, pm: null, string: '' },
{ id: 2, body: 'Test', created: null, complete: true, completed: null, priority: null, contexts: null, projects: ['Project 2'], due: '2023-02-01', t: null, rec: null, hidden: false, pm: null, string: '' },
{ id: 3, body: 'Test', created: null, complete: false, completed: null, priority: null, contexts: null, projects: ['Project 1'], due: '2023-03-01', t: null, rec: null, hidden: false, pm: null, string: '' },
];

test('should return all todo objects if no filters are provided', () => {
Expand All @@ -14,10 +14,12 @@ describe('Should filter todos based on passed filters', () => {
});

test('should filter todo objects based on project filter', () => {
const filters = [{ value: 'Project 1', exclude: false }];
const filters = {
projects: [ { value: 'Project 1', exclude: false } ]
}
const expected = [
{ id: 1, body: 'Test', created: null, complete: false, completed: null, priority: null, contexts: null, projects: ['Project 1'], due: '2023-01-01', t: null, rec: null, hidden: false, pm: null, string: '' },
{ id: 2, body: 'Test', created: null, complete: false, completed: null, priority: null, contexts: null, projects: ['Project 1'], due: '2023-03-01', t: null, rec: null, hidden: false, pm: null, string: '' },
{ id: 3, body: 'Test', created: null, complete: false, completed: null, priority: null, contexts: null, projects: ['Project 1'], due: '2023-03-01', t: null, rec: null, hidden: false, pm: null, string: '' },
];
const result = applyFilters(todoObjects, filters);
expect(result).toEqual(expected);
Expand Down
2 changes: 1 addition & 1 deletion src/main/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import buildMenu from './menu';
import { Sorting, File } from './util';
import processDataRequest from './modules/ProcessDataRequest';

const userDataDirectory = path.join(app.getPath('userData'), 'userData');
const userDataDirectory = path.join(app.getPath('userData'), 'userData' + app.getVersion());
console.log('config.ts: sleek userdata is located at: ' + userDataDirectory);

if (!fs.existsSync(userDataDirectory)) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/modules/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async function addFile(filePath: string): Promise<void> {
}
}

async function removeFile(index: number): Promise<void> {
async function removeFile(event: any, index: number): Promise<void> {
try {
let files: File[] = configStorage.get('files') as File[];

Expand Down
13 changes: 7 additions & 6 deletions src/main/modules/Filters.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { BrowserWindow } from 'electron';
import dayjs from 'dayjs';
import { TodoObject, Filter, Attributes } from '../util';
import { TodoObject, Filters, Filter, Attributes } from '../util';

function applyFilters(todoObjects: TodoObject[], filters: Filter[] | null): TodoObject[] {
function applyFilters(todoObjects: TodoObject[], filters: Filters | null): TodoObject[] {
if (filters && Object.keys(filters).length > 0) {
return todoObjects.filter((todoObject: TodoObject) => {

return Object.entries(filters).every(([key, filterArray]) => {

if (Array.isArray(filterArray) && filterArray.length === 0) {
if (filterArray?.length === 0) {
return true;
}

const attributeValues: any = todoObject[key as keyof TodoObject];

return filterArray.every(({ value, exclude }) => {
return filterArray.every(({ value, exclude }: Filter) => {
if (
attributeValues === undefined ||
attributeValues === null ||
Expand All @@ -29,7 +30,6 @@ function applyFilters(todoObjects: TodoObject[], filters: Filter[] | null): Todo
});
});
}

return todoObjects;
}

Expand All @@ -53,7 +53,8 @@ function createAttributesObject(todoObjects: TodoObject[]): Attributes {

todoObjects.forEach((item) => {
Object.keys(attributes).forEach((key) => {
const value: any = item[key];

const value = item[key as keyof TodoObject];

if (Array.isArray(value)) {
value.forEach((element) => {
Expand Down
5 changes: 3 additions & 2 deletions src/main/modules/ProcessTodoObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,17 @@ function sortAndGroupTodoObjects(todoObjects: TodoObject[], sorting: { id: strin
}

function flattenTodoObjects(todoObjects: TodoObject[], topLevelGroup: string) {

const flattenedObjects = [];

function flatten(todoObject: TodoObject, sortingKey: string) {
function flatten(todoObject: any, sortingKey: string) {
if (typeof todoObject === 'object' && todoObject !== null) {
if ('id' in todoObject) {
flattenedObjects.push(todoObject);
}

for (const key in todoObject) {
if (key !== sortingKey) {
if (key !== sortingKey && typeof todoObject[key] === 'object') {
flatten(todoObject[key], sortingKey);
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/main/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export interface TodoObject {
completed: string | null;
priority: string | null;
contexts: string[] | null;
projects: string[] | null;
due: string | null;
projects: string[] | createdull;
due: string | completed
t: string | null;
rec: string | null;
hidden: boolean;
Expand All @@ -45,6 +45,17 @@ export interface Sorting {
invert: boolean;
}

export interface Filters {
projects?: Filter[];
contexts?: Filter[];
priority?: Filter[];
pm?: Filter[];
due?: Filter[];
t?: Filter[];
created?: Filter[];
completed?: Filter[];
}

export interface Filter {
value: string;
exclude: boolean;
Expand Down
7 changes: 3 additions & 4 deletions src/renderer/DataGridRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const DataGridRow = React.memo(({ todoObject, attributes, filters, setDialogOpen
return substrings;
};

const matches = () => {
const extractedMatches = () => {
const expressions = [
{ pattern: /(@\S+)/, type: 'contexts', key: '@' },
{ pattern: /\+\S+/, type: 'projects', key: '+' },
Expand Down Expand Up @@ -149,7 +149,7 @@ const DataGridRow = React.memo(({ todoObject, attributes, filters, setDialogOpen
};


const elements = matches().map((element, index) => {
const rowElements = extractedMatches().map((element, index) => {

if(!element.value) return;

Expand Down Expand Up @@ -185,7 +185,6 @@ const DataGridRow = React.memo(({ todoObject, attributes, filters, setDialogOpen
}
});


return (
<ThemeProvider theme={theme}>
<ContextMenu index={todoObject.id} anchorPosition={contextMenuPosition} setContextMenuPosition={setContextMenuPosition} />
Expand All @@ -208,7 +207,7 @@ const DataGridRow = React.memo(({ todoObject, attributes, filters, setDialogOpen
<FontAwesomeIcon icon={faEyeSlash} />
)}

{elements}
{rowElements}

</ListItem>
</ThemeProvider>
Expand Down

0 comments on commit 1ecbf8c

Please sign in to comment.