Skip to content

Commit 4109d77

Browse files
committed
Update calcNowOffset and add tests
1 parent af5c64b commit 4109d77

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/timeline/__tests__/presenter.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,19 @@ describe('timeline presenter', () => {
6868
expect(uut.buildTimeString(15, 0, '2017-03-05')).toBe('2017-03-05 15:00:00');
6969
});
7070
});
71+
72+
describe('calcNowOffset', () => {
73+
it('should give offset based on current time', () => {
74+
jest.useFakeTimers().setSystemTime(new Date('2020-01-01 15:30').getTime());
75+
expect(uut.calcNowOffset(100)).toBe(1550);
76+
77+
jest.useFakeTimers().setSystemTime(new Date('2020-01-01 12:10').getTime());
78+
expect(uut.calcNowOffset(100)).toBeCloseTo(1216.66, 1);
79+
});
80+
81+
it('should give offset based on given time', () => {
82+
expect(uut.calcNowOffset(100, 15, 30)).toBe(1550);
83+
expect(uut.calcNowOffset(100, 12, 10)).toBeCloseTo(1216.66, 1);
84+
});
85+
});
7186
});

src/timeline/helpers/presenter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ export function buildTimeString(hour = 0, minutes = 0, date = '') {
1111
return `${date} ${hour.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:00`.trimStart();
1212
}
1313

14-
export function calcNowOffset(hourBlockHeight: number) {
14+
export function calcNowOffset(hourBlockHeight: number, hour?: number, minutes?: number) {
1515
const now = new Date();
16-
const hour = now.getHours();
17-
const minutes = now.getMinutes();
18-
return (hour + minutes / 60) * hourBlockHeight;
16+
const h = hour ?? now.getHours();
17+
const m = minutes ?? now.getMinutes();
18+
return (h + m / 60) * hourBlockHeight;
1919
}

0 commit comments

Comments
 (0)