Skip to content

Commit 23f1a36

Browse files
authored
test: add group sort order tests (#2024)
* test: test showing current urgency group order * test: test showing current status group order * test: test showing current tag group order * test: BacklinkField group order * test: StatusTypeField group order * test: collect sample tasks in one class * test: fix tag order (bad cherry pick from another branch * test: FilenameField group order * test: FolderField group order * test: HeadingField group order * test: PathField group order * test: fix wrong function rename * test: StartDateField group order * test: DoneDateField group order * test: CreatedDateField group order * test: ScheduledDateField group order * test: HappensDateField group order * test: rename method to include roots * test: RootField group order * test: Recurrence field group order * test: RecurringField group order
1 parent b03a079 commit 23f1a36

22 files changed

+416
-74
lines changed

tests/Query/Filter/BacklinkField.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BacklinkField } from '../../../src/Query/Filter/BacklinkField';
2-
import { fromLine } from '../../TestHelpers';
2+
import { SampleTasks, fromLine } from '../../TestHelpers';
33

44
describe('backlink', () => {
55
it('should provide the backlink', () => {
@@ -65,4 +65,20 @@ describe('grouping by backlink', () => {
6565
expect(grouper(fromLine({ line: t, path: path, precedingHeader: heading }))).toEqual(groups);
6666
},
6767
);
68+
69+
it('should sort groups for BacklinkField', () => {
70+
// Arrange
71+
const tasks = SampleTasks.withAllRootsPathsHeadings();
72+
const grouper = new BacklinkField().createNormalGrouper();
73+
74+
// Assert
75+
expect({ grouper, tasks }).groupHeadingsToBe([
76+
'\\_c\\_',
77+
'\\_c\\_ > heading _italic text_',
78+
'a\\_b\\_c',
79+
'c',
80+
'c > heading',
81+
'Unknown Location',
82+
]);
83+
});
6884
});

tests/Query/Filter/CreatedDateField.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
expectTaskComparesBefore,
1212
expectTaskComparesEqual,
1313
} from '../../CustomMatchers/CustomMatchersForSorting';
14+
import { SampleTasks } from '../../TestHelpers';
1415

1516
window.moment = moment;
1617

@@ -105,4 +106,17 @@ describe('grouping by created date', () => {
105106
expect(grouper.grouper(taskWithDate)).toEqual(['1970-01-01 Thursday']);
106107
expect(grouper.grouper(taskWithoutDate)).toEqual(['No created date']);
107108
});
109+
110+
it('should sort groups for CreatedDateField', () => {
111+
const grouper = new CreatedDateField().createNormalGrouper();
112+
const tasks = SampleTasks.withAllRepresentativeCreatedDates();
113+
114+
expect({ grouper, tasks }).groupHeadingsToBe([
115+
'2023-05-30 Tuesday',
116+
'2023-05-31 Wednesday',
117+
'2023-06-01 Thursday',
118+
'Invalid date',
119+
'No created date',
120+
]);
121+
});
108122
});

tests/Query/Filter/DoneDateField.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { FilterOrErrorMessage } from '../../../src/Query/Filter/Filter';
77
import { TaskBuilder } from '../../TestingTools/TaskBuilder';
88
import { testFilter } from '../../TestingTools/FilterTestHelpers';
99
import { expectTaskComparesAfter, expectTaskComparesBefore } from '../../CustomMatchers/CustomMatchersForSorting';
10+
import { SampleTasks } from '../../TestHelpers';
1011

1112
window.moment = moment;
1213

@@ -102,4 +103,17 @@ describe('grouping by done date', () => {
102103
expect(grouper.grouper(taskWithDate)).toEqual(['1970-01-01 Thursday']);
103104
expect(grouper.grouper(taskWithoutDate)).toEqual(['No done date']);
104105
});
106+
107+
it('should sort groups for DoneDateField', () => {
108+
const grouper = new DoneDateField().createNormalGrouper();
109+
const tasks = SampleTasks.withAllRepresentativeDoneDates();
110+
111+
expect({ grouper, tasks }).groupHeadingsToBe([
112+
'2023-05-30 Tuesday',
113+
'2023-05-31 Wednesday',
114+
'2023-06-01 Thursday',
115+
'Invalid date',
116+
'No done date',
117+
]);
118+
});
105119
});

