-
-
Notifications
You must be signed in to change notification settings - Fork 356
Expand file tree
/
Copy pathDefaultTaskSerializer.test.ts
More file actions
119 lines (103 loc) · 5.17 KB
/
DefaultTaskSerializer.test.ts
File metadata and controls
119 lines (103 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* @jest-environment jsdom
*/
import moment from 'moment';
import { Priority } from '../../src/Task';
import type { Settings } from '../../src/Config/Settings';
import { DefaultTaskSerializer } from '../../src/TaskSerializer';
import { RecurrenceBuilder } from '../TestingTools/RecurrenceBuilder';
import { DEFAULT_SYMBOLS, type DefaultTaskSerializerSymbols } from '../../src/TaskSerializer/DefaultTaskSerializer';
import { TaskBuilder } from '../TestingTools/TaskBuilder';
jest.mock('obsidian');
window.moment = moment;
type DefaultTaskSerializeSymbolMap = readonly {
taskFormat: Settings['taskFormat'];
symbols: DefaultTaskSerializerSymbols;
}[];
// A map that facilitates parameterizing the tests over symbols
const symbolMap: DefaultTaskSerializeSymbolMap = [{ taskFormat: 'tasksPluginEmoji', symbols: DEFAULT_SYMBOLS }];
describe.each(symbolMap)("DefaultTaskSerializer with '$taskFormat' symbols", ({ symbols }) => {
const taskSerializer = new DefaultTaskSerializer(symbols);
const serialize = taskSerializer.serialize.bind(taskSerializer);
const deserialize = taskSerializer.deserialize.bind(taskSerializer);
const { startDateSymbol, createdDateSymbol, recurrenceSymbol, scheduledDateSymbol, dueDateSymbol, doneDateSymbol } =
symbols;
describe('deserialize', () => {
it('should parse an empty string', () => {
const taskDetails = deserialize('');
expect(taskDetails).toMatchTaskDetails({});
});
it.each([
{ what: 'startDate', symbol: startDateSymbol },
{ what: 'createdDate', symbol: createdDateSymbol },
{ what: 'scheduledDate', symbol: scheduledDateSymbol },
{ what: 'dueDate', symbol: dueDateSymbol },
{ what: 'doneDate', symbol: doneDateSymbol },
] as const)('should parse a $what', ({ what, symbol }) => {
const taskDetails = deserialize(`${symbol} 2021-06-20`);
expect(taskDetails).toMatchTaskDetails({ [what]: moment('2021-06-20', 'YYYY-MM-DD') });
});
it('should parse a priority', () => {
const priorities = ['Highest', 'High', 'None', 'Medium', 'Low', 'Lowest'] as const;
for (const p of priorities) {
const prioritySymbol = symbols.prioritySymbols[p];
const priority = Priority[p];
const taskDetails = deserialize(`${prioritySymbol}`);
expect(taskDetails).toMatchTaskDetails({ priority });
}
});
it('should parse a recurrence', () => {
const taskDetails = deserialize(`${recurrenceSymbol} every day`);
expect(taskDetails).toMatchTaskDetails({
recurrence: new RecurrenceBuilder().rule('every day').build(),
});
});
it('should parse tags', () => {
const description = '#hello #world #task';
const taskDetails = deserialize(description);
expect(taskDetails).toMatchTaskDetails({ tags: ['#hello', '#world', '#task'], description });
});
});
describe('serialize', () => {
it('should serialize an "Empty" Task as the empty string', () => {
const serialized = serialize(new TaskBuilder().description('').build());
expect(serialized).toEqual('');
});
it.each([
{ what: 'startDate', symbol: startDateSymbol },
{ what: 'createdDate', symbol: createdDateSymbol },
{ what: 'scheduledDate', symbol: scheduledDateSymbol },
{ what: 'dueDate', symbol: dueDateSymbol },
{ what: 'doneDate', symbol: doneDateSymbol },
] as const)('should serialize a $what', ({ what, symbol }) => {
const serialized = serialize(new TaskBuilder()[what]('2021-06-20').description('').build());
expect(serialized).toEqual(` ${symbol} 2021-06-20`);
});
it('should serialize a Highest, High, Medium, Low and Lowest priority', () => {
const priorities = ['Highest', 'High', 'Medium', 'Low', 'Lowest'] as const;
for (const p of priorities) {
const task = new TaskBuilder().priority(Priority[p]).description('').build();
const serialized = serialize(task);
expect(serialized).toEqual(` ${symbols.prioritySymbols[p]}`);
}
});
it('should serialize a None priority', () => {
const task = new TaskBuilder().priority(Priority.None).description('').build();
const serialized = serialize(task);
expect(serialized).toEqual('');
});
it('should serialize a recurrence', () => {
const task = new TaskBuilder()
.recurrence(new RecurrenceBuilder().rule('every day').build())
.description('')
.build();
const serialized = serialize(task);
expect(serialized).toEqual(` ${recurrenceSymbol} every day`);
});
it('should serialize tags', () => {
const task = new TaskBuilder().description('').tags(['#hello', '#world', '#task']).build();
const serialized = serialize(task);
expect(serialized).toEqual(' #hello #world #task');
});
});
});