Skip to content

Commit

Permalink
Clean up fix of recurring tasks without a date
Browse files Browse the repository at this point in the history
Added new test file for recurrence.
  • Loading branch information
schemar committed Apr 18, 2022
1 parent 0fd369f commit 147d699
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/Recurrence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Recurrence {
* same relative distance to the due date as the original task. For example
* "starts one week before it is due".
*/
private readonly referenceDate: Moment;
private readonly referenceDate: Moment | null;

constructor({
rrule,
Expand All @@ -33,7 +33,7 @@ export class Recurrence {
}: {
rrule: RRule;
baseOnToday: boolean;
referenceDate: Moment;
referenceDate: Moment | null;
startDate: Moment | null;
scheduledDate: Moment | null;
dueDate: Moment | null;
Expand Down Expand Up @@ -80,11 +80,9 @@ export class Recurrence {
referenceDate = window.moment(scheduledDate);
} else if (startDate) {
referenceDate = window.moment(startDate);
} else {
referenceDate = window.moment();
}

if (!baseOnToday) {
if (!baseOnToday && referenceDate !== null) {
options.dtstart = window
.moment(referenceDate)
.startOf('day')
Expand Down Expand Up @@ -148,7 +146,9 @@ export class Recurrence {
// date if possible. Otherwise, base it on today if we do not have a
// reference date.
const after = window
.moment(this.referenceDate) // Can be `null` to mean "today".
// Reference date can be `null` to mean "today".
// Moment only accepts `undefined`, not `null`.
.moment(this.referenceDate ?? undefined)
.endOf('day')
.utc(true);

Expand Down
30 changes: 30 additions & 0 deletions tests/Recurrence.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @jest-environment jsdom
*/
import moment from 'moment';
import { Recurrence } from '../src/Recurrence';

jest.mock('obsidian');
window.moment = moment;

describe('Recurrence', () => {
it('creates a recurring instance even if no date is given', () => {
// Arrange
const recurrence = Recurrence.fromText({
recurrenceRuleText: 'every week',
startDate: null,
scheduledDate: null,
dueDate: null,
});

// Act
const next = recurrence!.next();

// Assert
expect(next).toStrictEqual({
startDate: null,
scheduledDate: null,
dueDate: null,
});
});
});
3 changes: 0 additions & 3 deletions tests/Task.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,6 @@ describe('toggle done', () => {
nextScheduled: '2021-10-18',
nextDue: '2021-10-20',
},
{
interval: 'every week',
},
];

test.concurrent.each<RecurrenceCase>(recurrenceCases)(
Expand Down

0 comments on commit 147d699

Please sign in to comment.