From 22dd92f3395ce15675ab639eeb3c837b2d0c6360 Mon Sep 17 00:00:00 2001 From: Michael Evans Date: Sat, 1 Jan 2022 17:23:58 -0600 Subject: [PATCH] Extract millisFromDuration helper and add tests --- frontend/package.json | 3 ++- frontend/src/App.test.tsx | 9 --------- frontend/src/helpers/millisFromDuration.test.ts | 13 +++++++++++++ frontend/src/helpers/millisFromDuration.ts | 10 ++++++++++ 4 files changed, 25 insertions(+), 10 deletions(-) delete mode 100644 frontend/src/App.test.tsx create mode 100644 frontend/src/helpers/millisFromDuration.test.ts create mode 100644 frontend/src/helpers/millisFromDuration.ts diff --git a/frontend/package.json b/frontend/package.json index e8fec40..d12a2ad 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -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": { diff --git a/frontend/src/App.test.tsx b/frontend/src/App.test.tsx deleted file mode 100644 index 2a68616..0000000 --- a/frontend/src/App.test.tsx +++ /dev/null @@ -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(); - const linkElement = screen.getByText(/learn react/i); - expect(linkElement).toBeInTheDocument(); -}); diff --git a/frontend/src/helpers/millisFromDuration.test.ts b/frontend/src/helpers/millisFromDuration.test.ts new file mode 100644 index 0000000..95b0415 --- /dev/null +++ b/frontend/src/helpers/millisFromDuration.test.ts @@ -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); + }); +}); diff --git a/frontend/src/helpers/millisFromDuration.ts b/frontend/src/helpers/millisFromDuration.ts new file mode 100644 index 0000000..b24eb9d --- /dev/null +++ b/frontend/src/helpers/millisFromDuration.ts @@ -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; \ No newline at end of file