fix: tests, adding tests for threshold feature

Signed-off-by: Chris Langhout <clanghout@bol.com>
This commit is contained in:
Chris Langhout
2022-11-25 13:31:43 +01:00
parent 9a341d5fd2
commit 7b6a317e87
2 changed files with 39 additions and 4 deletions
@@ -17,7 +17,7 @@
import React, { PropsWithChildren } from 'react';
import { renderInTestApp } from '@backstage/test-utils';
import { CostGrowth } from './CostGrowth';
import { Currency, CurrencyType, Duration } from '../../types';
import { ChangeThreshold, Currency, CurrencyType, Duration } from '../../types';
import { findAlways } from '../../utils/assert';
import { MockConfigProvider, MockCurrencyProvider } from '../../testUtils';
import { defaultCurrencies } from '../../utils/currency';
@@ -33,11 +33,16 @@ const MockContext = ({
children,
currency,
engineerCost,
engineerThreshold,
}: PropsWithChildren<{
currency: Currency;
engineerCost: number;
engineerThreshold?: number;
}>) => (
<MockConfigProvider engineerCost={engineerCost}>
<MockConfigProvider
engineerCost={engineerCost}
engineerThreshold={engineerThreshold ?? 0.5}
>
<MockCurrencyProvider currency={currency}>{children}</MockCurrencyProvider>
</MockConfigProvider>
);
@@ -104,3 +109,28 @@ describe.each`
expect(getByText(expected)).toBeInTheDocument();
});
});
describe.each`
ratio | amount | threshold | expected
${0} | ${0} | ${0} | ${'less than an engineer'}
${0} | ${0} | ${200} | ${'Negligible'}
${ChangeThreshold.lower} | ${0.5} | ${0} | ${'less than an engineer'}
${ChangeThreshold.lower} | ${0.5} | ${0.5} | ${'Negligible'}
${ChangeThreshold.upper} | ${0.5} | ${0.000001} | ${'less than an engineer'}
${ChangeThreshold.upper} | ${0.5} | ${0.5} | ${'Negligible'}
${3} | ${500_000} | ${0} | ${`300% or ~ 30 engineers`}
${3} | ${500_000} | ${30} | ${'Negligible'}
`('<CostGrowth />', ({ ratio, amount, threshold, expected }) => {
it(`should display the correct difference the threshold is different. ratio: ${ratio} threshold:${threshold} expected:${expected}`, async () => {
const { getByText } = await renderInTestApp(
<MockContext
engineerCost={200_000}
engineerThreshold={threshold}
currency={engineers}
>
<CostGrowth change={{ ratio, amount }} duration={Duration.P30D} />
</MockContext>,
);
expect(getByText(expected)).toBeInTheDocument();
});
});
@@ -18,6 +18,7 @@ import React from 'react';
import { renderInTestApp } from '@backstage/test-utils';
import { CostGrowthIndicator } from './CostGrowthIndicator';
import { ChangeThreshold, EngineerThreshold } from '../../types';
import { MockConfigProvider } from '../../testUtils';
describe.each`
ratio | amount | ariaLabel
@@ -30,7 +31,9 @@ describe.each`
`('growthOf', ({ ratio, amount, ariaLabel }) => {
it(`should display the correct indicator for ${ariaLabel}`, async () => {
const { getByLabelText } = await renderInTestApp(
<CostGrowthIndicator change={{ ratio, amount }} />,
<MockConfigProvider engineerThreshold={EngineerThreshold}>
<CostGrowthIndicator change={{ ratio, amount }} />
</MockConfigProvider>,
);
expect(getByLabelText(ariaLabel)).toBeInTheDocument();
});
@@ -48,7 +51,9 @@ describe.each`
`('growthOf', ({ ratio, amount }) => {
it('should display the correct indicator for negligible growth', async () => {
const { queryByLabelText } = await renderInTestApp(
<CostGrowthIndicator change={{ ratio, amount }} />,
<MockConfigProvider engineerThreshold={EngineerThreshold}>
<CostGrowthIndicator change={{ ratio, amount }} />
</MockConfigProvider>,
);
expect(queryByLabelText('savings')).not.toBeInTheDocument();
expect(queryByLabelText('excess')).not.toBeInTheDocument();