Skip to content

Commit 5bec831

Browse files
committed
Test improvements to calcDateOffsetStr and other new tests and edge case fix
1 parent f8114d3 commit 5bec831

File tree

3 files changed

+338
-87
lines changed

3 files changed

+338
-87
lines changed

helpers/NPWindows.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,10 @@
55
// ----------------------------------------------------------------------------
66

77
import { clo, logDebug, logError, logInfo, logWarn } from '@helpers/dev'
8+
import { usersVersionHas } from '@helpers/NPVersions'
89
import { caseInsensitiveMatch, caseInsensitiveStartsWith } from '@helpers/search'
910
import { inputIntegerBounded } from '@helpers/userInput'
1011

11-
// ----------------------------------------------------------------------------
12-
// CONSTANTS
13-
14-
export const MAIN_SIDEBAR_CONTROL_BUILD_VERSION = 1440 // v3.19.2
15-
export const FOLDER_VIEWS_CONTROL_BUILD_VERSION = 1340 // v3.18???
16-
1712
// ----------------------------------------------------------------------------
1813
// TYPES
1914

@@ -78,7 +73,7 @@ export async function setEditorWidth(widthIn?: number, mainSidebarWidth?: number
7873
}
7974

8075
logDebug('setEditorWidth', `Attempting to set width for main NP Window to ${String(width)}`)
81-
if (NotePlan.environment.buildVersion >= MAIN_SIDEBAR_CONTROL_BUILD_VERSION && mainSidebarWidth && !isNaN(mainSidebarWidth)) {
76+
if (usersVersionHas('mainSidebarControl') && mainSidebarWidth && !isNaN(mainSidebarWidth)) {
8277
if (mainSidebarWidth === 0) {
8378
logDebug('setEditorWidth', `- will hide main sidebar`)
8479
NotePlan.toggleSidebar(true, false, true)
@@ -690,7 +685,7 @@ export async function constrainMainWindow(): Promise<void> {
690685
}
691686

692687
export function logSidebarWidth(): void {
693-
if (NotePlan.environment.buildVersion >= MAIN_SIDEBAR_CONTROL_BUILD_VERSION) {
688+
if (usersVersionHas('mainSidebarControl')) {
694689
const sidebarWidth = NotePlan.getSidebarWidth()
695690
logInfo('logSidebarWidth', `Sidebar width: ${sidebarWidth} -- WARNING: This cannot tell if the sidebar is actually visible or not!`)
696691
} else {
@@ -700,7 +695,7 @@ export function logSidebarWidth(): void {
700695

701696
// eslint-disable-next-line require-await
702697
export async function setSidebarWidth(widthIn?: number): Promise<void> {
703-
if (NotePlan.environment.buildVersion >= MAIN_SIDEBAR_CONTROL_BUILD_VERSION) {
698+
if (usersVersionHas('mainSidebarControl')) {
704699
const width = widthIn ?? await inputIntegerBounded('Set Width for main NP Window', `Width (pixels)? (up to ${String(NotePlan.environment.screenWidth)})`, NotePlan.environment.screenWidth)
705700
NotePlan.setSidebarWidth(width)
706701
logDebug('setSidebarWidth', `Sidebar width set to ${width}`)

helpers/__tests__/dateTime.test.js

Lines changed: 254 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -290,35 +290,39 @@ describe(`${PLUGIN_NAME}`, () => {
290290

291291
// @dwertheimer
292292
describe('getDateObjFromDateTimeString ', () => {
293-
test('should create date and HH:MM from string, no seconds', () => {
294-
expect(dt.getDateObjFromDateTimeString('2021-01-01 09:40').toTimeString()).toMatch(/09:40:00/) //not checking date b/c it's locale-dependent
295-
})
296-
test('should work with seconds specified', () => {
297-
expect(dt.getDateObjFromDateTimeString('2021-01-02 00:00:01').toTimeString()).toMatch(/00:00:01/)
298-
})
299-
test('should work with only date, no time given', () => {
300-
expect(dt.getDateObjFromDateTimeString('2021-01-03').toTimeString()).toMatch(/00:00:00/) //not checking date b/c it's locale-dependent
301-
})
302-
// Errors should throw
303-
test('should throw error when date format is incorrect', () => {
304-
expect(() => {
305-
dt.getDateObjFromDateTimeString(`foo 00:00`)
306-
}).toThrow(/not in expected format/)
307-
})
308-
test('should throw error when date format is incorrect (no day)', () => {
309-
expect(() => {
310-
dt.getDateObjFromDateTimeString(`2020-04 02:02`)
311-
}).toThrow(/not in expected format/)
312-
})
313-
test('should throw error when time format is incorrect', () => {
314-
expect(() => {
315-
dt.getDateObjFromDateTimeString(`2020-01-05 02`)
316-
}).toThrow(/not in expected format/)
293+
describe('should work', () => {
294+
test('should create date and HH:MM from string, no seconds', () => {
295+
expect(dt.getDateObjFromDateTimeString('2021-01-01 09:40').toTimeString()).toMatch(/09:40:00/) //not checking date b/c it's locale-dependent
296+
})
297+
test('should work with seconds specified', () => {
298+
expect(dt.getDateObjFromDateTimeString('2021-01-02 00:00:01').toTimeString()).toMatch(/00:00:01/)
299+
})
300+
test('should work with only date, no time given', () => {
301+
expect(dt.getDateObjFromDateTimeString('2021-01-03').toTimeString()).toMatch(/00:00:00/) //not checking date b/c it's locale-dependent
302+
})
317303
})
318-
test('should throw error when time format is incorrect', () => {
319-
expect(() => {
320-
dt.getDateObjFromDateTimeString(`2020-01-06 aa:00`)
321-
}).toThrow(/Invalid Date/)
304+
305+
describe('errors', () => {
306+
test('should throw error when date format is incorrect', () => {
307+
expect(() => {
308+
dt.getDateObjFromDateTimeString(`foo 00:00`)
309+
}).toThrow(/not in expected format/)
310+
})
311+
test('should throw error when date format is incorrect (no day)', () => {
312+
expect(() => {
313+
dt.getDateObjFromDateTimeString(`2020-04 02:02`)
314+
}).toThrow(/not in expected format/)
315+
})
316+
test('should throw error when time format is incorrect', () => {
317+
expect(() => {
318+
dt.getDateObjFromDateTimeString(`2020-01-05 02`)
319+
}).toThrow(/not in expected format/)
320+
})
321+
test('should throw error when time format is incorrect', () => {
322+
expect(() => {
323+
dt.getDateObjFromDateTimeString(`2020-01-06 aa:00`)
324+
}).toThrow(/Invalid Date/)
325+
})
322326
})
323327

324328
describe('getDateObjFromString mocked date', () => {
@@ -336,35 +340,9 @@ describe(`${PLUGIN_NAME}`, () => {
336340
})
337341
})
338342

339-
test('getTimeStringFromDate should return time portion of Date as string HH:MM', () => {
340-
expect(dt.getTimeStringFromDate(new Date('2020-01-01 23:59'))).toEqual('23:59')
341-
})
342-
343-
describe('withinDateRange', () => {
344-
test('test 1', () => {
345-
expect(dt.withinDateRange('20210424', '20210501', '20210531')).toEqual(false)
346-
})
347-
test('test 2', () => {
348-
expect(dt.withinDateRange('20210501', '20210501', '20210531')).toEqual(true)
349-
})
350-
test('test 3', () => {
351-
expect(dt.withinDateRange('20210524', '20210501', '20210531')).toEqual(true)
352-
})
353-
test('test 4', () => {
354-
expect(dt.withinDateRange('20210531', '20210501', '20210531')).toEqual(true)
355-
})
356-
test('test 5', () => {
357-
expect(dt.withinDateRange('20210624', '20210501', '20210531')).toEqual(false)
358-
})
359-
test('test 6 over year boundary', () => {
360-
expect(dt.withinDateRange('20240101', '20231201', '20240201')).toEqual(true)
361-
})
362-
test('test 7 on a valid leap day', () => {
363-
expect(dt.withinDateRange('20240229', '20240201', '20240301')).toEqual(true)
364-
})
365-
// TODO: fix this edge case
366-
test.skip('test 8 on an invalid leap day', () => {
367-
expect(dt.withinDateRange('20230229', '20230201', '20230301')).toEqual(false)
343+
describe('getTimeStringFromDate', () => {
344+
test('should return time portion of Date as string HH:MM', () => {
345+
expect(dt.getTimeStringFromDate(new Date('2020-01-01 23:59'))).toEqual('23:59')
368346
})
369347
})
370348

@@ -427,12 +405,229 @@ describe(`${PLUGIN_NAME}`, () => {
427405
})
428406
})
429407

408+
describe('withinDateRange', () => {
409+
test('test 1', () => {
410+
expect(dt.withinDateRange('20210424', '20210501', '20210531')).toEqual(false)
411+
})
412+
test('test 2', () => {
413+
expect(dt.withinDateRange('20210501', '20210501', '20210531')).toEqual(true)
414+
})
415+
test('test 3', () => {
416+
expect(dt.withinDateRange('20210524', '20210501', '20210531')).toEqual(true)
417+
})
418+
test('test 4', () => {
419+
expect(dt.withinDateRange('20210531', '20210501', '20210531')).toEqual(true)
420+
})
421+
test('test 5', () => {
422+
expect(dt.withinDateRange('20210624', '20210501', '20210531')).toEqual(false)
423+
})
424+
test('test 6 over year boundary', () => {
425+
expect(dt.withinDateRange('20240101', '20231201', '20240201')).toEqual(true)
426+
})
427+
test('test 7 on a valid leap day', () => {
428+
expect(dt.withinDateRange('20240229', '20240201', '20240301')).toEqual(true)
429+
})
430+
test('test 8 on an invalid leap day', () => {
431+
expect(dt.withinDateRange('20230229', '20230201', '20230301')).toEqual(false)
432+
})
433+
})
434+
430435
describe('relativeDateFromNumber', () => {
431-
// TODO: this can be tested
436+
describe('default style (long format)', () => {
437+
test('should return "today" for 0 days', () => {
438+
expect(dt.relativeDateFromNumber(0)).toEqual('today')
439+
})
440+
test('should return "1 day ago" for -1 days', () => {
441+
expect(dt.relativeDateFromNumber(-1)).toEqual('1 day ago')
442+
})
443+
test('should return "in 1 day" for 1 day', () => {
444+
expect(dt.relativeDateFromNumber(1)).toEqual('in 1 day')
445+
})
446+
test('should return "2 days ago" for -2 days', () => {
447+
expect(dt.relativeDateFromNumber(-2)).toEqual('2 days ago')
448+
})
449+
test('should return "in 2 days" for 2 days', () => {
450+
expect(dt.relativeDateFromNumber(2)).toEqual('in 2 days')
451+
})
452+
test('should return "8 days ago" for -8 days', () => {
453+
expect(dt.relativeDateFromNumber(-8)).toEqual('8 days ago')
454+
})
455+
test('should return "in 8 days" for 8 days', () => {
456+
expect(dt.relativeDateFromNumber(8)).toEqual('in 8 days')
457+
})
458+
test('should return "1 wk ago" for -10 days', () => {
459+
expect(dt.relativeDateFromNumber(-10)).toEqual('1 wk ago')
460+
})
461+
test('should return "in 1 wk" for 10 days', () => {
462+
expect(dt.relativeDateFromNumber(10)).toEqual('in 1 wk')
463+
})
464+
test('should return "3 wks ago" for -21 days', () => {
465+
expect(dt.relativeDateFromNumber(-21)).toEqual('3 wks ago')
466+
})
467+
test('should return "in 3 wks" for 21 days', () => {
468+
expect(dt.relativeDateFromNumber(21)).toEqual('in 3 wks')
469+
})
470+
test('should return "1 mon ago" for -30 days', () => {
471+
expect(dt.relativeDateFromNumber(-30)).toEqual('1 mon ago')
472+
})
473+
test('should return "in 1 mon" for 30 days', () => {
474+
expect(dt.relativeDateFromNumber(30)).toEqual('in 1 mon')
475+
})
476+
test('should return "12 mon ago" for -365 days', () => {
477+
expect(dt.relativeDateFromNumber(-365)).toEqual('12 mon ago')
478+
})
479+
test('should return "in 12 mon" for 365 days', () => {
480+
expect(dt.relativeDateFromNumber(365)).toEqual('in 12 mon')
481+
})
482+
test('should return "16 mon ago" for -500 days (less than 550)', () => {
483+
expect(dt.relativeDateFromNumber(-500)).toEqual('16 mon ago')
484+
})
485+
test('should return "in 16 mon" for 500 days (less than 550)', () => {
486+
expect(dt.relativeDateFromNumber(500)).toEqual('in 16 mon')
487+
})
488+
test('should return "2 yrs ago" for -550 days (550/365 rounds to 2)', () => {
489+
expect(dt.relativeDateFromNumber(-550)).toEqual('2 yrs ago')
490+
})
491+
test('should return "in 2 yrs" for 550 days (550/365 rounds to 2)', () => {
492+
expect(dt.relativeDateFromNumber(550)).toEqual('in 2 yrs')
493+
})
494+
test('should return "2 yrs ago" for -730 days', () => {
495+
expect(dt.relativeDateFromNumber(-730)).toEqual('2 yrs ago')
496+
})
497+
test('should return "in 2 yrs" for 730 days', () => {
498+
expect(dt.relativeDateFromNumber(730)).toEqual('in 2 yrs')
499+
})
500+
})
501+
describe('short style', () => {
502+
test('should return "today" for 0 days', () => {
503+
expect(dt.relativeDateFromNumber(0, true)).toEqual('today')
504+
})
505+
test('should return "1d ago" for -1 days', () => {
506+
expect(dt.relativeDateFromNumber(-1, true)).toEqual('1d ago')
507+
})
508+
test('should return "in 1d" for 1 day', () => {
509+
expect(dt.relativeDateFromNumber(1, true)).toEqual('in 1d')
510+
})
511+
test('should return "8d ago" for -8 days', () => {
512+
expect(dt.relativeDateFromNumber(-8, true)).toEqual('8d ago')
513+
})
514+
test('should return "in 8d" for 8 days', () => {
515+
expect(dt.relativeDateFromNumber(8, true)).toEqual('in 8d')
516+
})
517+
test('should return "1w ago" for -10 days', () => {
518+
expect(dt.relativeDateFromNumber(-10, true)).toEqual('1w ago')
519+
})
520+
test('should return "in 1w" for 10 days', () => {
521+
expect(dt.relativeDateFromNumber(10, true)).toEqual('in 1w')
522+
})
523+
test('should return "3w ago" for -21 days', () => {
524+
expect(dt.relativeDateFromNumber(-21, true)).toEqual('3w ago')
525+
})
526+
test('should return "in 3w" for 21 days', () => {
527+
expect(dt.relativeDateFromNumber(21, true)).toEqual('in 3w')
528+
})
529+
test('should return "1m ago" for -30 days', () => {
530+
expect(dt.relativeDateFromNumber(-30, true)).toEqual('1m ago')
531+
})
532+
test('should return "in 1m" for 30 days', () => {
533+
expect(dt.relativeDateFromNumber(30, true)).toEqual('in 1m')
534+
})
535+
test('should return "12m ago" for -365 days', () => {
536+
expect(dt.relativeDateFromNumber(-365, true)).toEqual('12m ago')
537+
})
538+
test('should return "in 12m" for 365 days', () => {
539+
expect(dt.relativeDateFromNumber(365, true)).toEqual('in 12m')
540+
})
541+
test('should return "16m ago" for -500 days (less than 550)', () => {
542+
expect(dt.relativeDateFromNumber(-500, true)).toEqual('16m ago')
543+
})
544+
test('should return "in 16m" for 500 days (less than 550)', () => {
545+
expect(dt.relativeDateFromNumber(500, true)).toEqual('in 16m')
546+
})
547+
test('should return "2y ago" for -550 days (550/365 rounds to 2)', () => {
548+
expect(dt.relativeDateFromNumber(-550, true)).toEqual('2y ago')
549+
})
550+
test('should return "in 2y" for 550 days (550/365 rounds to 2)', () => {
551+
expect(dt.relativeDateFromNumber(550, true)).toEqual('in 2y')
552+
})
553+
})
554+
describe('edge cases', () => {
555+
test('should return "unknown date" for undefined', () => {
556+
expect(dt.relativeDateFromNumber(undefined)).toEqual('unknown date')
557+
})
558+
test('should return "unknown date" for null', () => {
559+
expect(dt.relativeDateFromNumber(null)).toEqual('unknown date')
560+
})
561+
test('should return "unknown date" for NaN', () => {
562+
expect(dt.relativeDateFromNumber(NaN)).toEqual('unknown date')
563+
})
564+
})
432565
})
433566

434567
describe('getDateFromString', () => {
435-
// TODO: this can be tested
568+
// Note: If this function doesn't exist yet, these tests assume it extracts a Date from various string formats
569+
// Similar to getDateObjFromDateString but potentially with broader format support
570+
test('should extract date from ISO date string', () => {
571+
const result = dt.getDateObjFromDateString('2021-03-04')
572+
expect(result).toBeInstanceOf(Date)
573+
expect(result.getFullYear()).toEqual(2021)
574+
expect(result.getMonth()).toEqual(2) // months are 0-indexed
575+
expect(result.getDate()).toEqual(4)
576+
})
577+
test('should extract date from string containing ISO date', () => {
578+
const result = dt.getDateObjFromDateString('Task due on 2021-03-04')
579+
expect(result).toBeInstanceOf(Date)
580+
expect(result.getFullYear()).toEqual(2021)
581+
expect(result.getMonth()).toEqual(2)
582+
expect(result.getDate()).toEqual(4)
583+
})
584+
test('should extract date from @due format', () => {
585+
const result = dt.getDateObjFromDateString('@due(2021-03-04)')
586+
expect(result).toBeInstanceOf(Date)
587+
expect(result.getFullYear()).toEqual(2021)
588+
expect(result.getMonth()).toEqual(2)
589+
expect(result.getDate()).toEqual(4)
590+
})
591+
test('should extract date from scheduled format', () => {
592+
const result = dt.getDateObjFromDateString('>2021-03-04')
593+
expect(result).toBeInstanceOf(Date)
594+
expect(result.getFullYear()).toEqual(2021)
595+
expect(result.getMonth()).toEqual(2)
596+
expect(result.getDate()).toEqual(4)
597+
})
598+
test('should extract date from link format', () => {
599+
const result = dt.getDateObjFromDateString('[[2021-03-04]]')
600+
expect(result).toBeInstanceOf(Date)
601+
expect(result.getFullYear()).toEqual(2021)
602+
expect(result.getMonth()).toEqual(2)
603+
expect(result.getDate()).toEqual(4)
604+
})
605+
test('should extract first date when multiple dates present', () => {
606+
const result = dt.getDateObjFromDateString('2021-03-04 and 2022-05-15')
607+
expect(result).toBeInstanceOf(Date)
608+
expect(result.getFullYear()).toEqual(2021)
609+
expect(result.getMonth()).toEqual(2)
610+
expect(result.getDate()).toEqual(4)
611+
})
612+
test('should return undefined for string without date', () => {
613+
const result = dt.getDateObjFromDateString('no date here')
614+
expect(result).toBeUndefined()
615+
})
616+
test('should return undefined for empty string', () => {
617+
const result = dt.getDateObjFromDateString('')
618+
expect(result).toBeUndefined()
619+
})
620+
test('should handle YYYYMMDD format if supported', () => {
621+
const result = dt.getDateObjFromDateString('20210304')
622+
// If function supports this format, it should return a Date
623+
// Otherwise, it might return undefined
624+
if (result) {
625+
expect(result).toBeInstanceOf(Date)
626+
expect(result.getFullYear()).toEqual(2021)
627+
expect(result.getMonth()).toEqual(2)
628+
expect(result.getDate()).toEqual(4)
629+
}
630+
})
436631
})
437632

438633
describe('getISODateStringFromYYYYMMDD', () => {
@@ -1050,12 +1245,10 @@ describe(`${PLUGIN_NAME}`, () => {
10501245
const result = dt.getDateStringFromCalendarFilename('2022-.md')
10511246
expect(result).toEqual('(invalid date)')
10521247
})
1053-
// FIXME: "/2025042"
10541248
test('should return valid date for teamspace daily calendar filename', () => {
10551249
const result = dt.getDateStringFromCalendarFilename('%%NotePlanCloud%%/c484b190-77dd-4d40-a05c-e7d7144f24e1/20250422.md')
10561250
expect(result).toEqual('20250422')
10571251
})
1058-
// FIXME: "/2025-W0"
10591252
test('should return valid date for teamspace weekly calendar filename', () => {
10601253
const result = dt.getDateStringFromCalendarFilename('%%NotePlanCloud%%/c484b190-77dd-4d40-a05c-e7d7144f24e1/2025-W01.txt')
10611254
expect(result).toEqual('2025-W01')

0 commit comments

Comments
 (0)