-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
⏱️ Implement TimelineContext (refactoring of LabelTabContext) #1138
Changes from all commits
b1adc4f
b18e610
0e86c12
8d34461
2546629
49c4989
45e3811
1c195bf
368f6c8
1a12b8b
f165a3f
751c4d0
94e4020
072c220
c5f6970
6c07e44
4a22227
ed768a7
126e46a
baf79c0
13ce501
be21343
0136eeb
cc7434f
dfd3376
4ca6e3f
5ae3ea8
00c655c
82e0382
fe4e6eb
4da2761
da7283e
1269aad
f1961a6
cf9aaf7
21b9faa
91af26c
b690c0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import React, { useEffect } from 'react'; | ||
import { View, Text } from 'react-native'; | ||
import { act, render, screen, waitFor } from '@testing-library/react-native'; | ||
import { useTimelineContext } from '../js/TimelineContext'; | ||
import { mockLogger } from '../__mocks__/globalMocks'; | ||
import { mockBEMServerCom, mockBEMUserCache } from '../__mocks__/cordovaMocks'; | ||
|
||
mockLogger(); | ||
mockBEMUserCache(); | ||
|
||
jest.mock('../js/services/commHelper', () => ({ | ||
getPipelineRangeTs: jest.fn(() => Promise.resolve({ start_ts: 1, end_ts: 10 })), | ||
getRawEntries: jest.fn((key_list, _, __) => { | ||
let phone_data: any[] = []; | ||
if (key_list.includes('analysis/composite_trip')) { | ||
phone_data = [ | ||
{ | ||
_id: { $oid: 'trip1' }, | ||
metadata: { write_ts: 1, origin_key: 'analysis/confirmed_trip' }, | ||
data: { start_ts: 1, end_ts: 2 }, | ||
}, | ||
{ | ||
_id: { $oid: 'trip2' }, | ||
metadata: { write_ts: 2, origin_key: 'analysis/confirmed_trip' }, | ||
data: { start_ts: 3, end_ts: 4 }, | ||
}, | ||
{ | ||
_id: { $oid: 'trip3' }, | ||
metadata: { write_ts: 3, origin_key: 'analysis/confirmed_trip' }, | ||
data: { start_ts: 5, end_ts: 6 }, | ||
}, | ||
]; | ||
} | ||
return Promise.resolve({ phone_data }); | ||
}), | ||
fetchUrlCached: jest.fn(() => Promise.resolve(null)), | ||
})); | ||
|
||
// Mock useAppConfig default export | ||
jest.mock('../js/useAppConfig', () => { | ||
return jest.fn(() => ({ intro: {} })); | ||
}); | ||
|
||
const TimelineContextTestComponent = () => { | ||
const { timelineMap, setDateRange } = useTimelineContext(); | ||
|
||
useEffect(() => { | ||
// setDateRange(['2021-01-01', '2021-01-07']); | ||
}, []); | ||
|
||
if (!timelineMap) return null; | ||
|
||
console.debug('timelineMap', timelineMap); | ||
|
||
return ( | ||
<View testID="timeline-entries"> | ||
{[...timelineMap.values()].map((entry, i) => ( | ||
<Text key={i}>{'entry ID: ' + entry._id.$oid}</Text> | ||
))} | ||
</View> | ||
); | ||
}; | ||
|
||
describe('TimelineContext', () => { | ||
it('renders correctly', async () => { | ||
render(<TimelineContextTestComponent />); | ||
await waitFor(() => { | ||
// make sure timeline entries are rendered | ||
expect(screen.getByTestId('timeline-entries')).toBeTruthy(); | ||
// make sure number of Text components matches number of timeline entries | ||
expect(screen.getAllByText(/entry ID:/).length).toBe(3); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { | ||
calculatePercentChange, | ||
formatDate, | ||
formatDateRangeOfDays, | ||
getLabelsForDay, | ||
getUniqueLabelsForDays, | ||
segmentDaysByWeeks, | ||
} from '../js/metrics/metricsHelper'; | ||
import { | ||
DayOfClientMetricData, | ||
DayOfMetricData, | ||
DayOfServerMetricData, | ||
} from '../js/metrics/metricsTypes'; | ||
|
||
describe('metricsHelper', () => { | ||
describe('getUniqueLabelsForDays', () => { | ||
const days1 = [ | ||
{ label_a: 1, label_b: 2 }, | ||
{ label_c: 1, label_d: 3 }, | ||
] as any as DayOfServerMetricData[]; | ||
it("should return unique labels for days with 'label_*'", () => { | ||
expect(getUniqueLabelsForDays(days1)).toEqual(['a', 'b', 'c', 'd']); | ||
}); | ||
|
||
const days2 = [ | ||
{ mode_a: 1, mode_b: 2 }, | ||
{ mode_c: 1, mode_d: 3 }, | ||
] as any as DayOfClientMetricData[]; | ||
it("should return unique labels for days with 'mode_*'", () => { | ||
expect(getUniqueLabelsForDays(days2)).toEqual(['a', 'b', 'c', 'd']); | ||
}); | ||
}); | ||
|
||
describe('getLabelsForDay', () => { | ||
const day1 = { label_a: 1, label_b: 2 } as any as DayOfServerMetricData; | ||
it("should return labels for a day with 'label_*'", () => { | ||
expect(getLabelsForDay(day1)).toEqual(['a', 'b']); | ||
}); | ||
|
||
const day2 = { mode_a: 1, mode_b: 2 } as any as DayOfClientMetricData; | ||
it("should return labels for a day with 'mode_*'", () => { | ||
expect(getLabelsForDay(day2)).toEqual(['a', 'b']); | ||
}); | ||
}); | ||
|
||
// secondsToMinutes | ||
|
||
// secondsToHours | ||
|
||
describe('segmentDaysByWeeks', () => { | ||
const days1 = [ | ||
{ date: '2021-01-01' }, | ||
{ date: '2021-01-02' }, | ||
{ date: '2021-01-04' }, | ||
{ date: '2021-01-08' }, | ||
{ date: '2021-01-09' }, | ||
{ date: '2021-01-10' }, | ||
] as any as DayOfClientMetricData[]; | ||
|
||
it("should segment days with 'date' into weeks", () => { | ||
expect(segmentDaysByWeeks(days1, '2021-01-10')).toEqual([ | ||
// most recent week | ||
[ | ||
{ date: '2021-01-04' }, | ||
{ date: '2021-01-08' }, | ||
{ date: '2021-01-09' }, | ||
{ date: '2021-01-10' }, | ||
], | ||
// prior week | ||
[{ date: '2021-01-01' }, { date: '2021-01-02' }], | ||
]); | ||
}); | ||
|
||
const days2 = [ | ||
{ fmt_time: '2021-01-01T00:00:00Z' }, | ||
{ fmt_time: '2021-01-02T00:00:00Z' }, | ||
{ fmt_time: '2021-01-04T00:00:00Z' }, | ||
{ fmt_time: '2021-01-08T00:00:00Z' }, | ||
{ fmt_time: '2021-01-09T00:00:00Z' }, | ||
{ fmt_time: '2021-01-10T00:00:00Z' }, | ||
] as any as DayOfServerMetricData[]; | ||
it("should segment days with 'fmt_time' into weeks", () => { | ||
expect(segmentDaysByWeeks(days2, '2021-01-10')).toEqual([ | ||
// most recent week | ||
[ | ||
{ fmt_time: '2021-01-04T00:00:00Z' }, | ||
{ fmt_time: '2021-01-08T00:00:00Z' }, | ||
{ fmt_time: '2021-01-09T00:00:00Z' }, | ||
{ fmt_time: '2021-01-10T00:00:00Z' }, | ||
], | ||
// prior week | ||
[{ fmt_time: '2021-01-01T00:00:00Z' }, { fmt_time: '2021-01-02T00:00:00Z' }], | ||
]); | ||
}); | ||
}); | ||
|
||
describe('formatDate', () => { | ||
const day1 = { date: '2021-01-01' } as any as DayOfClientMetricData; | ||
it('should format date', () => { | ||
expect(formatDate(day1)).toEqual('1/1'); | ||
}); | ||
|
||
const day2 = { fmt_time: '2021-01-01T00:00:00Z' } as any as DayOfServerMetricData; | ||
it('should format date', () => { | ||
expect(formatDate(day2)).toEqual('1/1'); | ||
}); | ||
}); | ||
|
||
describe('formatDateRangeOfDays', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should also add tests for the part before or after the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what do you mean by this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean that you should have tests that result in strings of the form |
||
const days1 = [ | ||
{ date: '2021-01-01' }, | ||
{ date: '2021-01-02' }, | ||
{ date: '2021-01-04' }, | ||
] as any as DayOfClientMetricData[]; | ||
it('should format date range for days with date', () => { | ||
expect(formatDateRangeOfDays(days1)).toEqual('1/1 - 1/4'); | ||
}); | ||
|
||
const days2 = [ | ||
{ fmt_time: '2021-01-01T00:00:00Z' }, | ||
{ fmt_time: '2021-01-02T00:00:00Z' }, | ||
{ fmt_time: '2021-01-04T00:00:00Z' }, | ||
] as any as DayOfServerMetricData[]; | ||
it('should format date range for days with fmt_time', () => { | ||
expect(formatDateRangeOfDays(days2)).toEqual('1/1 - 1/4'); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not really testing the uniqueness of the labels for the day. Since the labels are all unique, this seems identical to
getLabelsForDay
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right; this should test a case where one label is present across multiple days
TODO