Split the first test

This commit is contained in:
vinassefranche 2022-03-04 17:18:48 +01:00
parent a8a660aa8d
commit 51e335067d
2 changed files with 43 additions and 9 deletions

View file

@ -1,5 +1,6 @@
import { option } from 'fp-ts'; import { option } from 'fp-ts';
import { import {
getCountryCurrencyOfOptionalCountryCode,
getValidCountryCodeOfCountryNames, getValidCountryCodeOfCountryNames,
giveCurrencyOfCountryToUser, giveCurrencyOfCountryToUser,
performAsyncComputationInParallel, performAsyncComputationInParallel,
@ -7,6 +8,23 @@ import {
} from './exo5'; } from './exo5';
describe('exo5', () => { describe('exo5', () => {
describe('getCountryCurrencyOfOptionalCountryCode', () => {
it('should return a Task<None> if given a None', async () => {
const result = await getCountryCurrencyOfOptionalCountryCode(
option.none,
)();
expect(result).toStrictEqual(option.none);
});
it('should return a Task<Option> with the currency if given a Some', async () => {
const result = await getCountryCurrencyOfOptionalCountryCode(
option.some('FR'),
)();
expect(result).toStrictEqual(option.some('EUR'));
});
});
describe('giveCurrencyOfCountryToUser', () => { describe('giveCurrencyOfCountryToUser', () => {
it('should return Some<EUR> if provided string is "France"', async () => { it('should return Some<EUR> if provided string is "France"', async () => {
const result = await giveCurrencyOfCountryToUser('France')(); const result = await giveCurrencyOfCountryToUser('France')();

View file

@ -82,19 +82,35 @@ export const naiveGiveCurrencyOfCountryToUser = (
// currency. // currency.
// Let's do better than that! // Let's do better than that!
// Use `traverse` to implement `giveCurrencyOfCountryToUser` below which returns // First we need a way to transform our `Option<Task<Currency>>` to
// a `Task<Option<Currency>>`. // `Task<Option<Currency>>`
// That's precisely what traverse is about.
// Use `option.traverse` to implement `getCountryCurrencyOfOptionalCountryCode`
// below. This function takes an `Option<CountryCode>`, should apply
// `getCountryCurrency` to the `CountryCode` and make it so that the result
// is `Task<Option<Currency>>`
// //
// HINT: Take a look at `option.traverse` to transform an `Option<Task>` to // HINT: `option.traverse` asks for an Applicative as the first parameter. You
// a `Task<Option>` // can find it for `Task` in `task.ApplicativePar`
// HINT: you can find an applicative functor of `Task` in `task.ApplicativePar`
export const getCountryCurrencyOfOptionalCountryCode: (
optionalCountryCode: Option<CountryCode>,
) => Task<Option<Currency>> = unimplementedAsync;
// Let's now use this function in our naive implementation's pipe to how it
// improves it.
// Implement `giveCurrencyOfCountryToUser` below so that it returns a
// `Task<Option<Currency>>`
//
// HINT: You should be able to copy the pipe from naiveGiveCurrencyOfCountryToUser
// and make only few updates of it
export const giveCurrencyOfCountryToUser: ( export const giveCurrencyOfCountryToUser: (
countryNameFromUserMock: string, countryNameFromUserMock: string,
) => Task<Option<Currency>> = () => unimplementedAsync(); ) => Task<Option<Currency>> = unimplementedAsync;
// BONUS: We don't necessarily need `traverse` to do this. Try implementing // BONUS: We don't necessarily need `traverse` to do this. Try implementing
// `giveCurrencyOfCountryToUser` by lifting all the functions' results to // `giveCurrencyOfCountryToUser` by lifting some of the functions' results to
// `TaskOption` // `TaskOption`
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -168,7 +184,7 @@ const createSimulatedAsyncMethod = (): ((toAdd: number) => Task<number>) => {
export const simulatedAsyncMethodForParallel = createSimulatedAsyncMethod(); export const simulatedAsyncMethodForParallel = createSimulatedAsyncMethod();
export const performAsyncComputationInParallel: ( export const performAsyncComputationInParallel: (
numbers: ReadonlyArray<number>, numbers: ReadonlyArray<number>,
) => Task<ReadonlyArray<number>> = () => unimplementedAsync(); ) => Task<ReadonlyArray<number>> = unimplementedAsync;
// Write a method to traverse an array by running the method // Write a method to traverse an array by running the method
// `simulatedAsyncMethodForSequence: (toAdd: number) => Task<number>` // `simulatedAsyncMethodForSequence: (toAdd: number) => Task<number>`
@ -180,4 +196,4 @@ export const performAsyncComputationInParallel: (
export const simulatedAsyncMethodForSequence = createSimulatedAsyncMethod(); export const simulatedAsyncMethodForSequence = createSimulatedAsyncMethod();
export const performAsyncComputationInSequence: ( export const performAsyncComputationInSequence: (
numbers: ReadonlyArray<number>, numbers: ReadonlyArray<number>,
) => Task<ReadonlyArray<number>> = () => unimplementedAsync(); ) => Task<ReadonlyArray<number>> = unimplementedAsync;