Skip to content

Commit f411be8

Browse files
committed
FDG-11112 adding test
1 parent bd2547f commit f411be8

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

src/layouts/explainer/sections/treasury-savings-bonds/how-savings-bonds-finance-government/how-savings-bonds-finance-government.spec.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { fireEvent, render, screen } from '@testing-library/react';
33
import '@testing-library/jest-dom';
4-
import HowSavingsBondsFinanceGovernment from './how-savings-bonds-finance-government';
4+
import HowSavingsBondsFinanceGovernment, { higherOrLowerOrSameAs } from './how-savings-bonds-finance-government';
55
import { RecoilRoot } from 'recoil';
66
import { useStaticQuery } from 'gatsby';
77
import fetchMock from 'fetch-mock';
@@ -150,12 +150,20 @@ describe('How Savings Bonds Finance The Government Section', () => {
150150
fireEvent.keyDown(getByRole('button', { name: 'View in glossary' }), { key: 'Escape', code: 'Escape', charCode: 27 });
151151
});
152152

153-
it('returns the correct text for the percentage of total debt held by public chart', () => {
154-
const { getByText } = render(
155-
<RecoilRoot>
156-
<HowSavingsBondsFinanceGovernment />
157-
</RecoilRoot>
158-
);
159-
expect(getByText('lower than')).toBeInTheDocument();
153+
describe('tests for higherOrLowerOrSameAs function logic', () => {
154+
it('returns "higher than" when the difference is greater than 0', () => {
155+
const result = higherOrLowerOrSameAs(2.3);
156+
expect(result).toBe('higher than');
157+
});
158+
159+
it('returns "lower than" when the difference less than 0', () => {
160+
const result = higherOrLowerOrSameAs(-0.4);
161+
expect(result).toBe('lower than');
162+
});
163+
164+
it('returns "the same as" when the difference equals 0', () => {
165+
const result = higherOrLowerOrSameAs(0);
166+
expect(result).toBe('the same as');
167+
});
160168
});
161169
});

src/layouts/explainer/sections/treasury-savings-bonds/how-savings-bonds-finance-government/how-savings-bonds-finance-government.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ interface ApiResponse {
3737
meta: { 'total-pages': number };
3838
}
3939

40+
export const higherOrLowerOrSameAs = difference => {
41+
if (difference > 0) {
42+
return 'higher than';
43+
} else if (difference < 0) {
44+
return 'lower than';
45+
} else {
46+
return 'the same as';
47+
}
48+
};
49+
4050
const HowSavingsBondsFinanceGovernment: FunctionComponent<{ width?: number }> = ({ width }) => {
4151
const [chartData, setChartData] = useState<ChartDataItem[]>([]);
4252
const [savingBondsPercentage, setSavingBondsPercentage] = useState<number | null>(null);
@@ -134,16 +144,6 @@ const HowSavingsBondsFinanceGovernment: FunctionComponent<{ width?: number }> =
134144
});
135145
}, []);
136146

137-
const higherOrLowerOrSameAs = difference => {
138-
if (difference > 0) {
139-
return 'higher than';
140-
} else if (difference < 0) {
141-
return 'lower than';
142-
} else {
143-
return 'the same as';
144-
}
145-
};
146-
147147
useEffect(() => {
148148
if (savingBondsPercentage !== null && historicalSavingBondsPercentage !== null) {
149149
const difference = savingBondsPercentage - historicalSavingBondsPercentage;

0 commit comments

Comments
 (0)