-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Timeline]: Earliest date defaulted to current date when found date w…
…as later than current day (#3458) * 🐛 Timeline startDate defaulted to current Date when later than current day * 🧪 Added tests * Update @navikt/core/react/src/timeline/hooks/useTimelineRows.ts Co-authored-by: Halvor Haugan <[email protected]> * 🔥 Removed old exceptions * 📝 Changeset --------- Co-authored-by: Halvor Haugan <[email protected]>
- Loading branch information
1 parent
3b43ad5
commit 6d6900c
Showing
3 changed files
with
161 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@navikt/ds-react": patch | ||
--- | ||
|
||
Timeline: In cases where earliest found date were after current date, timeline-start ended up defaulting to current date. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
@navikt/core/react/src/timeline/tests/useTimelineRows.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import { renderHook } from "@testing-library/react"; | ||
import { addDays, isSameDay } from "date-fns"; | ||
import { describe, expect, test } from "vitest"; | ||
import { | ||
useEarliestDate, | ||
useLatestDate, | ||
useTimelineRows, | ||
} from "../hooks/useTimelineRows"; | ||
|
||
describe("useEarliestDate", () => { | ||
test("returns the provided startDate if it exists", () => { | ||
const startDate = new Date(2023, 0, 1); | ||
const { result } = renderHook(() => | ||
useEarliestDate({ startDate, rows: [] }), | ||
); | ||
expect(result.current).toEqual(startDate); | ||
}); | ||
|
||
test("returns the earliest date from the rows if startDate is not provided", () => { | ||
const rows = [ | ||
[{ start: new Date(2023, 0, 1) }], | ||
[{ start: new Date(2022, 0, 1) }], | ||
]; | ||
|
||
const { result } = renderHook(() => useEarliestDate({ rows })); | ||
expect(result.current).toEqual(new Date(2022, 0, 1)); | ||
}); | ||
|
||
test("returns the earliest date from the rows if startDate is not provided and date is later than todays date", () => { | ||
const earliestDate = addDays(new Date(), 400); | ||
const rows = [ | ||
[{ start: earliestDate }], | ||
[{ start: addDays(earliestDate, 40) }], | ||
]; | ||
|
||
const { result } = renderHook(() => useEarliestDate({ rows })); | ||
expect(result.current).toEqual(earliestDate); | ||
}); | ||
|
||
test("returns the current date if no startDate and rows are empty", () => { | ||
const { result } = renderHook(() => useEarliestDate({ rows: [] })); | ||
expect(isSameDay(result.current, new Date())).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
describe("useLatestDate", () => { | ||
test("returns the provided endDate if it exists", () => { | ||
const endDate = new Date(2023, 0, 1); | ||
const { result } = renderHook(() => useLatestDate({ endDate, rows: [] })); | ||
expect(result.current).toEqual(endDate); | ||
}); | ||
|
||
test("returns the latest date from the rows plus one day if endDate is not provided", () => { | ||
const rows = [ | ||
[{ start: new Date(2023, 0, 1), end: new Date(2023, 0, 10) }], | ||
[{ start: new Date(2022, 0, 1), end: new Date(2022, 0, 5) }], | ||
]; | ||
const { result } = renderHook(() => useLatestDate({ rows })); | ||
expect(result.current).toEqual(addDays(new Date(2023, 0, 10), 1)); | ||
}); | ||
|
||
test("returns the current date plus one day if no endDate and rows are empty", () => { | ||
const { result } = renderHook(() => useLatestDate({ rows: [] })); | ||
expect(result.current).toEqual(addDays(new Date(0), 1)); | ||
}); | ||
}); | ||
|
||
describe("useTimelineRows", () => { | ||
const rows = [ | ||
{ | ||
label: "Row 1", | ||
periods: [ | ||
{ | ||
start: new Date(2023, 0, 1), | ||
end: new Date(2023, 0, 10), | ||
status: "active", | ||
}, | ||
{ | ||
start: new Date(2023, 0, 15), | ||
end: new Date(2023, 0, 20), | ||
status: "inactive", | ||
}, | ||
], | ||
}, | ||
{ | ||
label: "Row 2", | ||
periods: [ | ||
{ | ||
start: new Date(2022, 0, 1), | ||
end: new Date(2022, 0, 5), | ||
status: "active", | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
test("returns the correct timeline rows", () => { | ||
const startDate = new Date(2022, 0, 1); | ||
const endDate = new Date(2023, 0, 31); | ||
const direction = "left"; | ||
const { result } = renderHook(() => | ||
useTimelineRows(rows, startDate, endDate, direction), | ||
); | ||
|
||
expect(result.current).toHaveLength(2); | ||
expect(result.current[0].periods).toHaveLength(2); | ||
expect(result.current[1].periods).toHaveLength(1); | ||
}); | ||
|
||
test("handles empty rows", () => { | ||
const startDate = new Date(2022, 0, 1); | ||
const endDate = new Date(2023, 0, 31); | ||
const direction = "left"; | ||
const { result } = renderHook(() => | ||
useTimelineRows([], startDate, endDate, direction), | ||
); | ||
|
||
expect(result.current).toHaveLength(0); | ||
}); | ||
|
||
test("handles different directions", () => { | ||
const startDate = new Date(2022, 0, 1); | ||
const endDate = new Date(2023, 0, 31); | ||
const direction = "right"; | ||
const { result } = renderHook(() => | ||
useTimelineRows(rows, startDate, endDate, direction), | ||
); | ||
|
||
expect(result.current[0].periods[0].start).toEqual(new Date(2023, 0, 15)); | ||
}); | ||
}); |