-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1138 from JGreenlee/refactoring_timelinecontext
⏱️ Implement TimelineContext (refactoring of LabelTabContext)
- Loading branch information
Showing
40 changed files
with
1,110 additions
and
747 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
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,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); | ||
}); | ||
}); | ||
}); |
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
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,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', () => { | ||
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'); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.