migrate assertion utils

This commit is contained in:
Ryan Vazquez
2020-10-28 11:02:48 -04:00
parent d20428775c
commit 86baf03274
15 changed files with 72 additions and 50 deletions
@@ -36,11 +36,11 @@ import { currencyFormatter } from '../../utils/formatters';
import {
BarChartData,
Maybe,
notEmpty,
ResourceData,
DataKey,
CostInsightsTheme,
} from '../../types';
import { notEmpty } from '../../utils/assert';
import { useBarChartStyles } from '../../utils/styles';
import { resourceSort } from '../../utils/sort';
@@ -17,11 +17,8 @@
import React, { PropsWithChildren } from 'react';
import { renderInTestApp } from '@backstage/test-utils';
import { CostGrowth } from './CostGrowth';
import {
Currency,
Duration,
findAlways,
} from '../../types';
import { Currency, Duration } from '../../types';
import { findAlways } from '../../utils/assert';
import { MockConfigProvider, MockCurrencyProvider } from '../../utils/tests';
import { defaultCurrencies, CurrencyType } from '../../utils/currency';
@@ -25,7 +25,7 @@ import {
} from '@material-ui/core';
import { useNavigationStyles } from '../../utils/styles';
import { useConfig, useScroll } from '../../hooks';
import { findAlways } from '../../types';
import { findAlways } from '../../utils/assert';
import {
DefaultNavigation,
NavigationItem,
@@ -66,10 +66,10 @@ export const CostInsightsNavigation = ({
})}
</Badge>
) : (
React.cloneElement(item.icon, {
className: classes.navigationIcon,
})
)
React.cloneElement(item.icon, {
className: classes.navigationIcon,
})
)
}
title={item.title}
/>
@@ -26,11 +26,11 @@ import { useScroll, useFilters, useConfig } from '../../hooks';
import { mapFiltersToProps } from './selector';
import { DefaultNavigation } from '../../utils/navigation';
import { formatPercent } from '../../utils/formatters';
import { findAlways } from '../../utils/assert';
import {
Cost,
CostInsightsTheme,
MetricData,
findAlways,
getComparedChange,
} from '../../types';
@@ -16,7 +16,9 @@
import React from 'react';
import { MenuItem, Select, SelectProps } from '@material-ui/core';
import { Currency, CurrencyType, findAlways } from '../../types';
import { Currency } from '../../types';
import { CurrencyType } from '../../utils/currency';
import { findAlways } from '../../utils/assert';
import { useSelectStyles as useStyles } from '../../utils/styles';
const NULL_VALUE = 'engineers';
@@ -20,7 +20,8 @@ import {
formatLastTwoLookaheadQuarters,
formatLastTwoMonths,
} from '../../utils/formatters';
import { Duration, findAlways } from '../../types';
import { Duration } from '../../types';
import { findAlways } from '../../utils/assert';
import { useSelectStyles as useStyles } from '../../utils/styles';
import { useLastCompleteBillingDate } from '../../hooks';
@@ -16,7 +16,8 @@
import { MapFiltersToProps } from '../../hooks/useFilters';
import { MapLoadingToProps } from '../../hooks/useLoading';
import { Duration, PageFilters, ProductPeriod, findAlways } from '../../types';
import { Duration, PageFilters, ProductPeriod } from '../../types';
import { findAlways } from '../../utils/assert';
type ProductInsightsCardFilterProps = PageFilters & {
product: ProductPeriod;
@@ -23,8 +23,9 @@ import {
MockConfigProvider,
MockBillingDateProvider,
} from '../../utils/tests';
import { AlertCost, findAlways } from '../../types';
import { AlertCost } from '../../types';
import { defaultCurrencies } from '../../utils/currency';
import { findAlways } from '../../utils/assert';
const engineers = findAlways(defaultCurrencies, c => c.kind === null);
@@ -17,7 +17,9 @@
import React, { PropsWithChildren } from 'react';
import { renderInTestApp } from '@backstage/test-utils';
import { ResourceGrowthBarChartLegend } from './ResourceGrowthBarChartLegend';
import { defaultCurrencies, Duration, findAlways } from '../../types';
import { Duration } from '../../types';
import { defaultCurrencies } from '../../utils/currency';
import { findAlways } from '../../utils/assert';
import { MockConfigProvider, MockCurrencyProvider } from '../../utils/tests';
const engineers = findAlways(defaultCurrencies, c => c.kind === null);
@@ -20,7 +20,8 @@ import React, {
useContext,
PropsWithChildren,
} from 'react';
import { Currency, findAlways } from '../types';
import { Currency } from '../types';
import { findAlways } from '../utils/assert';
import { defaultCurrencies } from '../utils/currency';
export type CurrencyContextProps = {
+1 -1
View File
@@ -15,7 +15,7 @@
*/
import moment from 'moment';
import { assertNever } from './Maybe';
import { assertNever } from '../utils/assert';
/**
* Time periods for cost comparison; slight abuse of ISO 8601 periods. We take P1M and P3M to mean
+1 -29
View File
@@ -13,33 +13,5 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export type Maybe<T> = T | null;
export function notEmpty<TValue>(
value: TValue | null | undefined,
): value is TValue {
return value !== null && value !== undefined;
}
// Utility for exhaustiveness checking in switch statements
export function assertNever(x: never): never {
throw new Error(`Exhaustiveness check failed: ${x}`);
}
export function assertAlways<T>(argument: T | undefined): T {
if (argument === undefined) {
throw new TypeError(
'Expected to always find a value but received undefined',
);
}
return argument;
}
// Utility for working with static lists; asserts a value will always be found or
// throws an error
export function findAlways<T>(
collection: T[],
callback: (el: T) => boolean,
): T {
return assertAlways(collection.find(callback));
}
+44
View File
@@ -0,0 +1,44 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export function notEmpty<TValue>(
value: TValue | null | undefined,
): value is TValue {
return value !== null && value !== undefined;
}
// Utility for exhaustiveness checking in switch statements
export function assertNever(x: never): never {
throw new Error(`Exhaustiveness check failed: ${x}`);
}
export function assertAlways<T>(argument: T | undefined): T {
if (argument === undefined) {
throw new TypeError(
'Expected to always find a value but received undefined',
);
}
return argument;
}
// Utility for working with static lists; asserts a value will always be found or
// throws an error
export function findAlways<T>(
collection: T[],
callback: (el: T) => boolean,
): T {
return assertAlways(collection.find(callback));
}
+2 -1
View File
@@ -13,7 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { assertNever, Currency, Duration } from '../types';
import { Currency, Duration } from '../types';
import { assertNever } from '../utils/assert';
export enum CurrencyType {
USD = 'USD',
+1 -1
View File
@@ -17,7 +17,6 @@
import {
Duration,
Entity,
findAlways,
Product,
ProductCost,
ProductFilters,
@@ -29,6 +28,7 @@ import {
DefaultLoadingAction,
getDefaultState as getDefaultLoadingState,
} from '../utils/loading';
import { findAlways } from '../utils/assert';
import { Config } from '@backstage/config';
import { ConfigApi } from '@backstage/core';