This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function getPricesLastDays(days) { | |
try { | |
const result = await fetch(`https://api.gold/price?days=${days}`); | |
const prices = await result.json(); | |
const sum = prices.reduce((total, currentValue) => total + currentValue, 0); | |
const average = sum / prices.length; | |
return { average: +average.toFixed(2) }; | |
} catch (error) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('When the average price is called for 7 days', () => { | |
beforeEach(async () => { | |
averagePrice = await getPricesLastDays(7); | |
}); | |
it('Then the correct average should be returned', () => { | |
expect(averagePrice).toEqual({ average: MOCK_AVERAGE }); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const result = await fetch(`https://api.gold/price?days=${days}`); | |
const prices = await result.json(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MOCK_PRICES = [50, 47, 53, 50, 49, 51, 52]; | |
global.fetch = jest.fn(() => Promise.resolve({ | |
json: () => Promise.resolve(MOCK_PRICES) | |
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MOCK_PRICES = [50, 47, 53, 50, 49, 51, 52]; | |
const MOCK_AVERAGE = 50.29; | |
global.fetch = jest.fn(() => Promise.resolve({ | |
json: () => Promise.resolve(MOCK_PRICES) | |
})); | |
describe('Gold prices', () => { | |
let averagePrice; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('When the promise is rejected', () => { | |
beforeEach(async () => { | |
fetch.mockRejectedValueOnce(new Error("I'm a teapot")); | |
averagePrice = await getPricesLastDays(7); | |
}); | |
it('Then the fuction should return null', () => { | |
expect(averagePrice).toEqual(null); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useCallback } from 'react'; | |
import styles from './tabTitle.module.css'; | |
export type Props = { | |
title: string; | |
index: number; | |
setSelectedTab: (index: number) => void; | |
isActive?: boolean; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { ReactElement } from 'react'; | |
type Props = { | |
title: string; | |
children: ReactElement | ReactElement[]; | |
}; | |
const TabPane = ({ children }: Props): JSX.Element => <div>{children}</div>; | |
export default TabPane; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { ReactElement } from 'react'; | |
type Props = { | |
title: string; | |
children: ReactElement | ReactElement[]; | |
}; | |
const TabPane = ({ children }: Props): JSX.Element => <div>{children}</div>; | |
export default TabPane; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { ReactElement, useState } from 'react'; | |
import styles from './tabs.module.css'; | |
import TabTitle, { Props as TabTitleProps } from './TabTitle'; | |
type Props = { | |
children: ReactElement<TabTitleProps>[]; | |
preSelectedTabIndex?: number; | |
}; |