Skip to content

Commit

Permalink
add unit test to summariseTimeOnline. Fix sleep and wake times.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maigo Erit committed May 25, 2020
1 parent fe7e3bb commit 9103ea7
Show file tree
Hide file tree
Showing 8 changed files with 623 additions and 45 deletions.
6 changes: 6 additions & 0 deletions client/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
]
}
4 changes: 4 additions & 0 deletions client/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
6 changes: 3 additions & 3 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,21 @@
"scripts": {
"start": "cross-env ELECTRON_ENABLE_SECURITY_WARNINGS=false BROWSER=none LOG_LEVEL=debug react-app-rewired start",
"build": "cross-env PUBLIC_URL=./ react-app-rewired build",
"test": "react-app-rewired test --env=jsdom",
"test": "yarn jest",
"eject": "react-app-rewired eject",
"lint": "eslint 'src/**/*{.ts,.tsx}' --fix"
},
"devDependencies": {
"@babel/cli": "^7.8.3",
"@babel/core": "^7.8.3",
"@babel/preset-env": "^7.8.3",
"@types/jest": "^24.9.1",
"@types/jest": "^25.2.3",
"@types/node": "^13.5.0",
"@types/react": "^16.9.19",
"@types/react-dom": "^16.9.5",
"@types/webpack-env": "1.15.0",
"babel-eslint": "^10.0.3",
"babel-plugin-import": "1.13.0",
"devtron": "^1.4.0",
"eslint": "^6.8.0",
"eslint-config-airbnb": "^18.0.1",
"eslint-config-prettier": "^6.9.0",
Expand All @@ -67,6 +66,7 @@
"react-app-rewire-less": "2.1.3",
"react-app-rewired": "2.1.5",
"react-hot-loader": "4.12.19",
"ts-jest": "^26.0.0",
"typescript": "3.7.5"
},
"browserslist": [
Expand Down
7 changes: 5 additions & 2 deletions client/src/components/LineCharts/LineChart.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useContext } from 'react';
import moment from 'moment';
import { values } from 'lodash';
import { VictoryBar, VictoryChart, VictoryAxis, VictoryTooltip } from 'victory';
import { convertDate, TIME_FORMAT, COLORS } from '../../constants';
import { chartTheme } from '../Timeline/ChartTheme';
Expand Down Expand Up @@ -30,6 +31,8 @@ export const LineChart = () => {
const chartWidth = useWindowWidth();
const { onlineTimesSummary } = useContext(SummaryContext);

const onlineTimesValues = values(onlineTimesSummary);

return (
<VictoryChart
theme={chartTheme}
Expand All @@ -52,7 +55,7 @@ export const LineChart = () => {
y0={d => toTimeDuration(convertDate(d.beginDate), convertDate(d.endDate))}
x={d => convertDate(d.beginDate).startOf('day')}
barWidth={10}
data={onlineTimesSummary}
data={onlineTimesValues}
labelComponent={labelComponent()}
labels={({ datum }) =>
`Start time: ${convertDate(datum.beginDate).format(
Expand All @@ -68,7 +71,7 @@ export const LineChart = () => {
x={d => convertDate(d.beginDate).startOf('day')}
barWidth={10}
style={{ data: { fill: COLORS.green } }}
data={onlineTimesSummary}
data={onlineTimesValues}
labelComponent={labelComponent()}
labels={({ datum }) => `Worked: ${moment.duration(datum.online).format()}`}
/>
Expand Down
96 changes: 96 additions & 0 deletions client/src/components/SummaryCalendar/SummaryCalendar.util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { summariseTimeOnline } from './SummaryCalendar.util';
import { TEST_INPUT } from './SummaryCalendar.util.testdata';
import moment from 'moment';

describe('summariseTimeOnline', () => {
it('parses online items', () => {
const actual = summariseTimeOnline(TEST_INPUT, 'month');
const result = {
'12': { beginDate: 1589311528755, endDate: 1589312113568, online: 358396 },
'14': { beginDate: 1589480558049, endDate: 1589483666850, online: 2707998 },
'24': { beginDate: 1590354279611, endDate: 1590357497348, online: 3085853 },
'25': { beginDate: 1590375522178, endDate: 1590388476539, online: 1527164 },
};

expect(actual).toEqual(result);
});

it('online item over midnight - until 04:00 - it goes to previous day', () => {
const input = [
{
app: 'ONLINE',
beginDate: moment('2020-04-10 02:30').valueOf(),
endDate: moment('2020-04-10 03:30').valueOf(),
},
];
const actual = summariseTimeOnline(input, 'month');
const result = {
'9': {
beginDate: moment('2020-04-10 02:30').valueOf(),
endDate: moment('2020-04-10 03:30').valueOf(),
online: 3600000,
},
};

expect(actual).toEqual(result);
});

it('online item after 04:00 goes to correct day', () => {
const input = [
{
app: 'ONLINE',
beginDate: moment('2020-04-10 04:30').valueOf(),
endDate: moment('2020-04-10 05:30').valueOf(),
},
];
const actual = summariseTimeOnline(input, 'month');
const result = {
'10': {
beginDate: moment('2020-04-10 04:30').valueOf(),
endDate: moment('2020-04-10 05:30').valueOf(),
online: 3600000,
},
};

expect(actual).toEqual(result);
});

it('woke up 04:30 and went to sleep 03:30', () => {
const input = [
{
app: 'ONLINE',
beginDate: moment('2020-04-10 04:30').valueOf(),
endDate: moment('2020-04-10 05:30').valueOf(),
},
{
app: 'ONLINE',
beginDate: moment('2020-04-10 11:30').valueOf(),
endDate: moment('2020-04-10 15:30').valueOf(),
},
{
app: 'ONLINE',
beginDate: moment('2020-04-10 23:30').valueOf(),
endDate: moment('2020-04-11 01:30').valueOf(),
},
{
app: 'ONLINE',
beginDate: moment('2020-04-11 02:30').valueOf(),
endDate: moment('2020-04-11 03:30').valueOf(),
},
];
const actual = summariseTimeOnline(input, 'month');
const result = {
'10': {
beginDate: moment('2020-04-10 04:30').valueOf(),
endDate: moment('2020-04-11 03:30').valueOf(),
online: 28800000,
},
};

expect(actual).toEqual(result);
});
});

export // Use an empty export to please Babel's single file emit.
// https://github.com/Microsoft/TypeScript/issues/15230
{};
Loading

0 comments on commit 9103ea7

Please sign in to comment.