Extract millisFromDuration helper and add tests
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Michael Evans 2022-01-01 17:23:58 -06:00
parent 9ef4cc5853
commit 22dd92f339
4 changed files with 25 additions and 10 deletions

View File

@ -21,7 +21,8 @@
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "echo 'no tests yet' # react-scripts test",
"test": "react-scripts test --watchAll=false",
"test:watch": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {

View File

@ -1,9 +0,0 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import App from './App';
test('renders learn react link', () => {
render(<App />);
const linkElement = screen.getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
});

View File

@ -0,0 +1,13 @@
import millisFromDuration from "./millisFromDuration";
import { Duration } from '../generated/google/protobuf/duration';
describe('millisFromDuration', () => {
it('returns 0 if duration is not passed', () => {
expect(millisFromDuration()).toEqual(0);
});
it('correctly returns the ms when the duration has both seconds and nanos', () => {
const duration: Duration = { seconds: 34, nanos: 549_875_992 };
expect(millisFromDuration(duration)).toEqual(34_549);
});
});

View File

@ -0,0 +1,10 @@
import { Duration } from '../generated/google/protobuf/duration';
function millisFromDuration(dur?: Duration): number {
if (dur == undefined) {
return 0;
}
return Math.floor(dur.seconds * 1000.0 + dur.nanos / 1000.0 / 1000.0);
}
export default millisFromDuration;