tests/Query/Filter/DueDateField.test.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* @jest-environment jsdom
33
*/
44
import moment from 'moment';
5-
import type { Task } from 'Task';
65
import { DueDateField } from '../../../src/Query/Filter/DueDateField';
76
import type { FilterOrErrorMessage } from '../../../src/Query/Filter/Filter';
87
import { TaskBuilder } from '../../TestingTools/TaskBuilder';
@@ -14,6 +13,7 @@ import {
1413
} from '../../CustomMatchers/CustomMatchersForSorting';
1514
import { Query } from '../../../src/Query/Query';
1615
import { MarkdownTable } from '../../TestingTools/VerifyMarkdownTable';
16+
import { SampleTasks } from '../../TestHelpers';
1717

1818
window.moment = moment;
1919

@@ -535,7 +535,7 @@ describe('grouping by due date', () => {
535535

536536
it('should sort groups for DueDateField', () => {
537537
const grouper = new DueDateField().createNormalGrouper();
538-
const tasks = withAllRepresentativeDueDates();
538+
const tasks = SampleTasks.withAllRepresentativeDueDates();
539539

540540
expect({ grouper, tasks }).groupHeadingsToBe([
541541
'2023-05-30 Tuesday',
@@ -546,13 +546,3 @@ describe('grouping by due date', () => {
546546
]);
547547
});
548548
});
549-
550-
function withAllRepresentativeDueDates(): Task[] {
551-
const dates = ['2023-05-30', '2023-05-31', '2023-06-01', '2023-02-32', null];
552-
553-
const tasks = dates.map((date) => {
554-
return new TaskBuilder().dueDate(date).build();
555-
});
556-
557-
return tasks;
558-
}

tests/Query/Filter/FilenameField.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { TaskBuilder } from '../../TestingTools/TaskBuilder';
22
import { FilenameField } from '../../../src/Query/Filter/FilenameField';
33
import * as CustomMatchersForSorting from '../../CustomMatchers/CustomMatchersForSorting';
4-
import { fromLine } from '../../TestHelpers';
4+
import { SampleTasks, fromLine } from '../../TestHelpers';
55

66
describe('filename', () => {
77
it('should provide access to the file name with extension', () => {
@@ -119,4 +119,13 @@ describe('grouping by filename', () => {
119119
// Assert
120120
expect(grouper(fromLine({ line: taskLine, path: path }))).toEqual(groups);
121121
});
122+
123+
it('should sort groups for FilenameField', () => {
124+
// Arrange
125+
const tasks = SampleTasks.withAllRootsPathsHeadings();
126+
const grouper = new FilenameField().createNormalGrouper();
127+
128+
// Assert
129+
expect({ grouper, tasks }).groupHeadingsToBe(['[[_c_]]', '[[a_b_c]]', '[[c]]', 'Unknown Location']);
130+
});
122131
});

tests/Query/Filter/FolderField.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { FolderField } from '../../../src/Query/Filter/FolderField';
2-
import { fromLine } from '../../TestHelpers';
2+
import { SampleTasks, fromLine } from '../../TestHelpers';
33

44
describe('folder', () => {
55
it('should provide access to the folder', () => {
@@ -48,4 +48,13 @@ describe('grouping by folder', () => {
4848
// Assert
4949
expect(grouper(fromLine({ line: taskLine, path: path }))).toEqual(groups);
5050
});
51+
52+
it('should sort groups for FolderField', () => {
53+
// Arrange
54+
const tasks = SampleTasks.withAllRootsPathsHeadings();
55+
const grouper = new FolderField().createNormalGrouper();
56+
57+
// Assert
58+
expect({ grouper, tasks }).groupHeadingsToBe(['/', 'a/b/', 'a/d/', 'e/d/']);
59+
});
5160
});

tests/Query/Filter/HappensDateField.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { HappensDateField } from '../../../src/Query/Filter/HappensDateField';
66
import { TaskBuilder } from '../../TestingTools/TaskBuilder';
77
import { testFilter } from '../../TestingTools/FilterTestHelpers';
88
import * as CustomMatchersForSorting from '../../CustomMatchers/CustomMatchersForSorting';
9-
import { fromLine } from '../../TestHelpers';
9+
import { SampleTasks, fromLine } from '../../TestHelpers';
1010

1111
window.moment = moment;
1212

@@ -155,4 +155,21 @@ describe('grouping by happens date', () => {
155155
// Assert
156156
expect(grouper.grouper(fromLine({ line: taskLine }))).toEqual(expectedResult);
157157
});
158+
159+
it('should sort groups for HappensDateField', () => {
160+
const grouper = new HappensDateField().createNormalGrouper();
161+
const tasks = [
162+
...SampleTasks.withAllRepresentativeDueDates(),
163+
...SampleTasks.withAllRepresentativeScheduledDates(),
164+
...SampleTasks.withAllRepresentativeStartDates(),
165+
];
166+
167+
expect({ grouper, tasks }).groupHeadingsToBe([
168+
'2023-05-30 Tuesday',
169+
'2023-05-31 Wednesday',
170+
'2023-06-01 Thursday',
171+
'Invalid date',
172+
'No happens date',
173+
]);
174+
});
158175
});

tests/Query/Filter/HeadingField.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { FilterOrErrorMessage } from '../../../src/Query/Filter/Filter';
33
import { TaskBuilder } from '../../TestingTools/TaskBuilder';
44
import { testFilter } from '../../TestingTools/FilterTestHelpers';
55
import * as CustomMatchersForSorting from '../../CustomMatchers/CustomMatchersForSorting';
6-
import { fromLine } from '../../TestHelpers';
6+
import { SampleTasks, fromLine } from '../../TestHelpers';
77

88
function testTaskFilterForHeading(filter: FilterOrErrorMessage, precedingHeader: string | null, expected: boolean) {
99
const builder = new TaskBuilder();
@@ -110,4 +110,19 @@ describe('grouping by heading', () => {
110110
expect(grouper(fromLine({ line: taskLine, precedingHeader: header }))).toEqual(groups);
111111
},
112112
);
113+
114+
it('should sort groups for HeadingField', () => {
115+
// Arrange
116+
const tasks = SampleTasks.withAllRootsPathsHeadings();
117+
const grouper = new HeadingField().createNormalGrouper();
118+
119+
// Assert
120+
expect({ grouper, tasks }).groupHeadingsToBe([
121+
'(No heading)',
122+
'a_b_c',
123+
'c',
124+
'heading',
125+
'heading _italic text_',
126+
]);
127+
});
113128
});

tests/Query/Filter/PathField.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
expectTaskComparesBefore,
88
expectTaskComparesEqual,
99
} from '../../CustomMatchers/CustomMatchersForSorting';
10-
import { fromLine } from '../../TestHelpers';
10+
import { SampleTasks, fromLine } from '../../TestHelpers';
1111

1212
function testTaskFilterForTaskWithPath(filter: FilterOrErrorMessage, path: string, expected: boolean) {
1313
const builder = new TaskBuilder();
@@ -163,4 +163,20 @@ describe('grouping by path', () => {
163163
// Assert
164164
expect(grouper(fromLine({ line: taskLine, path: path }))).toEqual(groups);
165165
});
166+
167+
it('should sort groups for PathField', () => {
168+
// Arrange
169+
const tasks = SampleTasks.withAllRootsPathsHeadings();
170+
const grouper = new PathField().createNormalGrouper();
171+
172+
// Assert
173+
expect({ grouper, tasks }).groupHeadingsToBe([
174+
// Why there is no path for empty path?
175+
'a/b/\\_c\\_',
176+
'a/b/c',
177+
'a/d/c',
178+
'a\\_b\\_c',
179+
'e/d/c',
180+
]);
181+
});
166182
});

tests/Query/Filter/PriorityField.test.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Priority, Task } from '../../../src/Task';
1+
import { Priority } from '../../../src/Task';
22
import { TaskBuilder } from '../../TestingTools/TaskBuilder';
33
import { testFilter } from '../../TestingTools/FilterTestHelpers';
44
import { PriorityField } from '../../../src/Query/Filter/PriorityField';
5-
import { fromLine } from '../../TestHelpers';
5+
import { SampleTasks, fromLine } from '../../TestHelpers';
66

77
import {
88
expectTaskComparesAfter,
@@ -229,7 +229,7 @@ describe('grouping by priority', () => {
229229

230230
it('should sort groups for PriorityField', () => {
231231
const grouper = new PriorityField().createNormalGrouper();
232-
const tasks = withAllPriorities();
232+
const tasks = SampleTasks.withAllPriorities();
233233

234234
expect({ grouper, tasks }).groupHeadingsToBe([
235235
'Priority 0: Highest',
@@ -241,18 +241,3 @@ describe('grouping by priority', () => {
241241
]);
242242
});
243243
});
244-
245-
export function withAllPriorities(): Task[] {
246-
const tasks: Task[] = [];
247-
const allPriorities = Object.values(Priority);
248-
allPriorities.forEach((priority) => {
249-
// This description is chosen to be useful for including tasks in user docs, so
250-
// changing it will change documentation and sample vault content.
251-
const priorityName = PriorityField.priorityNameUsingNormal(priority);
252-
const description = `#task ${priorityName} priority`;
253-
254-
const task = new TaskBuilder().priority(priority).description(description).build();
255-
tasks.push(task);
256-
});
257-
return tasks;
258-
}

0 commit comments

Comments
 (0)