From 9cf0e3b96db455b62b9f11ff55b221efcd5a4c9b Mon Sep 17 00:00:00 2001 From: Ilyas Landikov Date: Tue, 12 Dec 2023 15:45:04 +0600 Subject: [PATCH 1/4] test: - better coverage of postpone menu item names --- tests/Scripting/Postponer.test.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/Scripting/Postponer.test.ts b/tests/Scripting/Postponer.test.ts index 16fc7a7045..bdd230deb5 100644 --- a/tests/Scripting/Postponer.test.ts +++ b/tests/Scripting/Postponer.test.ts @@ -18,7 +18,10 @@ import { TaskBuilder } from '../TestingTools/TaskBuilder'; window.moment = moment; +const yesterday = '2023-12-02'; const today = '2023-12-03'; +const tomorrow = '2023-12-04'; + beforeEach(() => { jest.useFakeTimers(); jest.setSystemTime(new Date(today)); @@ -169,13 +172,27 @@ describe('postpone - UI text', () => { ); }); - it('should include date type and new date in context menu labels', () => { + it('should include date type and new date in context menu labels when due today', () => { const task = new TaskBuilder().dueDate(today).build(); // TODO This text is misleading if the date is already in the future. // In that case, it should still be 'Postpone'??? expect(postponeMenuItemTitle(task, 1, 'day')).toEqual('Due in a day, on Mon 4th Dec'); expect(postponeMenuItemTitle(task, 2, 'days')).toEqual('Due in 2 days, on Tue 5th Dec'); }); + + it('should include date type and new date in context menu labels when overdue', () => { + const task = new TaskBuilder().dueDate(yesterday).build(); + + expect(postponeMenuItemTitle(task, 1, 'day')).toEqual('Due in a day, on Mon 4th Dec'); + expect(postponeMenuItemTitle(task, 2, 'days')).toEqual('Due in 2 days, on Tue 5th Dec'); + }); + + it('should include date type and new date in context menu labels when due in future', () => { + const task = new TaskBuilder().dueDate(tomorrow).build(); + + expect(postponeMenuItemTitle(task, 1, 'day')).toEqual('Due in a day, on Tue 5th Dec'); + expect(postponeMenuItemTitle(task, 2, 'days')).toEqual('Due in 2 days, on Wed 6th Dec'); + }); }); describe('postpone - new task creation', () => { From ba9e0724eb44151f698a40b9080f90ae2fd84d1a Mon Sep 17 00:00:00 2001 From: Ilyas Landikov Date: Tue, 12 Dec 2023 15:55:59 +0600 Subject: [PATCH 2/4] fix: - better menu item names for future tasks --- src/Scripting/Postponer.ts | 22 ++++++++++++++++------ tests/Scripting/Postponer.test.ts | 15 +++++++-------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/Scripting/Postponer.ts b/src/Scripting/Postponer.ts index 9c6daa55ef..2e7904e27c 100644 --- a/src/Scripting/Postponer.ts +++ b/src/Scripting/Postponer.ts @@ -1,4 +1,4 @@ -import type { Moment, unitOfTime } from 'moment'; +import moment, { type Moment, type unitOfTime } from 'moment'; import { Task } from '../Task'; import { TasksDate } from './TasksDate'; @@ -63,11 +63,21 @@ export function postponeMenuItemTitle(task: Task, amount: number, timeUnit: unit } const updatedDateType = getDateFieldToPostpone(task)!; const dateToUpdate = task[updatedDateType] as Moment; - const updatedDateDisplayText = capitalizeFirstLetter(updatedDateType.replace('Date', '')); + if (dateToUpdate.isSameOrBefore(moment(), 'day')) { + const updatedDateDisplayText = capitalizeFirstLetter(updatedDateType.replace('Date', '')); - const postponedDate = new TasksDate(dateToUpdate).postpone(timeUnit, amount); - const formattedNewDate = postponedDate.format('ddd Do MMM'); + const postponedDate = new TasksDate(dateToUpdate).postpone(timeUnit, amount); + const formattedNewDate = postponedDate.format('ddd Do MMM'); - const amountOrArticle = amount > 1 ? amount : 'a'; - return `${updatedDateDisplayText} in ${amountOrArticle} ${timeUnit}, on ${formattedNewDate}`; + const amountOrArticle = amount > 1 ? amount : 'a'; + return `${updatedDateDisplayText} in ${amountOrArticle} ${timeUnit}, on ${formattedNewDate}`; + } else { + const updatedDateDisplayText = updatedDateType.replace('Date', ' date'); + const amountOrArticle = amount > 1 ? amount : 'a'; + + const postponedDate = new TasksDate(dateToUpdate).postpone(timeUnit, amount); + const formattedNewDate = postponedDate.format('ddd Do MMM'); + // 'Postpone due date by a day, to Tue 5th Dec') + return `Postpone ${updatedDateDisplayText} by ${amountOrArticle} ${timeUnit}, to ${formattedNewDate}`; + } } diff --git a/tests/Scripting/Postponer.test.ts b/tests/Scripting/Postponer.test.ts index bdd230deb5..e4807597e1 100644 --- a/tests/Scripting/Postponer.test.ts +++ b/tests/Scripting/Postponer.test.ts @@ -174,24 +174,23 @@ describe('postpone - UI text', () => { it('should include date type and new date in context menu labels when due today', () => { const task = new TaskBuilder().dueDate(today).build(); - // TODO This text is misleading if the date is already in the future. - // In that case, it should still be 'Postpone'??? + expect(postponeMenuItemTitle(task, 1, 'day')).toEqual('Due in a day, on Mon 4th Dec'); expect(postponeMenuItemTitle(task, 2, 'days')).toEqual('Due in 2 days, on Tue 5th Dec'); }); it('should include date type and new date in context menu labels when overdue', () => { - const task = new TaskBuilder().dueDate(yesterday).build(); + const task = new TaskBuilder().scheduledDate(yesterday).build(); - expect(postponeMenuItemTitle(task, 1, 'day')).toEqual('Due in a day, on Mon 4th Dec'); - expect(postponeMenuItemTitle(task, 2, 'days')).toEqual('Due in 2 days, on Tue 5th Dec'); + expect(postponeMenuItemTitle(task, 1, 'day')).toEqual('Scheduled in a day, on Mon 4th Dec'); + expect(postponeMenuItemTitle(task, 2, 'days')).toEqual('Scheduled in 2 days, on Tue 5th Dec'); }); it('should include date type and new date in context menu labels when due in future', () => { - const task = new TaskBuilder().dueDate(tomorrow).build(); + const task = new TaskBuilder().startDate(tomorrow).build(); - expect(postponeMenuItemTitle(task, 1, 'day')).toEqual('Due in a day, on Tue 5th Dec'); - expect(postponeMenuItemTitle(task, 2, 'days')).toEqual('Due in 2 days, on Wed 6th Dec'); + expect(postponeMenuItemTitle(task, 1, 'day')).toEqual('Postpone start date by a day, to Tue 5th Dec'); + expect(postponeMenuItemTitle(task, 2, 'days')).toEqual('Postpone start date by 2 days, to Wed 6th Dec'); }); }); From 6e555d55b1eb47c8bbe194e39cee7f8e43cba04e Mon Sep 17 00:00:00 2001 From: Ilyas Landikov Date: Tue, 12 Dec 2023 16:00:05 +0600 Subject: [PATCH 3/4] refactor: - remove duplicated code --- src/Scripting/Postponer.ts | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/Scripting/Postponer.ts b/src/Scripting/Postponer.ts index 2e7904e27c..7179c2be61 100644 --- a/src/Scripting/Postponer.ts +++ b/src/Scripting/Postponer.ts @@ -61,23 +61,19 @@ export function postponeMenuItemTitle(task: Task, amount: number, timeUnit: unit function capitalizeFirstLetter(word: string) { return word.charAt(0).toUpperCase() + word.slice(1); } + const updatedDateType = getDateFieldToPostpone(task)!; const dateToUpdate = task[updatedDateType] as Moment; - if (dateToUpdate.isSameOrBefore(moment(), 'day')) { - const updatedDateDisplayText = capitalizeFirstLetter(updatedDateType.replace('Date', '')); - const postponedDate = new TasksDate(dateToUpdate).postpone(timeUnit, amount); - const formattedNewDate = postponedDate.format('ddd Do MMM'); + const postponedDate = new TasksDate(dateToUpdate).postpone(timeUnit, amount); + const formattedNewDate = postponedDate.format('ddd Do MMM'); - const amountOrArticle = amount > 1 ? amount : 'a'; + const amountOrArticle = amount > 1 ? amount : 'a'; + if (dateToUpdate.isSameOrBefore(moment(), 'day')) { + const updatedDateDisplayText = capitalizeFirstLetter(updatedDateType.replace('Date', '')); return `${updatedDateDisplayText} in ${amountOrArticle} ${timeUnit}, on ${formattedNewDate}`; } else { const updatedDateDisplayText = updatedDateType.replace('Date', ' date'); - const amountOrArticle = amount > 1 ? amount : 'a'; - - const postponedDate = new TasksDate(dateToUpdate).postpone(timeUnit, amount); - const formattedNewDate = postponedDate.format('ddd Do MMM'); - // 'Postpone due date by a day, to Tue 5th Dec') return `Postpone ${updatedDateDisplayText} by ${amountOrArticle} ${timeUnit}, to ${formattedNewDate}`; } } From 34fb408bbd391feec4aa78bf56a08eb9c3cb4679 Mon Sep 17 00:00:00 2001 From: Ilyas Landikov Date: Tue, 12 Dec 2023 16:15:55 +0600 Subject: [PATCH 4/4] refactor: - get moment() from Obsidian --- src/Scripting/Postponer.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Scripting/Postponer.ts b/src/Scripting/Postponer.ts index 7179c2be61..178806c8a5 100644 --- a/src/Scripting/Postponer.ts +++ b/src/Scripting/Postponer.ts @@ -1,4 +1,4 @@ -import moment, { type Moment, type unitOfTime } from 'moment'; +import type { Moment, unitOfTime } from 'moment'; import { Task } from '../Task'; import { TasksDate } from './TasksDate'; @@ -69,7 +69,7 @@ export function postponeMenuItemTitle(task: Task, amount: number, timeUnit: unit const formattedNewDate = postponedDate.format('ddd Do MMM'); const amountOrArticle = amount > 1 ? amount : 'a'; - if (dateToUpdate.isSameOrBefore(moment(), 'day')) { + if (dateToUpdate.isSameOrBefore(window.moment(), 'day')) { const updatedDateDisplayText = capitalizeFirstLetter(updatedDateType.replace('Date', '')); return `${updatedDateDisplayText} in ${amountOrArticle} ${timeUnit}, on ${formattedNewDate}`; } else {